File tree Expand file tree Collapse file tree 3 files changed +29
-1
lines changed
Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -24,4 +24,5 @@ PHP Value object for storing valid data.
2424 - touch (` setTouched ` , ` isTouched ` , ` getTouchedAll ` )
2525 - add interface ` ValueObjectInterface `
2626 - remove trait
27- - min request PHP 7.4
27+ - min request PHP 7.4
28+ - check null
Original file line number Diff line number Diff line change 33namespace OGSoft \ValueObjects ;
44
55use Carbon \Carbon ;
6+ use DateTime ;
67use Exception ;
78use OGSoft \ValueObjects \Exceptions \ValidatorException ;
89use OGSoft \ValueObjects \Interfaces \GlobalValidatorInterface ;
@@ -191,6 +192,11 @@ private static function transformBuildInType(ReflectionParameter $reflectionPara
191192 }
192193 }
193194
195+ // check null
196+ if (!$ reflectionParameter ->allowsNull () && empty ($ value )) {
197+ throw new Exception ("Wrong data type of property. Data can not be null " );
198+ }
199+
194200 // general
195201 return $ value ;
196202 }
Original file line number Diff line number Diff line change 33
44namespace Tests ;
55
6+ use OGSoft \ValueObjects \Exceptions \ValidatorException ;
67use PHPUnit \Framework \TestCase ;
78use Tests \ValueObjects \CarValueObject ;
89
@@ -43,5 +44,25 @@ public function testTest(): void
4344
4445 $ this ->assertTrue ($ car ->isTouched ("name " ));
4546 }
47+
48+ public function testNull ()
49+ {
50+ $ data = [
51+ "name " => "My car " ,
52+ // test null value
53+ "enginePower " => null ,
54+ "wheels " => [
55+ ["size " => 5 ],
56+ ["size " => null ],
57+ [],
58+ ["size " => 6 , "tire " => ["winter " => true ]],
59+ ]
60+ ];
61+
62+ $ car = new CarValueObject ();
63+ $ this ->expectException (ValidatorException::class);
64+ $ car ->init ($ data );
65+ $ this ->assertTrue (true );
66+ }
4667}
4768
You can’t perform that action at this time.
0 commit comments