-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcollection.php
More file actions
158 lines (140 loc) · 2.91 KB
/
collection.php
File metadata and controls
158 lines (140 loc) · 2.91 KB
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
<?php
/**
* @package wp-content-aware-engine
* @author Joachim Jensen <joachim@dev.institute>
* @license GPLv3
* @copyright 2023 by Joachim Jensen
*/
class WPCACollection implements IteratorAggregate, Countable
{
/** @var array */
private $items;
/**
* @param array $items
*/
public function __construct($items = [])
{
$this->items = $items;
}
/**
* @param mixed $value
* @return $this
*/
public function add($value)
{
//backwards compat with $value,$key signature
$args = func_get_args();
if (count($args) === 2) {
list($value2, $key) = $args;
if (!$this->has($key)) {
$this->put($key, $value2);
}
return $this;
}
$this->items[] = $value;
return $this;
}
/**
* @param string $key
* @param mixed $value
* @return $this
*/
public function put($key, $value)
{
$this->items[$key] = $value;
return $this;
}
public function set($value, $key)
{
_deprecated_function(__METHOD__, '2.0');
$this->put($key, $value);
}
/**
* @param string $key
* @return $this
*/
public function remove($key)
{
unset($this->items[$key]);
return $this;
}
/**
* @return mixed|null
*/
public function pop()
{
return array_pop($this->items);
}
/**
* @param string $key
*
* @return bool
*/
public function has($key)
{
return isset($this->items[$key]);
}
/**
* @param string $key
* @param mixed|null $default_value
*
* @return mixed|null
*/
public function get($key, $default_value = null)
{
return $this->has($key) ? $this->items[$key] : $default_value;
}
/**
* @return array
*/
public function all()
{
return $this->items;
}
public function get_all()
{
_deprecated_function(__METHOD__, '2.0');
return $this->all();
}
public function set_all($items)
{
_deprecated_function(__METHOD__, '2.0');
foreach ($items as $item) {
$this->add($item);
}
}
/**
* @param callable $callback
* @return static
*/
public function filter($callback)
{
if (!is_callable($callback)) {
return $this;
}
return new static(array_filter($this->items, $callback, ARRAY_FILTER_USE_BOTH));
}
/**
* @inheritDoc
*/
#[ReturnTypeWillChange]
public function count()
{
return count($this->items);
}
/**
* @return bool
*/
public function is_empty()
{
return empty($this->items);
}
/**
* @inheritDoc
*/
#[ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->items);
}
}