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

Add TypeFactory, ArrayType, NullType, ResourceType & CallableType #772

Merged
merged 6 commits into from
Nov 9, 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
70 changes: 70 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/ArrayContentDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\PHP\Type;

use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\PHP\Type\Native\ArrayType;
use Flow\ETL\PHP\Type\Native\NullType;
use Flow\ETL\PHP\Type\Native\ScalarType;

final class ArrayContentDetector
{
public function __construct(private readonly Types $uniqueKeysType, private readonly Types $uniqueValuesType)
{
}

public function firstKeyType() : ?ScalarType
{
$type = $this->uniqueKeysType->first();

if (null !== $type && !$type instanceof ScalarType) {
throw InvalidArgumentException::because('First unique key type must be of ScalarType, given: ' . $type::class);
}

return $type;
}

public function firstValueType() : ?Type
{
return $this->uniqueValuesType->first();
}

public function isList() : bool
{
if (!$this->firstKeyType()?->isInteger()) {
return false;
}

return 1 === $this->uniqueValuesType->without(ArrayType::empty(), new NullType())->count();
}

public function isMap() : bool
{
if (1 === $this->uniqueValuesType->without(ArrayType::empty(), new NullType())->count()) {
if ($this->isList()) {
return false;
}

if (!$this->firstKeyType()?->isValidArrayKey()) {
return false;
}

return 1 === $this->uniqueKeysType->count();
}

return false;
}

public function isStructure() : bool
{
if ($this->isList() || $this->isMap()) {
return false;
}

return $this->firstKeyType()?->isString()
&& 1 === $this->uniqueKeysType->count()
&& 0 !== $this->uniqueValuesType->without(ArrayType::empty(), new NullType())->count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static function float() : self
return new self(ScalarType::float());
}

public static function fromType(Type $type) : self
{
return new self($type);
}

public static function integer() : self
{
return new self(ScalarType::integer());
Expand Down
5 changes: 5 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
Expand Up @@ -10,6 +10,11 @@ private function __construct(private readonly ScalarType $value)
{
}

public static function fromType(ScalarType $type) : self
{
return new self($type);
}

public static function integer() : self
{
return new self(ScalarType::integer());
Expand Down
5 changes: 5 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
Expand Up @@ -24,6 +24,11 @@ public static function float() : self
return new self(ScalarType::float());
}

public static function fromType(Type $type) : self
{
return new self($type);
}

public static function integer() : self
{
return new self(ScalarType::integer());
Expand Down
37 changes: 37 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ArrayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace Flow\ETL\PHP\Type\Native;

use Flow\ETL\PHP\Type\Type;

final class ArrayType implements NativeType
{
public function __construct(private readonly bool $empty = false)
{
}

public static function empty() : self
{
return new self(true);
}

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

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

public function toString() : string
{
if ($this->empty) {
return 'array<empty, empty>';
}

return 'array<mixed>';
stloyd marked this conversation as resolved.
Show resolved Hide resolved
}
}
24 changes: 24 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/CallableType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Flow\ETL\PHP\Type\Native;

use Flow\ETL\PHP\Type\Type;

final class CallableType implements NativeType
{
public function isEqual(Type $type) : bool
{
return $type instanceof self;
}

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

public function toString() : string
{
return 'callable';
}
}
24 changes: 24 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/NullType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Flow\ETL\PHP\Type\Native;

use Flow\ETL\PHP\Type\Type;

final class NullType implements NativeType
{
public function isEqual(Type $type) : bool
{
return $type instanceof self;
}

public function isValid(mixed $value) : bool
{
return null === $value;
}

public function toString() : string
{
return 'null';
}
}
24 changes: 24 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ResourceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Flow\ETL\PHP\Type\Native;

use Flow\ETL\PHP\Type\Type;

final class ResourceType implements NativeType
{
public function isEqual(Type $type) : bool
{
return $type instanceof self;
}

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

public function toString() : string
{
return 'resource';
}
}
31 changes: 29 additions & 2 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ScalarType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ final class ScalarType implements NativeType

public const STRING = 'string';

private function __construct(private readonly string $value, private readonly bool $optional)
private readonly string $value;

private function __construct(string $value, private readonly bool $optional)
{
match (\strtolower($value)) {
$this->value = match (\strtolower($value)) {
'integer' => self::INTEGER,
'float', 'double' => self::FLOAT,
'string' => self::STRING,
Expand Down Expand Up @@ -53,11 +55,31 @@ public static function string(bool $optional = false) : self
return new self(self::STRING, $optional);
}

public function isBoolean() : bool
{
return $this->value === self::BOOLEAN;
}

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

public function isFloat() : bool
{
return $this->value === self::FLOAT;
}

public function isInteger() : bool
{
return $this->value === self::INTEGER;
}

public function isString() : bool
{
return $this->value === self::STRING;
}

public function isValid(mixed $value) : bool
{
if (null === $value && $this->optional) {
Expand All @@ -82,6 +104,11 @@ public function isValid(mixed $value) : bool
return true;
}

public function isValidArrayKey() : bool
{
return $this->isString() || $this->isInteger();
}

public function optional() : bool
{
return $this->optional;
Expand Down
72 changes: 72 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/TypeFactory.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;

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\Logical\Map\MapKey;
use Flow\ETL\PHP\Type\Logical\Map\MapValue;
use Flow\ETL\PHP\Type\Logical\MapType;
use Flow\ETL\PHP\Type\Logical\Structure\StructureElement;
use Flow\ETL\PHP\Type\Logical\StructureType;
use Flow\ETL\PHP\Type\Native\ArrayType;
use Flow\ETL\PHP\Type\Native\NullType;
use Flow\ETL\PHP\Type\Native\ObjectType;
use Flow\ETL\PHP\Type\Native\ScalarType;

final class TypeFactory
{
public function getType(mixed $value) : Type
{
stloyd marked this conversation as resolved.
Show resolved Hide resolved
if (null === $value) {
return new NullType();
}

if (\is_scalar($value)) {
return ScalarType::fromString(\gettype($value));
}

if (\is_object($value)) {
return ObjectType::of($value::class);
}

if (\is_array($value)) {
if ([] === $value) {
return ArrayType::empty();
}

$detector = new ArrayContentDetector(
new Types(...\array_map([$this, 'getType'], \array_keys($value))),
new Types(...\array_map([$this, 'getType'], \array_values($value)))
);

$firstKey = $detector->firstKeyType();
$firstValue = $detector->firstValueType();

if ($detector->isList() && $firstValue) {
return new ListType(ListElement::fromType($firstValue));
}

if ($detector->isMap() && $firstKey && $firstValue) {
return new MapType(MapKey::fromType($firstKey), MapValue::fromType($firstValue));
}

if ($detector->isStructure()) {
$elements = [];

foreach ($value as $key => $item) {
$elements[] = new StructureElement($key, $this->getType($item));
}

return new StructureType(...$elements);
}

return new ArrayType([] === \array_filter($value, fn ($value) : bool => null !== $value));
norberttech marked this conversation as resolved.
Show resolved Hide resolved
}

throw InvalidArgumentException::because('Unsupported type given: ' . \gettype($value));
}
}
46 changes: 46 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\PHP\Type;

final class Types implements \Countable
{
private readonly ?Type $first;

private readonly array $types;

public function __construct(Type ...$types)
{
$this->types = \array_map(
fn (string $type) : Type => \unserialize($type),
\array_unique(
\array_map(fn (Type $type) : string => \serialize($type), $types)
)
);
$this->first = $this->types[0] ?? null;
}

public function count() : int
{
return \count($this->types);
}

public function first() : ?Type
{
return $this->first;
}

public function without(Type ...$types) : self
{
return new self(...\array_filter($this->types, function (Type $type) use ($types) : bool {
foreach ($types as $withoutType) {
if ($type->isEqual($withoutType)) {
return false;
}
}

return true;
}));
}
}
Loading