-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractTransfer.php
More file actions
209 lines (168 loc) · 4.81 KB
/
AbstractTransfer.php
File metadata and controls
209 lines (168 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
declare(strict_types=1);
namespace Picamator\TransferObject\Transfer;
use Picamator\TransferObject\Transfer\Attribute\AttributeTrait;
use SplFixedArray;
use Traversable;
/**
* @api
*/
abstract class AbstractTransfer implements TransferInterface
{
use AttributeTrait {
getInitiatorAttribute as private;
getTransformerAttribute as private;
}
/**
* @var int<0, max>
*/
protected const int META_DATA_SIZE = 0;
/**
* @var array<string, int>
*/
protected const array META_DATA = [];
/**
* @var array<string, string>
*/
protected const array META_INITIATORS = [];
/**
* @var array<string, string>
*/
protected const array META_TRANSFORMERS = [];
/**
* @var \SplFixedArray<mixed>
*/
private SplFixedArray $_data;
/**
* @param array<string,mixed>|null $data
*
* @throws \Picamator\TransferObject\Transfer\Exception\DataAssertTransferException
*/
final public function __construct(?array $data = null)
{
$this->initData();
if ($data !== null && \count($data) > 0) {
$this->hydrateData($data);
}
}
/**
* @return array<string,mixed>
*/
final public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* @return array<string,SplFixedArray<mixed>>
*/
final public function __serialize(): array
{
return ['_data' => $this->_data];
}
/**
* @param array<string,\SplFixedArray<mixed>> $data
*/
final public function __unserialize(array $data): void
{
$this->_data = $data['_data'];
}
final public function count(): int
{
return static::META_DATA_SIZE;
}
final public function __debugInfo(): array
{
return $this->toArray();
}
final public function getIterator(): Traversable
{
foreach (static::META_DATA as $propertyName => $index) {
yield $propertyName => $this->_data[$index];
}
}
/**
* @throws \Picamator\TransferObject\Transfer\Exception\DataAssertTransferException
*/
final public function __clone(): void
{
/** @var \SplFixedArray<mixed> $data */
$data = \unserialize(\serialize($this->_data));
$this->_data = $data;
}
final public function toArray(): array
{
$data = [];
$metaData = static::META_DATA;
foreach (static::META_TRANSFORMERS as $propertyName => $constantName) {
$index = $metaData[$propertyName];
$value = $this->_data[$index];
if ($value === null) {
continue;
}
$data[$propertyName] = $this->getTransformerAttribute($constantName)->toArray($value);
unset($metaData[$propertyName]);
}
foreach ($metaData as $propertyName => $index) {
$data[$propertyName] = $this->_data[$index];
}
return $data;
}
final public function fromArray(array $data): static
{
$this->initData();
if (\count($data) > 0) {
$this->hydrateData($data);
}
return $this;
}
final protected function getData(int $index): mixed
{
return $this->_data[$index];
}
final protected function setData(int $index, mixed $value): void
{
$this->_data[$index] = $value;
}
/**
* @param array<string,mixed> $data
*/
private function hydrateData(array $data): void
{
$this->filterData($data);
if (\count($data) === 0) {
return;
}
foreach (static::META_TRANSFORMERS as $propertyName => $constantName) {
if (!isset($data[$propertyName])) {
continue;
}
$index = static::META_DATA[$propertyName];
$value = $this->getTransformerAttribute($constantName)->fromArray($data[$propertyName]);
$this->_data[$index] = $value;
unset($data[$propertyName]);
}
foreach ($data as $propertyName => $value) {
$this->$propertyName = $value;
}
}
/**
* @param array<string,mixed> $data
*/
private function filterData(array &$data): void
{
foreach ($data as $propertyName => $value) {
if ($value === null || !isset(static::META_DATA[$propertyName])) {
unset($data[$propertyName]);
}
}
}
private function initData(): void
{
$this->_data = new SplFixedArray(size: static::META_DATA_SIZE);
foreach (static::META_INITIATORS as $propertyName => $constantName) {
$index = static::META_DATA[$propertyName];
$value = $this->getInitiatorAttribute($constantName)->getInitialValue();
$this->_data[$index] = $value;
}
}
}