Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new MapType logical type #762

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
stloyd marked this conversation as resolved.
Show resolved Hide resolved
{
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 @@ -8,6 +8,7 @@
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\PHP\Type\Logical\List\ListElement;
use Flow\ETL\PHP\Type\Logical\ListType;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row\Entry;
use Flow\ETL\Row\Reference;
use Flow\ETL\Row\Schema\Definition;
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() : Type
{
return $this->type;
}
Expand Down
4 changes: 2 additions & 2 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,9 @@

namespace Flow\ETL\Row\Entry;

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

interface TypedCollection
{
public function type() : ListType;
public function type() : Type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Flow\ETL\Row\Schema\Constraint;

use Flow\ETL\PHP\Type\Logical\ListType;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row\Entry;
use Flow\ETL\Row\Schema\Constraint;
Expand Down Expand Up @@ -33,6 +34,11 @@ public function isSatisfiedBy(Entry $entry) : bool
return false;
}

if (!$entry->type() instanceof ListType) {
return false;
}

/** @psalm-suppress UndefinedInterfaceMethod */
return $entry->type()->element()->isEqual($this->type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\PHP\Type\Logical;

use Flow\ETL\PHP\Type\Logical\List\ListElement;
use Flow\ETL\PHP\Type\Logical\ListType;
use Flow\ETL\PHP\Type\Logical\Map\MapKey;
use Flow\ETL\PHP\Type\Logical\Map\MapValue;
use Flow\ETL\PHP\Type\Logical\MapType;
use PHPUnit\Framework\TestCase;

final class MapTypeTest extends TestCase
{
public function test_equals() : void
{
$this->assertTrue(
(new MapType(MapKey::string(), MapValue::float()))->isEqual(new MapType(MapKey::string(), MapValue::float()))
);
$this->assertFalse(
(new MapType(MapKey::string(), MapValue::float()))->isEqual(new ListType(ListElement::integer()))
);
$this->assertFalse(
(new MapType(MapKey::string(), MapValue::float()))->isEqual(new MapType(MapKey::string(), MapValue::integer()))
);
}

public function test_key() : void
{
$this->assertEquals(
$key = MapKey::string(),
(new MapType($key, MapValue::float()))->key()
);
}

public function test_to_string() : void
{
$this->assertSame(
'map<string, string>',
(new MapType(MapKey::string(), MapValue::string()))->toString()
);
}

public function test_valid() : void
{
$this->assertTrue(
(new MapType(MapKey::string(), MapValue::string()))->isValid(['one' => 'two'])
);
$this->assertFalse(
(new MapType(MapKey::integer(), MapValue::string()))->isValid(['one' => 'two'])
);
$this->assertFalse(
(new MapType(MapKey::integer(), MapValue::string()))->isValid([1, 2])
);
$this->assertFalse(
(new MapType(MapKey::string(), MapValue::string()))->isValid(123)
);
}

public function test_value() : void
{
$this->assertEquals(
$value = MapValue::string(),
(new MapType(MapKey::string(), $value))->value()
);
}
}