-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new
ListType
logical type (#749)
* Implement new `ListType` logical type * Improve implementation after review
- Loading branch information
Showing
21 changed files
with
230 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/core/etl/src/Flow/ETL/PHP/Type/Logical/List/ListElement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\ETL\PHP\Type\Logical\List; | ||
|
||
use Flow\ETL\PHP\Type\Native\ObjectType; | ||
use Flow\ETL\PHP\Type\Native\ScalarType; | ||
|
||
final class ListElement | ||
{ | ||
private function __construct(private readonly ScalarType|ObjectType $value) | ||
{ | ||
} | ||
|
||
public static function boolean() : self | ||
{ | ||
return new self(ScalarType::boolean); | ||
} | ||
|
||
public static function float() : self | ||
{ | ||
return new self(ScalarType::float); | ||
} | ||
|
||
public static function integer() : self | ||
{ | ||
return new self(ScalarType::integer); | ||
} | ||
|
||
/** | ||
* @param class-string $class | ||
*/ | ||
public static function object(string $class) : self | ||
{ | ||
return new self(new ObjectType($class)); | ||
} | ||
|
||
public static function scalar(string $value) : self | ||
{ | ||
return new self(ScalarType::fromString($value)); | ||
} | ||
|
||
public static function string() : self | ||
{ | ||
return new self(ScalarType::string); | ||
} | ||
|
||
public function isEqual(mixed $value) : bool | ||
{ | ||
return $this->value->isEqual($value); | ||
} | ||
|
||
public function isValid(mixed $value) : bool | ||
{ | ||
return $this->value->isValid($value); | ||
} | ||
|
||
public function toString() : string | ||
{ | ||
return $this->value->toString(); | ||
} | ||
|
||
public function value() : ScalarType|ObjectType | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\ETL\PHP\Type\Logical; | ||
|
||
use Flow\ETL\PHP\Type\Logical\List\ListElement; | ||
use Flow\ETL\PHP\Type\Type; | ||
use Flow\Serializer\Serializable; | ||
|
||
/** | ||
* @implements Serializable<array{element: ListElement}> | ||
*/ | ||
final class ListType implements Serializable, Type | ||
{ | ||
public function __construct(private readonly List\ListElement $element) | ||
{ | ||
} | ||
|
||
public function __serialize() : array | ||
{ | ||
return ['element' => $this->element]; | ||
} | ||
|
||
public function __unserialize(array $data) : void | ||
{ | ||
$this->element = $data['element']; | ||
} | ||
|
||
public function element() : ListElement | ||
{ | ||
return $this->element; | ||
} | ||
|
||
public function isEqual(Type $type) : bool | ||
{ | ||
if (!$type instanceof self) { | ||
return false; | ||
} | ||
|
||
return $this->element->toString() === $type->element()->toString(); | ||
} | ||
|
||
public function isValid(mixed $value) : bool | ||
{ | ||
if (!\is_array($value)) { | ||
return false; | ||
} | ||
|
||
if ([] !== $value && !\array_is_list($value)) { | ||
return false; | ||
} | ||
|
||
foreach ($value as $item) { | ||
if (!$this->element->isValid($item)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function toString() : string | ||
{ | ||
return $this->element->toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.