|
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
|
|
|
* Retrieve the DTO as an array |
|
22
|
|
|
* |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
54 |
|
public function toArray(): array |
|
26
|
|
|
{ |
|
27
|
54 |
|
$data = []; |
|
28
|
|
|
|
|
29
|
54 |
|
foreach ($this->getPropertiesMap() as $name => $property) { |
|
|
|
|
|
|
30
|
54 |
|
$key = strtolower(preg_replace(ArrayConverter::RE_SNAKE_CASE, '_', $name)); |
|
31
|
54 |
|
$data[$key] = ArrayConverter::instance()->convert($property->value(), true); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
54 |
|
return $data; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Retrieve the DTO as an iterator |
|
39
|
|
|
* |
|
40
|
|
|
* @return Traversable |
|
41
|
|
|
*/ |
|
42
|
15 |
|
public function getIterator(): Traversable |
|
43
|
|
|
{ |
|
44
|
15 |
|
return new ArrayIterator($this->toArray(), ArrayIterator::ARRAY_AS_PROPS); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Determine whether a given property has a value |
|
49
|
|
|
* |
|
50
|
|
|
* @param mixed $property |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
18 |
|
public function offsetExists($property): bool |
|
54
|
|
|
{ |
|
55
|
18 |
|
return $this->has($property); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Retrieve the given property value |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $property |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
* @throws UnknownDtoPropertyException |
|
64
|
|
|
*/ |
|
65
|
33 |
|
public function &offsetGet($property) |
|
66
|
|
|
{ |
|
67
|
33 |
|
$value = $this->get($property); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
27 |
|
return $value; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set the given property to the provided value |
|
74
|
|
|
* |
|
75
|
|
|
* @param mixed $property |
|
76
|
|
|
* @param mixed $value |
|
77
|
|
|
* @return void |
|
78
|
|
|
* @throws ImmutableDtoException |
|
79
|
|
|
* @throws UnknownDtoPropertyException |
|
80
|
|
|
*/ |
|
81
|
12 |
|
public function offsetSet($property, $value): void |
|
82
|
|
|
{ |
|
83
|
12 |
|
if (!($this->getFlags() & MUTABLE)) { |
|
|
|
|
|
|
84
|
6 |
|
throw new ImmutableDtoException(static::class); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
$this->set($property, $value); |
|
|
|
|
|
|
88
|
6 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set the given property to the provided value |
|
92
|
|
|
* |
|
93
|
|
|
* @param mixed $property |
|
94
|
|
|
* @return void |
|
95
|
|
|
* @throws ImmutableDtoException |
|
96
|
|
|
* @throws UnsetDtoPropertyException |
|
97
|
|
|
* @throws UnknownDtoPropertyException |
|
98
|
|
|
*/ |
|
99
|
9 |
|
public function offsetUnset($property): void |
|
100
|
|
|
{ |
|
101
|
9 |
|
if (!($this->getFlags() & MUTABLE)) { |
|
102
|
3 |
|
throw new ImmutableDtoException(static::class); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
6 |
|
$this->unset($property); |
|
|
|
|
|
|
106
|
3 |
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|