1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Dto\Traits; |
4
|
|
|
|
5
|
|
|
use Cerbero\Dto\Manipulators\ArrayConverter; |
6
|
|
|
|
7
|
|
|
use const Cerbero\Dto\CAMEL_CASE_ARRAY; |
|
|
|
|
8
|
|
|
use const Cerbero\Dto\MUTABLE; |
|
|
|
|
9
|
|
|
use const Cerbero\Dto\NONE; |
|
|
|
|
10
|
|
|
use const Cerbero\Dto\PARTIAL; |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait to manipulate a DTO data. |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
trait ManipulatesData |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Merge the given data in the DTO |
20
|
|
|
* |
21
|
|
|
* @param iterable $data |
22
|
|
|
* @param int $flags |
23
|
|
|
* @return self |
24
|
|
|
*/ |
25
|
3 |
|
public function merge(iterable $data, int $flags = NONE): self |
26
|
|
|
{ |
27
|
3 |
|
$mergedFlags = $this->getFlags() | $flags; |
|
|
|
|
28
|
3 |
|
$snakeCase = !($mergedFlags & CAMEL_CASE_ARRAY); |
29
|
3 |
|
$replacements = ArrayConverter::instance()->convert($data, $snakeCase); |
30
|
3 |
|
$mergedData = array_replace_recursive($this->toArray(), $replacements); |
|
|
|
|
31
|
|
|
|
32
|
3 |
|
if (!($this->getFlags() & MUTABLE)) { |
33
|
2 |
|
return new static($mergedData, $mergedFlags); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
$this->flags = $mergedFlags; |
|
|
|
|
37
|
1 |
|
$this->propertiesMap = $this->mapData($mergedData); |
|
|
|
|
38
|
|
|
|
39
|
1 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Retrieve the DTO including only the given properties |
44
|
|
|
* |
45
|
|
|
* @param array $properties |
46
|
|
|
* @param int $flags |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
4 |
|
public function only(array $properties, int $flags = NONE): self |
50
|
|
|
{ |
51
|
4 |
|
$data = []; |
52
|
4 |
|
$isMutable = $this->getFlags() & MUTABLE; |
53
|
4 |
|
$mergedFlags = $this->getFlags() | $flags | PARTIAL; |
54
|
|
|
|
55
|
4 |
|
foreach ($this->getPropertiesMap() as $name => $property) { |
|
|
|
|
56
|
4 |
|
if (in_array($name, $properties) && !$isMutable) { |
57
|
2 |
|
$data[$name] = $property->value(); |
58
|
4 |
|
} elseif (!in_array($name, $properties) && $isMutable) { |
59
|
2 |
|
unset($this->propertiesMap[$name]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
if ($isMutable) { |
64
|
2 |
|
$this->flags = $mergedFlags; |
|
|
|
|
65
|
2 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
return new static($data, $mergedFlags); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieve the DTO excluding the given properties |
73
|
|
|
* |
74
|
|
|
* @param array $properties |
75
|
|
|
* @param int $flags |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
2 |
|
public function except(array $properties, int $flags = NONE): self |
79
|
|
|
{ |
80
|
2 |
|
$propertiesToKeep = array_diff($this->getPropertyNames(), $properties); |
|
|
|
|
81
|
|
|
|
82
|
2 |
|
return $this->only($propertiesToKeep, $flags); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Make the DTO mutable while calling the given callback |
87
|
|
|
* |
88
|
|
|
* @param callable $callback |
89
|
|
|
* @return self |
90
|
|
|
*/ |
91
|
2 |
|
public function mutate(callable $callback): self |
92
|
|
|
{ |
93
|
2 |
|
$wasMutable = $this->getFlags() & MUTABLE; |
94
|
2 |
|
$this->flags |= MUTABLE; |
95
|
|
|
|
96
|
2 |
|
$callback($this); |
97
|
|
|
|
98
|
2 |
|
if (!$wasMutable) { |
99
|
1 |
|
$this->flags ^= MUTABLE; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|