Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function test_transforming_row() : void
'integer' => '1',
'float' => '1.0',
'boolean' => 'true',
'json' => '{"foo":"bar"}',
'array' => ['foo' => 'bar', 1 => 6.66],
'datetime' => '2021-01-01 00:00:00',
'null' => 'null',
'nil' => 'nil',
Expand All @@ -33,7 +33,7 @@ public function test_transforming_row() : void
'integer' => 1,
'float' => 1.0,
'boolean' => true,
'json' => ['foo' => 'bar'],
'array' => ['foo' => 'bar', 1 => 6.66],
'datetime' => new \DateTimeImmutable('2021-01-01 00:00:00'),
'null' => null,
'nil' => null,
Expand Down
25 changes: 3 additions & 22 deletions src/lib/types/src/Flow/Types/Type/AutoCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
type_date,
type_datetime,
type_float,
type_integer,
type_json,
type_uuid};
type_integer};
use Flow\Types\Type\Native\String\StringTypeChecker;

final readonly class AutoCaster
Expand All @@ -36,24 +34,15 @@ public function cast(mixed $value) : mixed
*/
private function castArray(array $value) : array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm I think I messed up a bit this castArray.

The only thing it does now is that it narrows array types.

For example when we have array [1, 2, 3.0] in which we have int's and float, it will narrow the type to float as all int can be represented by float however I don't think this should be responsibility of a class called AutoCaster 😅
Probably I was trying to fix some bug and I did it here without properly thinking this through 🤦‍♂️

I think this is much bigger issue that will need to be solved in separated PR (probably will require some more explicit array types narrowing mechanism) but for now removing key types makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
$keyTypes = [];
$valueTypes = [];

foreach ($value as $key => $item) {
$keyType = get_type($key);
foreach ($value as $item) {
$valueType = get_type($item);
$keyTypes[$keyType->toString()] = $keyType;
$valueTypes[$valueType->toString()] = $valueType;
}

if (isset($valueTypes['integer'], $valueTypes['float']) && \count($valueTypes) === 2) {
$castedArray = [];

foreach ($value as $key => $item) {
$castedArray[$key] = type_float()->cast($item);
}

return $castedArray;
return \array_map(fn ($item) => type_float()->cast($item), $value);
}

return $value;
Expand All @@ -79,14 +68,6 @@ private function castToString(string $value) : mixed
return type_boolean()->cast($value);
}

if ($typeChecker->isJson()) {
return type_json()->cast($value);
}

if ($typeChecker->isUuid()) {
return type_uuid()->cast($value);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think those two can be removed, it seems that we are missing tests for them but by removing those lines following code will fail, breaking backward compatibility of caster:

<?php

declare(strict_types=1);

use Flow\Types\Type\AutoCaster;
use Symfony\Component\Uid\Uuid;

require_once __DIR__ . '/vendor/autoload.php';

$output = (new AutoCaster())->cast(Uuid::v7()->toString());

dd($output); // this should be Uuid instance, not string 


if ($typeChecker->isDate()) {
return type_date()->cast($value);
}
Expand Down