Skip to content

Commit

Permalink
Implement new MapType logical type
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Nov 7, 2023
1 parent f9aac0a commit a913e7a
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/PHP/Type/Logical/ListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @implements Serializable<array{element: ListElement}>
*/
final class ListType implements Serializable, Type
final class ListType implements LogicalType, Serializable
{
public function __construct(private readonly List\ListElement $element)
{
Expand Down
11 changes: 11 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/LogicalType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\PHP\Type\Logical;

use Flow\ETL\PHP\Type\Type;

interface LogicalType extends Type
{
}
42 changes: 42 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/Map/MapKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace Flow\ETL\PHP\Type\Logical\Map;

use Flow\ETL\PHP\Type\Native\ScalarType;

final class MapKey
{
private function __construct(private readonly ScalarType $value)
{
}

public static function integer() : self
{
return new self(ScalarType::integer);
}

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
{
return $this->value;
}
}
66 changes: 66 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/Map/MapValue.php
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\Map;

use Flow\ETL\PHP\Type\Native\ObjectType;
use Flow\ETL\PHP\Type\Native\ScalarType;

final class MapValue
{
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;
}
}
72 changes: 72 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/MapType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

namespace Flow\ETL\PHP\Type\Logical;

use Flow\ETL\PHP\Type\Logical\Map\MapKey;
use Flow\ETL\PHP\Type\Logical\Map\MapValue;
use Flow\ETL\PHP\Type\Type;
use Flow\Serializer\Serializable;

/**
* @implements Serializable<array{key: MapKey, value: MapValue}>
*/
final class MapType implements LogicalType, Serializable
{
public function __construct(private readonly Map\MapKey $key, private readonly Map\MapValue $value)
{
}

public function __serialize() : array
{
return ['key' => $this->key, 'value' => $this->value];
}

public function __unserialize(array $data) : void
{
$this->key = $data['key'];
$this->value = $data['value'];
}

public function isEqual(Type $type) : bool
{
if (!$type instanceof self) {
return false;
}

return $this->key->toString() === $type->key()->toString() && $this->value->toString() === $type->value()->toString();
}

public function isValid(mixed $value) : bool
{
if (!\is_array($value)) {
return false;
}

foreach ($value as $key => $item) {
if (!$this->key->isValid($key)) {
return false;
}

if (!$this->value->isValid($item)) {
return false;
}
}

return true;
}

public function key() : MapKey
{
return $this->key;
}

public function toString() : string
{
return 'map<' . $this->key->toString() . ', ' . $this->value->toString() . '>';
}

public function value() : MapValue
{
return $this->value;
}
}
3 changes: 2 additions & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/ListEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @template T
*
* @implements Entry<array<T>, array{name: string, type: ListType, value: array<T>}>
* @implements TypedCollection<ListType>
*/
final class ListEntry implements Entry, TypedCollection
{
Expand Down Expand Up @@ -109,7 +110,7 @@ public function toString() : string
return \json_encode($this->value(), JSON_THROW_ON_ERROR);
}

public function type() : ListType
public function type()
{
return $this->type;
}
Expand Down
10 changes: 7 additions & 3 deletions src/core/etl/src/Flow/ETL/Row/Entry/TypedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace Flow\ETL\Row\Entry;

use Flow\ETL\PHP\Type\Logical\ListType;

/**
* @template T
*/
interface TypedCollection
{
public function type() : ListType;
/**
* @return T
*/
public function type();
}

0 comments on commit a913e7a

Please sign in to comment.