Skip to content

Commit 2607492

Browse files
author
František Mazura
committed
check null
1 parent fecd28e commit 2607492

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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

src/AbstractValueObject.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace OGSoft\ValueObjects;
44

55
use Carbon\Carbon;
6+
use DateTime;
67
use Exception;
78
use OGSoft\ValueObjects\Exceptions\ValidatorException;
89
use 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
}

tests/ValueObjectTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Tests;
55

6+
use OGSoft\ValueObjects\Exceptions\ValidatorException;
67
use PHPUnit\Framework\TestCase;
78
use 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

0 commit comments

Comments
 (0)