1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Waredesk; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
use Countable; |
7
|
|
|
use JsonSerializable; |
8
|
|
|
use ArrayAccess; |
9
|
|
|
|
10
|
|
|
abstract class Collection implements Iterator, Countable, ArrayAccess, JsonSerializable |
11
|
|
|
{ |
12
|
|
|
protected $items; |
13
|
|
|
protected $key; |
14
|
|
|
|
15
|
12 |
|
public function __construct(array $items = []) |
16
|
|
|
{ |
17
|
12 |
|
$this->items = $items; |
18
|
12 |
|
} |
19
|
|
|
|
20
|
1 |
|
public function __clone() |
21
|
|
|
{ |
22
|
1 |
|
foreach ($this->items as $key => $item) { |
23
|
1 |
|
$this->items[$key] = clone $item; |
24
|
|
|
} |
25
|
1 |
|
} |
26
|
|
|
|
27
|
4 |
|
public function reset(): void |
28
|
|
|
{ |
29
|
4 |
|
$this->items = []; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
|
|
public function replace(array $items = []): void |
33
|
|
|
{ |
34
|
|
|
$this->items = $items; |
35
|
|
|
} |
36
|
|
|
|
37
|
12 |
|
public function add($item): void |
38
|
|
|
{ |
39
|
12 |
|
$this->items[] = $item; |
40
|
12 |
|
} |
41
|
|
|
|
42
|
11 |
|
public function first() |
43
|
|
|
{ |
44
|
11 |
|
return isset($this->items[0]) ? $this->items[0] : null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function toArray(): array |
48
|
|
|
{ |
49
|
|
|
return $this->items; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function jsonSerialize(): array |
53
|
|
|
{ |
54
|
6 |
|
return array_map(function (JsonSerializable $item) { |
55
|
5 |
|
return $item->jsonSerialize(); |
56
|
6 |
|
}, $this->items); |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
public function count(): int |
60
|
|
|
{ |
61
|
7 |
|
return count($this->items); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
public function current() |
65
|
|
|
{ |
66
|
2 |
|
return current($this->items); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
public function next() |
70
|
|
|
{ |
71
|
2 |
|
return next($this->items); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function key(): int |
75
|
|
|
{ |
76
|
2 |
|
return key($this->items); |
77
|
|
|
} |
78
|
|
|
|
79
|
11 |
|
public function valid(): bool |
80
|
|
|
{ |
81
|
11 |
|
$key = key($this->items); |
82
|
11 |
|
return ($key !== null && $key !== false); |
83
|
|
|
} |
84
|
|
|
|
85
|
11 |
|
public function rewind(): void |
86
|
|
|
{ |
87
|
11 |
|
reset($this->items); |
88
|
11 |
|
} |
89
|
|
|
|
90
|
|
|
public function offsetExists($offset): bool |
91
|
|
|
{ |
92
|
|
|
return array_key_exists($offset, $this->items); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function offsetGet($offset) |
96
|
|
|
{ |
97
|
|
|
return $this->items[$offset]; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function offsetSet($offset, $value): void |
101
|
|
|
{ |
102
|
1 |
|
$this->items[$offset] = $value; |
103
|
1 |
|
} |
104
|
|
|
|
105
|
1 |
|
public function offsetUnset($offset): void |
106
|
|
|
{ |
107
|
1 |
|
unset($this->items[$offset]); |
108
|
1 |
|
} |
109
|
|
|
} |
110
|
|
|
|