Skip to content

Commit efb64ad

Browse files
committed
add validate
1 parent 53ceacd commit efb64ad

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

benchmarks/SerializeBench.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function setupObject(): void
8383
Revs(500),
8484
Iterations(5),
8585
BeforeMethods([ 'setupObjectCreation']),
86-
Assert('mode(variant.time.avg) < 120 microseconds +/- 5%')
86+
Assert('mode(variant.time.avg) < 130 microseconds +/- 5%')
8787
]
8888
public function benchObjectCreation(): void
8989
{

src/Resolvers/InputResolver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(
2626
/**
2727
* @throws ReflectionException
2828
*/
29-
public function resolve(ChooseSerializeContext $chooseContext, GroupDataCollection $groupCollection, array $payload)
29+
public function resolve(ChooseSerializeContext $chooseContext, GroupDataCollection $groupCollection, array $payload): object
3030
{
3131
$reflectionClass = $this->reflectionClassInstanceManager->get($groupCollection->getClassName());
3232
$object = $reflectionClass->newInstanceWithoutConstructor();
@@ -72,6 +72,11 @@ classInstance: $object,
7272
$this->inputConstructCast->resolve($groupCollection->getConstructProperties(), $object, $constructInputs);
7373
}
7474

75+
// validate execution
76+
if(method_exists($object,'validate')){
77+
$object->validate();
78+
}
79+
7580
return $object;
7681

7782
}

src/Serialize.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public static function faker(): static
8484
return $instance;
8585
}
8686

87+
public function validate(): void
88+
{
89+
90+
}
91+
8792
public function __debugInfo()
8893
{
8994
$res = get_object_vars($this);

tests/Serialize/From/FromSerializeTest.php

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ public function __construct(
3030
$this->type_null = $abc;
3131
}
3232
}
33+
34+
class TestConstructFromSerialize extends Serialize
35+
{
36+
public function __construct(
37+
public string $type_string,
38+
public object $type_object,
39+
public int $type_int,
40+
public int $type_null,
41+
public float $type_float,
42+
) {
43+
}
44+
}
45+
46+
class TestConstructValidationFromSerialize extends Serialize
47+
{
48+
49+
public string $type_string;
50+
51+
public function validate():void
52+
{
53+
if($this->type_string !== '123'){
54+
throw new Exception('type_string is not 123');
55+
}
56+
57+
$this->type_string = '234';
58+
59+
}
60+
}
3361
});
3462

3563
it('test parse Serialize class', function () {
@@ -64,25 +92,32 @@ public function __construct(
6492

6593
it('test parse construct Serialize class', function () {
6694

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
95+
$object = TestConstructFromSerialize::from(
96+
type_string: 123,
97+
type_object: null,
98+
type_int: '123',
99+
type_null: null,
100+
type_float: '0.01',
80101
);
81102

82-
expect($object)->toBeInstanceOf(TestFromSerialize::class)
103+
expect($object)->toBeInstanceOf(TestConstructFromSerialize::class)
83104
->and($object->type_string)->toBe('123')
84105
->and($object->type_object)->toBeInstanceOf(StdClass::class)
85-
->and($object->type_int)->toBe(11)
86-
->and($object->type_null)->toBe(123)
106+
->and($object->type_int)->toBe(123)
107+
->and($object->type_null)->toBe(0)
87108
->and($object->type_float)->toBe(0.01);
88109
});
110+
111+
it('throws exception when type_string is not 123', function () {
112+
expect(function () {
113+
TestConstructValidationFromSerialize::from(type_string: 111);
114+
})->toThrow(Exception::class, 'type_string is not 123');
115+
});
116+
117+
it('creates object successfully when type_string is 123', function () {
118+
$object = TestConstructValidationFromSerialize::from(type_string: 123);
119+
120+
expect($object)
121+
->toBeInstanceOf(TestConstructValidationFromSerialize::class)
122+
->and($object->type_string)->toBe('234');
123+
});

0 commit comments

Comments
 (0)