|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Valkyrja Framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Valkyrja\Type\Model\Trait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Trait Indexable. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Melech Mizrachi |
|
20
|
|
|
*/ |
|
21
|
|
|
trait Indexable |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Local cache for reversed indexes. |
|
25
|
|
|
* |
|
26
|
|
|
* <code> |
|
27
|
|
|
* static::class => [ |
|
28
|
|
|
* 0 => 'property_name', |
|
29
|
|
|
* 1 => 'other_property_name', |
|
30
|
|
|
* ] |
|
31
|
|
|
* </code> |
|
32
|
|
|
* |
|
33
|
|
|
* @var array<string, array<int, string>> |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static array $indexes = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
* |
|
40
|
|
|
* @return array<string, int> |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function getIndexes(): array |
|
43
|
|
|
{ |
|
44
|
|
|
return []; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritDoc |
|
49
|
|
|
* |
|
50
|
|
|
* @return array<int, string> |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function getReversedIndexes(): array |
|
53
|
|
|
{ |
|
54
|
|
|
return self::$indexes[static::class] |
|
55
|
|
|
??= array_flip(static::getIndexes()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
* |
|
61
|
|
|
* @param array<int, mixed> $properties The properties |
|
62
|
|
|
* |
|
63
|
|
|
* @return array<string, mixed> |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function getMappedArrayFromIndexedArray(array $properties = []): array |
|
66
|
|
|
{ |
|
67
|
|
|
$mappedProperties = []; |
|
68
|
|
|
$indexes = static::getIndexes(); |
|
69
|
|
|
$reversedIndexes = static::getReversedIndexes(); |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var int $index |
|
73
|
|
|
* @var mixed $value |
|
74
|
|
|
*/ |
|
75
|
|
|
foreach ($properties as $index => $value) { |
|
76
|
|
|
$name = $reversedIndexes[$index] ?? null; |
|
77
|
|
|
|
|
78
|
|
|
if ($name !== null) { |
|
79
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
80
|
|
|
$mappedProperties[$name] = $value; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Sort the array by index |
|
85
|
|
|
uksort($mappedProperties, static fn (string $a, string $b): int => $indexes[$a] <=> $indexes[$b]); |
|
86
|
|
|
|
|
87
|
|
|
return $mappedProperties; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritDoc |
|
92
|
|
|
* |
|
93
|
|
|
* @param array<string, mixed> $properties The properties |
|
94
|
|
|
* |
|
95
|
|
|
* @return array<int, mixed> |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function getIndexedArrayFromMappedArray(array $properties = []): array |
|
98
|
|
|
{ |
|
99
|
|
|
$indexedArray = []; |
|
100
|
|
|
$indexes = static::getIndexes(); |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @var string $name |
|
104
|
|
|
* @var mixed $value |
|
105
|
|
|
*/ |
|
106
|
|
|
foreach ($properties as $name => $value) { |
|
107
|
|
|
$index = $indexes[$name] ?? null; |
|
108
|
|
|
|
|
109
|
|
|
if ($index !== null) { |
|
110
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
111
|
|
|
$indexedArray[$index] = $value; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Sort the array by index |
|
116
|
|
|
ksort($indexedArray); |
|
117
|
|
|
|
|
118
|
|
|
return $indexedArray; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @inheritDoc |
|
123
|
|
|
* |
|
124
|
|
|
* @param array<int, mixed> $properties The properties |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function fromIndexedArray(array $properties): static |
|
127
|
|
|
{ |
|
128
|
|
|
/** @var static $model */ |
|
129
|
|
|
$model = static::fromArray(static::getMappedArrayFromIndexedArray($properties)); |
|
130
|
|
|
|
|
131
|
|
|
return $model; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @inheritDoc |
|
136
|
|
|
* |
|
137
|
|
|
* @param array<int, mixed> $properties The properties |
|
138
|
|
|
*/ |
|
139
|
|
|
public function updateIndexedProperties(array $properties): void |
|
140
|
|
|
{ |
|
141
|
|
|
$this->updateProperties(static::getMappedArrayFromIndexedArray($properties)); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @inheritDoc |
|
146
|
|
|
* |
|
147
|
|
|
* @param array<int, mixed> $properties The properties to modify |
|
148
|
|
|
*/ |
|
149
|
|
|
public function withIndexedProperties(array $properties): static |
|
150
|
|
|
{ |
|
151
|
|
|
/** @var static $model */ |
|
152
|
|
|
$model = $this->withProperties(static::getMappedArrayFromIndexedArray($properties)); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
return $model; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @inheritDoc |
|
159
|
|
|
* |
|
160
|
|
|
* @param string ...$properties [optional] An array of properties to return |
|
161
|
|
|
* |
|
162
|
|
|
* @return array<int, mixed> |
|
163
|
|
|
*/ |
|
164
|
|
|
public function asIndexedArray(string ...$properties): array |
|
165
|
|
|
{ |
|
166
|
|
|
return static::getIndexedArrayFromMappedArray($this->asArray(...$properties)); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @inheritDoc |
|
171
|
|
|
* |
|
172
|
|
|
* @return array<int, mixed> |
|
173
|
|
|
*/ |
|
174
|
|
|
public function asChangedIndexedArray(): array |
|
175
|
|
|
{ |
|
176
|
|
|
return static::getIndexedArrayFromMappedArray($this->asChangedArray()); |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @inheritDoc |
|
181
|
|
|
* |
|
182
|
|
|
* @return array<int, mixed> |
|
183
|
|
|
*/ |
|
184
|
|
|
public function asOriginalIndexedArray(): array |
|
185
|
|
|
{ |
|
186
|
|
|
return static::getIndexedArrayFromMappedArray($this->asOriginalArray()); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|