|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JsonFieldCast\Json; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @psalm-suppress MissingTemplateParam |
|
9
|
|
|
*/ |
|
10
|
|
|
class ArrayOfJsonObjectsField implements \JsonSerializable, \ArrayAccess, \Iterator, \Countable |
|
11
|
|
|
{ |
|
12
|
|
|
protected Model $model; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* JsonObject[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected array $data; |
|
18
|
|
|
|
|
19
|
|
|
protected int $iteratorPosition = 0; |
|
20
|
|
|
|
|
21
|
8 |
|
public function __construct(Model $model, array $data = []) |
|
22
|
|
|
{ |
|
23
|
8 |
|
$this->iteratorPosition = 0; |
|
24
|
8 |
|
$this->data = array_values(array_filter(array_map(function ($item) { |
|
25
|
6 |
|
if ($item instanceof JsonObject) { |
|
26
|
|
|
return $item; |
|
27
|
|
|
} |
|
28
|
6 |
|
if (is_array($item)) { |
|
29
|
6 |
|
return new JsonObject($item); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
6 |
|
return null; |
|
33
|
8 |
|
}, $data))); |
|
34
|
8 |
|
$this->model = $model; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function offsetExists(mixed $offset): bool |
|
38
|
|
|
{ |
|
39
|
1 |
|
return isset($this->data[$offset]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
5 |
|
public function offsetGet(mixed $offset): mixed |
|
43
|
|
|
{ |
|
44
|
5 |
|
return isset($this->data[$offset]) ? $this->data[$offset] : null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function offsetSet(mixed $offset, mixed $value): void |
|
48
|
|
|
{ |
|
49
|
2 |
|
if (is_null($offset)) { |
|
50
|
2 |
|
$this->data[] = $value; |
|
51
|
|
|
} else { |
|
52
|
1 |
|
$this->data[$offset] = $value; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function offsetUnset(mixed $offset): void |
|
57
|
|
|
{ |
|
58
|
1 |
|
unset($this->data[$offset]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
public function count(): int |
|
62
|
|
|
{ |
|
63
|
6 |
|
return count($this->data); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
public function current(): mixed |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->data[$this->iteratorPosition]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
public function next(): void |
|
72
|
|
|
{ |
|
73
|
2 |
|
++$this->iteratorPosition; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
public function key(): mixed |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->iteratorPosition; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public function valid(): bool |
|
82
|
|
|
{ |
|
83
|
2 |
|
return isset($this->data[$this->iteratorPosition]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
public function rewind(): void |
|
87
|
|
|
{ |
|
88
|
2 |
|
$this->iteratorPosition = 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
8 |
|
public function jsonSerialize(): mixed |
|
92
|
|
|
{ |
|
93
|
8 |
|
return array_values(array_filter(array_map(function ($item) { |
|
94
|
6 |
|
if ($item instanceof JsonObject) { |
|
95
|
6 |
|
return $item->jsonSerialize(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return null; |
|
99
|
8 |
|
}, $this->data))); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|