1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MichaelRubel\ValueObjects; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Support\Traits\Conditionable; |
9
|
|
|
use Illuminate\Support\Traits\Macroable; |
10
|
|
|
|
11
|
|
|
abstract class ValueObject implements Arrayable |
12
|
|
|
{ |
13
|
|
|
use Macroable, Conditionable; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Get the object value. |
17
|
|
|
* |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
|
|
abstract public function value(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Convenient method to create a value object statically. |
24
|
|
|
* |
25
|
|
|
* @param mixed $values |
26
|
|
|
* |
27
|
|
|
* @return static |
28
|
|
|
*/ |
29
|
8 |
|
public static function make(mixed ...$values): static |
30
|
|
|
{ |
31
|
8 |
|
return new static(...$values); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Convenient method to create a value object statically. |
36
|
|
|
* |
37
|
|
|
* @param mixed $values |
38
|
|
|
* |
39
|
|
|
* @return static |
40
|
|
|
*/ |
41
|
8 |
|
public static function from(mixed ...$values): static |
42
|
|
|
{ |
43
|
8 |
|
return static::make(...$values); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check if objects are instances of same class |
48
|
|
|
* and share the same properties and values. |
49
|
|
|
* |
50
|
|
|
* @param ValueObject $object |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
3 |
|
public function equals(ValueObject $object): bool |
55
|
|
|
{ |
56
|
3 |
|
return $this == $object; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Inversion for `equals` method. |
61
|
|
|
* |
62
|
|
|
* @param ValueObject $object |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
2 |
|
public function notEquals(ValueObject $object): bool |
67
|
|
|
{ |
68
|
2 |
|
return ! $this->equals($object); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get an array representation of the value object. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
5 |
|
public function toArray(): array |
77
|
|
|
{ |
78
|
5 |
|
return (array) $this->value(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get string representation of the value object. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
14 |
|
public function __toString(): string |
87
|
|
|
{ |
88
|
14 |
|
return (string) $this->value(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the internal property value. |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
8 |
|
public function __get(string $name): mixed |
99
|
|
|
{ |
100
|
8 |
|
return $this->{$name}; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Make sure value object is immutable. |
105
|
|
|
* |
106
|
|
|
* @param string $name |
107
|
|
|
* @param mixed $value |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
8 |
|
public function __set(string $name, mixed $value): void |
112
|
|
|
{ |
113
|
8 |
|
throw new \InvalidArgumentException('Value objects are immutable. You cannot modify properties, create a new object instead.'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|