1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Daikon\Entity\EntityType; |
4
|
|
|
|
5
|
|
|
use Daikon\Entity\Assert\Assertion; |
6
|
|
|
use Daikon\Entity\Entity\EntityInterface; |
7
|
|
|
use Daikon\Entity\Error\InvalidType; |
8
|
|
|
use Daikon\Entity\Error\MissingImplementation; |
9
|
|
|
use Daikon\Entity\ValueObject\ValueObjectInterface; |
10
|
|
|
|
11
|
|
|
class Attribute implements AttributeInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $name; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var EntityTypeInterface |
20
|
|
|
*/ |
21
|
|
|
private $entityType; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $valueImplementor; |
27
|
|
|
|
28
|
58 |
|
public static function define( |
29
|
|
|
string $name, |
30
|
|
|
$valueImplementor, |
31
|
|
|
EntityTypeInterface $entityType |
32
|
|
|
): AttributeInterface { |
33
|
58 |
|
if (!class_exists($valueImplementor)) { |
34
|
|
|
throw new MissingImplementation("Unable to load VO class $valueImplementor"); |
35
|
|
|
} |
36
|
58 |
|
if (!is_subclass_of($valueImplementor, ValueObjectInterface::class)) { |
37
|
|
|
throw new InvalidType("Given VO class $valueImplementor does not implement ".ValueObjectInterface::class); |
38
|
|
|
} |
39
|
58 |
|
return new static($name, $entityType, $valueImplementor); |
40
|
|
|
} |
41
|
|
|
|
42
|
32 |
|
public function makeValue($value = null, EntityInterface $parent = null): ValueObjectInterface |
43
|
|
|
{ |
44
|
32 |
|
if (is_object($value)) { |
45
|
|
|
Assertion::isInstanceOf($value, $this->valueImplementor); |
46
|
|
|
return $value; |
47
|
|
|
} |
48
|
32 |
|
return call_user_func([$this->valueImplementor, 'fromNative'], $value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getValueType(): string |
52
|
|
|
{ |
53
|
|
|
return $this->valueImplementor; |
54
|
|
|
} |
55
|
|
|
|
56
|
58 |
|
public function getName(): string |
57
|
|
|
{ |
58
|
58 |
|
return $this->name; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
public function getEntityType(): EntityTypeInterface |
62
|
|
|
{ |
63
|
3 |
|
return $this->entityType; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
public function getParent(): ?AttributeInterface |
67
|
|
|
{ |
68
|
2 |
|
return $this->getEntityType()->getParentAttribute(); |
69
|
|
|
} |
70
|
|
|
|
71
|
58 |
|
protected function __construct(string $name, EntityTypeInterface $entityType, string $valueImplementor) |
72
|
|
|
{ |
73
|
58 |
|
$this->name = $name; |
74
|
58 |
|
$this->valueImplementor = $valueImplementor; |
75
|
58 |
|
$this->entityType = $entityType; |
76
|
58 |
|
} |
77
|
|
|
} |
78
|
|
|
|