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 4 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
71 changes: 71 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,71 @@
<?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\ScalarType;

final class ArrayContentDetector
{
private readonly Type $firstKeyType;

private readonly Type $firstValueType;

public function __construct(private readonly Types $uniqueKeysType, private readonly Types $uniqueValuesType)
{
$this->firstKeyType = $this->uniqueKeysType->first();
$this->firstValueType = $this->uniqueValuesType->first();
stloyd marked this conversation as resolved.
Show resolved Hide resolved
}

public function firstKeyType() : ScalarType
{
if (!$this->firstKeyType instanceof ScalarType) {
throw InvalidArgumentException::because('First unique key type must be of ScalarType, given: ' . $this->firstKeyType::class);
}

return $this->firstKeyType;
}

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

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

return 1 === $this->uniqueValuesType->filter(fn (Type $type) : bool => !($type instanceof ArrayType && $type->empty()))->count();
}

public function isMap() : bool
{
if (1 === $this->uniqueValuesType->count()) {
if ($this->isList()) {
return false;
}

if (!($this->firstKeyType()->isString() || $this->firstKeyType()->isInteger())) {
return false;
}

return 1 === $this->uniqueKeysType->filter(fn (Type $type) : bool => !($type instanceof ArrayType && $type->empty()))->count();
}

return false;
}

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

return $this->firstKeyType()->isString() && 1 === $this->uniqueKeysType->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 function empty() : bool
{
return $this->empty;
}

public function isEqual(Type $type) : bool
{
return $type instanceof self;
stloyd marked this conversation as resolved.
Show resolved Hide resolved
}

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';
}
}
26 changes: 24 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 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 ([] === \array_filter($value, fn ($value) : bool => null !== $value)) {
stloyd marked this conversation as resolved.
Show resolved Hide resolved
return new ArrayType(true);
}

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

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

if ($detector->isMap()) {
return new MapType(
MapKey::fromType($detector->firstKeyType()),
MapValue::fromType($detector->firstValueType())
);
}

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

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

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

return new ArrayType();
}

throw InvalidArgumentException::because('Unsupported type given: ' . \gettype($value));
}
}
58 changes: 58 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,58 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\PHP\Type;

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

final class Types implements \Countable
{
private readonly array $types;

private function __construct(Type ...$types)
{
$this->types = \array_map(
fn (string $type) : Type => \unserialize($type),
\array_unique(
\array_map(
fn (Type $type) : string => \serialize($type),
\array_filter($types, fn (Type $type) : bool => !$type instanceof NullType)
stloyd marked this conversation as resolved.
Show resolved Hide resolved
)
)
);
}

public static function create(Type ...$types) : self
{
if (0 === \count($types)) {
throw new InvalidArgumentException('Type list cannot be empty');
}

return new self(...$types);
}

public function all() : array
stloyd marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->types;
}

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

/**
* @param callable(Type) : bool $callable
*/
public function filter(callable $callable) : self
stloyd marked this conversation as resolved.
Show resolved Hide resolved
{
return new self(...\array_filter($this->types, $callable));
}

public function first() : Type
{
return $this->types[0];
stloyd marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading