Skip to content

Commit

Permalink
Make types implementation serializable, mark native types as nullable (
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Nov 10, 2023
1 parent e4a4403 commit d2f1ec6
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function map(MapType $type) : self
*/
public static function object(string $class) : self
{
return new self(new ObjectType($class));
return new self(ObjectType::of($class));
}

public static function scalar(string $value) : self
Expand Down
5 changes: 2 additions & 3 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/ListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Flow\ETL\PHP\Type\Logical\List\ListElement;
use Flow\ETL\PHP\Type\Type;
use Flow\Serializer\Serializable;

/**
* @implements Serializable<array{element: ListElement}>
* @implements LogicalType<array{element: ListElement}>
*/
final class ListType implements LogicalType, Serializable
final class ListType implements LogicalType
{
public function __construct(private readonly List\ListElement $element)
{
Expand Down
5 changes: 5 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/LogicalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

use Flow\ETL\PHP\Type\Type;

/**
* @template T
*
* @extends Type<T>
*/
interface LogicalType extends Type
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function map(MapType $type) : self
*/
public static function object(string $class) : self
{
return new self(new ObjectType($class));
return new self(ObjectType::of($class));
}

public static function scalar(string $value) : self
Expand Down
5 changes: 2 additions & 3 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/MapType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
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}>
* @implements LogicalType<array{key: MapKey, value: MapValue}>
*/
final class MapType implements LogicalType, Serializable
final class MapType implements LogicalType
{
public function __construct(private readonly Map\MapKey $key, private readonly Map\MapValue $value)
{
Expand Down
5 changes: 2 additions & 3 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/StructureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\PHP\Type\Logical\Structure\StructureElement;
use Flow\ETL\PHP\Type\Type;
use Flow\Serializer\Serializable;

/**
* @implements Serializable<array{elements: array<StructureElement>}>
* @implements LogicalType<array{elements: array<StructureElement>}>
*/
final class StructureType implements LogicalType, Serializable
final class StructureType implements LogicalType
{
/**
* @var array<StructureElement>
Expand Down
18 changes: 18 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use Flow\ETL\PHP\Type\Type;

/**
* @implements NativeType<array{empty: bool}>
*/
final class ArrayType implements NativeType
{
public function __construct(private readonly bool $empty = false)
Expand All @@ -16,6 +19,16 @@ public static function empty() : self
return new self(true);
}

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

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

public function isEqual(Type $type) : bool
{
return $type instanceof self && $this->empty === $type->empty;
Expand All @@ -26,6 +39,11 @@ public function isValid(mixed $value) : bool
return \is_array($value);
}

public function nullable() : bool
{
return false;
}

public function toString() : string
{
if ($this->empty) {
Expand Down
27 changes: 25 additions & 2 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,43 @@

use Flow\ETL\PHP\Type\Type;

/**
* @implements NativeType<array{nullable: bool}>
*/
final class CallableType implements NativeType
{
public function __construct(private readonly bool $nullable)
{

}

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

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

public function isEqual(Type $type) : bool
{
return $type instanceof self;
return $type instanceof self && $this->nullable === $type->nullable;
}

public function isValid(mixed $value) : bool
{
return \is_callable($value);
}

public function nullable() : bool
{
return $this->nullable;
}

public function toString() : string
{
return 'callable';
return ($this->nullable ? '?' : '') . 'callable';
}
}
6 changes: 6 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/NativeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

use Flow\ETL\PHP\Type\Type;

/**
* @template T
*
* @extends Type<T>
*/
interface NativeType extends Type
{
public function nullable() : bool;
}
18 changes: 18 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@

use Flow\ETL\PHP\Type\Type;

/**
* @implements NativeType<array>
*/
final class NullType implements NativeType
{
public function __serialize() : array
{
return [];
}

public function __unserialize(array $data) : void
{

}

public function isEqual(Type $type) : bool
{
return $type instanceof self;
Expand All @@ -17,6 +30,11 @@ public function isValid(mixed $value) : bool
return null === $value;
}

public function nullable() : bool
{
return true;
}

public function toString() : string
{
return 'null';
Expand Down
25 changes: 22 additions & 3 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\PHP\Type\Type;

/**
* @implements NativeType<array{class: class-string, nullable: bool}>
*/
final class ObjectType implements NativeType
{
/**
* @param class-string $class
*/
public function __construct(public readonly string $class)
public function __construct(public readonly string $class, private readonly bool $nullable)
{
if (!\class_exists($class) && !\interface_exists($class)) {
throw new InvalidArgumentException("Class {$class} not found");
Expand All @@ -24,7 +27,18 @@ public function __construct(public readonly string $class)
*/
public static function of(string $class) : self
{
return new self($class);
return new self($class, false);
}

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

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

public function isEqual(Type $type) : bool
Expand All @@ -37,8 +51,13 @@ public function isValid(mixed $value) : bool
return $value instanceof $this->class;
}

public function nullable() : bool
{
return $this->nullable;
}

public function toString() : string
{
return 'object<' . $this->class . '>';
return ($this->nullable ? '?' : '') . 'object<' . $this->class . '>';
}
}
27 changes: 25 additions & 2 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,43 @@

use Flow\ETL\PHP\Type\Type;

/**
* @implements NativeType<array{nullable: bool}>
*/
final class ResourceType implements NativeType
{
public function __construct(private readonly bool $nullable)
{

}

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

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

public function isEqual(Type $type) : bool
{
return $type instanceof self;
return $type instanceof self && $this->nullable === $type->nullable;
}

public function isValid(mixed $value) : bool
{
return \is_resource($value);
}

public function nullable() : bool
{
return $this->nullable;
}

public function toString() : string
{
return 'resource';
return ($this->nullable ? '?' : '') . 'resource';
}
}
24 changes: 19 additions & 5 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ScalarType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\PHP\Type\Type;

/**
* @implements NativeType<array{value: string, nullable: bool}>
*/
final class ScalarType implements NativeType
{
public const BOOLEAN = 'boolean';
Expand All @@ -19,7 +22,7 @@ final class ScalarType implements NativeType

private readonly string $value;

private function __construct(string $value, private readonly bool $optional)
private function __construct(string $value, private readonly bool $nullable)
{
$this->value = match (\strtolower($value)) {
'integer' => self::INTEGER,
Expand Down Expand Up @@ -55,6 +58,17 @@ public static function string(bool $optional = false) : self
return new self(self::STRING, $optional);
}

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

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

public function isBoolean() : bool
{
return $this->value === self::BOOLEAN;
Expand Down Expand Up @@ -82,7 +96,7 @@ public function isString() : bool

public function isValid(mixed $value) : bool
{
if (null === $value && $this->optional) {
if (null === $value && $this->nullable) {
return true;
}

Expand All @@ -109,13 +123,13 @@ public function isValidArrayKey() : bool
return $this->isString() || $this->isInteger();
}

public function optional() : bool
public function nullable() : bool
{
return $this->optional;
return $this->nullable;
}

public function toString() : string
{
return ($this->optional ? '?' : '') . $this->value;
return ($this->nullable ? '?' : '') . $this->value;
}
}
9 changes: 8 additions & 1 deletion src/core/etl/src/Flow/ETL/PHP/Type/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

namespace Flow\ETL\PHP\Type;

interface Type
use Flow\Serializer\Serializable;

/**
* @template T
*
* @extends Serializable<T>
*/
interface Type extends Serializable
{
public function isEqual(self $type) : bool;

Expand Down
Loading

0 comments on commit d2f1ec6

Please sign in to comment.