Skip to content

Commit ec3172e

Browse files
committed
Entity: fixed enumeration checking for nullable properties
1 parent 33d320c commit ec3172e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/LeanMapper/Entity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ protected function set($property, $value)
677677
if ($value !== null) {
678678
settype($value, $property->getType());
679679
}
680-
if ($property->containsEnumeration() and !$property->isValueFromEnum($value)) {
680+
if ($value !== null and $property->containsEnumeration() and !$property->isValueFromEnum($value)) {
681681
throw new InvalidValueException(
682682
"Given value is not from possible values enumeration in property '{$property->getName()}' in entity " . get_called_class() . '.'
683683
);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use PDO as DbLayer;
4+
use Tester\Assert;
5+
6+
require_once __DIR__ . '/../bootstrap.php';
7+
8+
//////////
9+
10+
/**
11+
* @property int $id
12+
* @property string|null $state m:enum(self::STATE_*)
13+
* @property string $finalState m:enum(self::STATE_*)
14+
*/
15+
class Author extends LeanMapper\Entity
16+
{
17+
18+
const STATE_ACTIVE = 'active';
19+
20+
const STATE_INACTIVE = 'inactive';
21+
22+
const STATE_DELETED = 'deleted';
23+
24+
}
25+
26+
27+
//////////
28+
29+
$author = new Author;
30+
31+
$author->state = Author::STATE_ACTIVE;
32+
Assert::equal(Author::STATE_ACTIVE, $author->state);
33+
34+
$author->state = NULL;
35+
Assert::equal(NULL, $author->state);
36+
37+
$author->finalState = Author::STATE_DELETED;
38+
Assert::equal(Author::STATE_DELETED, $author->finalState);
39+
40+
Assert::exception(function () use ($author) {
41+
42+
$author->finalState = NULL;
43+
44+
}, 'LeanMapper\Exception\InvalidValueException', "Property 'finalState' in entity Author cannot be null.");

0 commit comments

Comments
 (0)