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