-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathArrayHash.php
265 lines (224 loc) · 5.33 KB
/
ArrayHash.php
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/*
* Copyright 2024 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace LaravelJsonApi\Eloquent\Fields;
use Closure;
use LaravelJsonApi\Core\Json\Hash;
use LaravelJsonApi\Core\Support\Arr;
use LaravelJsonApi\Validation\Fields\IsValidated;
use LaravelJsonApi\Validation\Fields\ValidatedWithArrayKeys;
use LaravelJsonApi\Validation\Rules\JsonObject;
class ArrayHash extends Attribute implements IsValidated
{
use ValidatedWithArrayKeys;
/**
* @var Closure|null
*/
private ?Closure $keys = null;
/**
* @var int|null
*/
private ?int $sortValues = null;
/**
* @var int|null
*/
private ?int $sortKeys = null;
/**
* The JSON:API field case.
*
* @var string|null
*/
private ?string $fieldCase = null;
/**
* The database key-case.
*
* @var string|null
*/
private ?string $keyCase = null;
/**
* Whether an empty array is allowed as the value.
*
* @var bool
*/
private bool $allowEmpty = false;
/**
* Create an array attribute.
*
* @param string $fieldName
* @param string|null $column
* @return ArrayHash
*/
public static function make(string $fieldName, string $column = null): self
{
return new self($fieldName, $column);
}
/**
* Sort values when deserializing the array.
*
* @param int $flags
* @return $this
*/
public function sorted(int $flags = SORT_REGULAR): self
{
$this->sortKeys = null;
$this->sortValues = $flags;
return $this;
}
/**
* Sort values by their keys when deserializing the array.
*
* @param int $flags
* @return $this
*/
public function sortKeys(int $flags = SORT_REGULAR): self
{
$this->sortKeys = $flags;
$this->sortValues = null;
return $this;
}
/**
* Use camel-case fields when serializing to JSON.
*
* @return $this
*/
public function camelizeFields(): self
{
$this->fieldCase = 'camelize';
return $this;
}
/**
* Use camel-case fields when storing values in the database.
*
* @return $this
*/
public function camelizeKeys(): self
{
$this->keyCase = 'camelize';
return $this;
}
/**
* Use snake-case fields when serializing to JSON.
*
* @return $this
*/
public function snakeFields(): self
{
$this->fieldCase = 'snake';
return $this;
}
/**
* Use snake-case fields when storing values in the database.
*
* @return $this
*/
public function snakeKeys(): self
{
$this->keyCase = 'snake';
return $this;
}
/**
* Use underscore fields when serializing to JSON.
*
* @return $this
*/
public function underscoreFields(): self
{
$this->fieldCase = 'underscore';
return $this;
}
/**
* Use underscore keys when storing values in the database.
*
* @return $this
*/
public function underscoreKeys(): self
{
$this->keyCase = 'underscore';
return $this;
}
/**
* Use dash-case fields when serializing to JSON.
*
* @return $this
*/
public function dasherizeFields(): self
{
$this->fieldCase = 'dasherize';
return $this;
}
/**
* Use dash-case keys when storing values in the database.
*
* @return $this
*/
public function dasherizeKeys(): self
{
$this->keyCase = 'dasherize';
return $this;
}
/**
* Whether an empty array is allowed as the value.
*
* @param bool $allowEmpty
* @return self
*/
public function allowEmpty(bool $allowEmpty = true): self
{
$this->allowEmpty = $allowEmpty;
return $this;
}
/**
* @inheritDoc
*/
public function serialize(object $model)
{
$value = parent::serialize($model);
return Hash::cast($value)
->maybeSorted($this->sortValues)
->maybeSortKeys($this->sortKeys)
->useCase($this->fieldCase);
}
/**
* @inheritDoc
*/
protected function deserialize($value)
{
$value = parent::deserialize($value);
if ($value && $this->keys) {
$value = ($this->keys)($value);
}
if ($value === null) {
return null;
}
return Hash::cast($value)
->maybeSorted($this->sortValues)
->maybeSortKeys($this->sortKeys)
->useCase($this->keyCase)
->all();
}
/**
* @inheritDoc
*/
protected function assertValue($value): void
{
if (($value !== null && !is_array($value)) || (!empty($value) && !Arr::isAssoc($value))) {
throw new \UnexpectedValueException(sprintf(
'Expecting the value of attribute %s to be an associative array.',
$this->name()
));
}
}
/**
* @return array<string, mixed>
*/
protected function defaultRules(): array
{
return ['.' => (new JsonObject())->allowEmpty($this->allowEmpty)];
}
}