|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\Dto\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayIterator; |
|
6
|
|
|
use Cerbero\Dto\Exceptions\ImmutableDtoException; |
|
7
|
|
|
use Cerbero\Dto\Exceptions\UnknownDtoPropertyException; |
|
8
|
|
|
use Cerbero\Dto\Exceptions\UnsetDtoPropertyException; |
|
9
|
|
|
use Cerbero\Dto\Manipulators\ArrayConverter; |
|
10
|
|
|
use Traversable; |
|
11
|
|
|
|
|
12
|
|
|
use const Cerbero\Dto\MUTABLE; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait to turn a DTO into an array. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
trait TurnsIntoArray |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The array converter. |
|
22
|
|
|
* |
|
23
|
|
|
* @var ArrayConverter |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $arrayConverter; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Retrieve the array converter |
|
29
|
|
|
* |
|
30
|
|
|
* @return ArrayConverter |
|
31
|
|
|
*/ |
|
32
|
39 |
|
public static function getArrayConverter(): ArrayConverter |
|
33
|
|
|
{ |
|
34
|
39 |
|
return static::$arrayConverter ?: ArrayConverter::instance(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Set the given array converter |
|
39
|
|
|
* |
|
40
|
|
|
* @param ArrayConverter |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public static function setArrayConverter(ArrayConverter $converter): void |
|
44
|
|
|
{ |
|
45
|
3 |
|
static::$arrayConverter = $converter; |
|
46
|
3 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Retrieve the DTO as an array |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
36 |
|
public function toArray(): array |
|
54
|
|
|
{ |
|
55
|
36 |
|
$data = []; |
|
56
|
|
|
|
|
57
|
36 |
|
foreach ($this->getPropertiesMap() as $name => $property) { |
|
|
|
|
|
|
58
|
36 |
|
$data[$name] = static::getArrayConverter()->convert($property->value()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
36 |
|
return $data; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Retrieve the DTO as an iterator |
|
66
|
|
|
* |
|
67
|
|
|
* @return Traversable |
|
68
|
|
|
*/ |
|
69
|
12 |
|
public function getIterator(): Traversable |
|
70
|
|
|
{ |
|
71
|
12 |
|
return new ArrayIterator($this->toArray(), ArrayIterator::ARRAY_AS_PROPS); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Determine whether a given property has a value |
|
76
|
|
|
* |
|
77
|
|
|
* @param mixed $property |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
18 |
|
public function offsetExists($property): bool |
|
81
|
|
|
{ |
|
82
|
18 |
|
return $this->has($property); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Retrieve the given property value |
|
87
|
|
|
* |
|
88
|
|
|
* @param mixed $property |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
* @throws UnknownDtoPropertyException |
|
91
|
|
|
*/ |
|
92
|
33 |
|
public function &offsetGet($property) |
|
93
|
|
|
{ |
|
94
|
33 |
|
$value = $this->get($property); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
27 |
|
return $value; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set the given property to the provided value |
|
101
|
|
|
* |
|
102
|
|
|
* @param mixed $property |
|
103
|
|
|
* @param mixed $value |
|
104
|
|
|
* @return void |
|
105
|
|
|
* @throws ImmutableDtoException |
|
106
|
|
|
* @throws UnknownDtoPropertyException |
|
107
|
|
|
*/ |
|
108
|
12 |
|
public function offsetSet($property, $value): void |
|
109
|
|
|
{ |
|
110
|
12 |
|
if (!($this->getFlags() & MUTABLE)) { |
|
|
|
|
|
|
111
|
6 |
|
throw new ImmutableDtoException(static::class); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
6 |
|
$this->set($property, $value); |
|
|
|
|
|
|
115
|
6 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set the given property to the provided value |
|
119
|
|
|
* |
|
120
|
|
|
* @param mixed $property |
|
121
|
|
|
* @return void |
|
122
|
|
|
* @throws ImmutableDtoException |
|
123
|
|
|
* @throws UnsetDtoPropertyException |
|
124
|
|
|
* @throws UnknownDtoPropertyException |
|
125
|
|
|
*/ |
|
126
|
9 |
|
public function offsetUnset($property): void |
|
127
|
|
|
{ |
|
128
|
9 |
|
if (!($this->getFlags() & MUTABLE)) { |
|
129
|
3 |
|
throw new ImmutableDtoException(static::class); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
6 |
|
$this->unset($property); |
|
|
|
|
|
|
133
|
3 |
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|