Total Complexity | 9 |
Total Lines | 76 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
10 | abstract class ValueObject implements ValueObjectInterface |
||
11 | { |
||
12 | /** @var mixed */ |
||
13 | protected $value; |
||
14 | |||
15 | /** |
||
16 | * ValueObject constructor. |
||
17 | * @param $value |
||
18 | * @throws \ReflectionException |
||
19 | */ |
||
20 | 41 | public function __construct($value) |
|
21 | { |
||
22 | 41 | $this->guard($value); |
|
23 | 35 | $this->value = $value; |
|
24 | 35 | } |
|
25 | |||
26 | /** |
||
27 | * @return mixed |
||
28 | */ |
||
29 | 32 | public function getValue() |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return string |
||
36 | */ |
||
37 | 2 | public function __toString(): string |
|
38 | { |
||
39 | 2 | if (is_scalar($this->value)) { |
|
40 | 1 | return (string)$this->value; |
|
41 | } |
||
42 | 1 | return (string)json_encode($this->value, 320); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param \DBUnt1tled\VO\VObjects\ValueObjectInterface $other |
||
47 | * @return bool |
||
48 | */ |
||
49 | 4 | public function equals(ValueObjectInterface $other): bool |
|
50 | { |
||
51 | 4 | if (get_class($this) !== get_class($other)) { |
|
52 | 1 | throw new InvalidVOArgumentException( |
|
53 | 1 | sprintf( |
|
54 | 1 | 'A Value Object of type %s can not be compared to another of type %s', |
|
55 | 1 | get_class($this), |
|
56 | 1 | get_class($other) |
|
57 | ), |
||
58 | [ |
||
59 | 1 | 'original' => $this, |
|
60 | 1 | 'destination' => $other |
|
61 | ] |
||
62 | ); |
||
63 | } |
||
64 | 3 | return $this->getValue() === $other->getValue(); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param mixed $value |
||
69 | * @param mixed ...$other |
||
70 | * @throws \ReflectionException |
||
71 | */ |
||
72 | 40 | public function guard($value, ...$other): void |
|
76 | } |
||
77 | 40 | } |
|
78 | |||
79 | /** |
||
80 | * @return \ReflectionClass |
||
81 | * @throws \ReflectionException |
||
82 | */ |
||
83 | 2 | protected function getReflection(): \ReflectionClass |
|
88 |