Skip to content

Commit cb3064f

Browse files
committed
add InputValueOnlyBaseTypeCast
1 parent c4dcf98 commit cb3064f

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

docs/en/mapper/out.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ $user = User::from([
2727
### Outputting the Object
2828

2929
```php
30-
// $user is an object by default
30+
// $user is an object by default
3131
echo $user->name; // Output: Jon
3232
echo $user->age; // Output: 30
3333
echo $user->login_log->remark; // Output 'Test Data'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Astral\Serialize\Casts\InputValue;
6+
7+
use Astral\Serialize\Contracts\Attribute\InputValueCastInterface;
8+
use Astral\Serialize\Enums\TypeKindEnum;
9+
use Astral\Serialize\Support\Collections\DataCollection;
10+
use Astral\Serialize\Support\Context\InputValueContext;
11+
use stdClass;
12+
13+
class InputValueOnlyBaseTypeCast implements InputValueCastInterface
14+
{
15+
public function match(mixed $value, DataCollection $collection, InputValueContext $context): bool
16+
{
17+
return $value && $collection->isNullable() === false && count($collection->getTypes()) == 1 ;
18+
}
19+
20+
public function resolve(mixed $value, DataCollection $collection, InputValueContext $context): mixed
21+
{
22+
23+
// if($context->getTypes()[0]->kind == TypeKindEnum::FLOAT){
24+
// print_r($context);
25+
// }
26+
27+
return match ($collection->getTypes()[0]->kind) {
28+
TypeKindEnum::INT => (int)$value,
29+
TypeKindEnum::FLOAT => (float)$value,
30+
TypeKindEnum::STRING => (string)$value,
31+
TypeKindEnum::BOOLEAN => (bool)$value,
32+
default => $value,
33+
};
34+
}
35+
}

src/Support/Config/ConfigManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Astral\Serialize\Casts\InputValue\InputObjectBestMatchChildCast;
88
use Astral\Serialize\Casts\InputValue\InputValueEnumCast;
99
use Astral\Serialize\Casts\InputValue\InputValueNullCast;
10+
use Astral\Serialize\Casts\InputValue\InputValueOnlyBaseTypeCast;
1011
use Astral\Serialize\Casts\Normalizer\ArrayNormalizerCast;
1112
use Astral\Serialize\Casts\Normalizer\DateTimeNormalizerCast;
1213
use Astral\Serialize\Casts\Normalizer\JsonNormalizerCast;
@@ -36,6 +37,7 @@ class ConfigManager
3637
InputArraySingleChildCast::class,
3738
InputArrayBestMatchChildCast::class,
3839
InputValueEnumCast::class,
40+
InputValueOnlyBaseTypeCast::class,
3941
];
4042

4143
/** @var (OutValueCastInterface|string)[] $outputValueCasts */

tests/Serialize/From/FromSerializeTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343
'type_float' => 0.02,
4444
'withoutType' => 'hhh',
4545
],
46-
type_float:null,
46+
type_float:null, // 'type_float' => 0.02 change to null
4747
input_name:null,
4848
type_object:null,
4949
type_mixed_other: ['abc' => ['bbb' => ['ccc' => 'dddd'],['abc']],'aaa','bbb','ccc',''],
@@ -61,3 +61,28 @@ public function __construct(
6161
->and($object->type_mixed_other['abc']['bbb']['ccc'])->toBe('dddd')
6262
->and($object->type_collect_object)->toBeInstanceOf(StdClass::class);
6363
});
64+
65+
it('test parse construct Serialize class', function () {
66+
67+
$object = TestFromSerialize::from(
68+
[
69+
'input_name' => [ fn () => new stdClass()],
70+
'type_string' => 123,
71+
'type_int' => '11',
72+
'type_float' => '0.01',
73+
'type_object' => new StdClass(),
74+
'withoutType' => 'hhh',
75+
],
76+
input_name:null,
77+
type_object:null,
78+
type_mixed_other: ['abc' => ['bbb' => ['ccc' => 'dddd'],['abc']],'aaa','bbb','ccc',''],
79+
abc:123
80+
);
81+
82+
expect($object)->toBeInstanceOf(TestFromSerialize::class)
83+
->and($object->type_string)->toBe('123')
84+
->and($object->type_object)->toBeInstanceOf(StdClass::class)
85+
->and($object->type_int)->toBe(11)
86+
->and($object->type_null)->toBe(123)
87+
->and($object->type_float)->toBe(0.01);
88+
});

0 commit comments

Comments
 (0)