1 | <?php |
||
13 | abstract class Entity implements EntityInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var EntityTypeInterface |
||
17 | */ |
||
18 | private $type; |
||
19 | |||
20 | /** |
||
21 | * @var EntityInterface |
||
22 | */ |
||
23 | private $parent; |
||
24 | |||
25 | /** |
||
26 | * @var ValueObjectMap |
||
27 | */ |
||
28 | private $valueObjectMap; |
||
29 | |||
30 | /** |
||
31 | * @param ValuePathParser |
||
32 | */ |
||
33 | private $pathParser; |
||
34 | |||
35 | /** |
||
36 | * @param mixed[] array |
||
37 | * @return EntityInterface |
||
38 | */ |
||
39 | 31 | public static function fromNative($nativeState): EntityInterface |
|
40 | { |
||
41 | 31 | $entityType = $nativeState[self::TYPE_KEY]; |
|
42 | 31 | Assertion::isInstanceOf($entityType, EntityTypeInterface::class); |
|
43 | 31 | $parent = null; |
|
44 | 31 | if (isset($nativeState[self::PARENT_KEY])) { |
|
45 | 25 | $parent = $nativeState[self::PARENT_KEY]; |
|
46 | 25 | Assertion::isInstanceOf($parent, EntityInterface::class); |
|
47 | 25 | unset($nativeState[self::PARENT_KEY]); |
|
48 | } |
||
49 | 31 | return new static($entityType, $nativeState, $parent); |
|
50 | } |
||
51 | |||
52 | 7 | public function toNative(): array |
|
53 | { |
||
54 | 7 | $entityState = $this->valueObjectMap->toNative(); |
|
55 | 7 | $entityState[self::TYPE_KEY] = $this->getEntityType()->getName(); |
|
56 | 7 | return $entityState; |
|
57 | } |
||
58 | |||
59 | 1 | public function isSameAs(EntityInterface $entity): bool |
|
60 | { |
||
61 | 1 | Assertion::isInstanceOf($entity, static::class); |
|
62 | 1 | return $this->getIdentity()->equals($entity->getIdentity()); |
|
63 | } |
||
64 | |||
65 | 13 | public function withValue(string $attributeName, $value): EntityInterface |
|
66 | { |
||
67 | 13 | $copy = clone $this; |
|
68 | 13 | $copy->valueObjectMap = $this->valueObjectMap->withValue($attributeName, $value); |
|
69 | 13 | return $copy; |
|
70 | } |
||
71 | |||
72 | 1 | public function withValues(array $values): EntityInterface |
|
73 | { |
||
74 | 1 | $copy = clone $this; |
|
75 | 1 | $copy->valueObjectMap = $this->valueObjectMap->withValues($values); |
|
76 | 1 | return $copy; |
|
77 | } |
||
78 | |||
79 | 6 | public function has(string $attributeName): bool |
|
86 | |||
87 | 14 | public function get(string $valuePath): ?ValueObjectInterface |
|
88 | { |
||
89 | 14 | if (mb_strpos($valuePath, '.')) { |
|
90 | 3 | return $this->evaluatePath($valuePath); |
|
91 | } |
||
101 | |||
102 | 1 | public function getEntityRoot(): EntityInterface |
|
112 | |||
113 | 2 | public function getEntityParent(): ?EntityInterface |
|
117 | |||
118 | 31 | public function getEntityType(): EntityTypeInterface |
|
122 | |||
123 | 31 | protected function __construct(EntityTypeInterface $type, array $values = [], EntityInterface $parent = null) |
|
130 | |||
131 | 3 | private function evaluatePath($valuePath): ?ValueObjectInterface |
|
145 | } |
||
146 |