AbstractCollection::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace phootwork\collection;
3
4
use phootwork\lang\Comparator;
5
6
/**
7
 * AbstractCollection providing implemention for the Collection interface.
8
 *
9
 * @author Thomas Gossmann
10
 */
11
abstract class AbstractCollection implements Collection {
12
13
	protected $collection = [];
14
15
	public function contains($element) {
16
		return in_array($element, $this->collection, true);
17
	}
18
19
	public function size() {
20
		return count($this->collection);
21
	}
22
23
	public function isEmpty() {
24
		return count($this->collection) == 0;
25
	}
26
27
	public function clear() {
28
		$this->collection = [];
29
	}
30
31
	public function toArray() {
32
		return $this->collection;
33
	}
34
35
	/**
36
	 * Applies the callback to the elements
37
	 *
38
	 * @param callable $callback the applied callback function
39
	 * @return static
40
	 */
41
	public function map(callable $callback) {
42
		return new static(array_map($callback, $this->collection));
43
	}
44
45
	/**
46
	 * Filters elements using a callback function
47
	 *
48
	 * @param callable $callback the filter function
49
	 * @return static
50
	 */
51
	public function filter(callable $callback) {
52
		return new static(array_filter($this->collection, $callback));
53
	}
54
55
	/**
56
	 * Tests whether all elements in the collection pass the test implemented by the provided function.
57
	 *
58
	 * Returns <code>true</code> for an empty collection.
59
	 *
60
	 * @param callable $callback
61
	 * @return boolean
62
	 */
63
	public function every(callable $callback) {
64
		$match = true;
65
		foreach ($this->collection as $element) {
66
			$match = $match && $callback($element);
67
		}
68
69
		return $match;
70
	}
71
72
	/**
73
	 * Tests whether at least one element in the collection passes the test implemented by the provided function.
74
	 *
75
	 * Returns <code>false</code> for an empty collection.
76
	 *
77
	 * @param callable $callback
78
	 * @return boolean
79
	 */
80
	public function some(callable $callback) {
81
		$match = false;
82
		foreach ($this->collection as $element) {
83
			$match = $match || $callback($element);
84
		}
85
86
		return $match;
87
	}
88
89
	/**
90
	 * Searches the collection for query using the callback function on each element
91
	 *
92
	 * The callback function takes one or two parameters:
93
	 *
94
	 *     function ($element [, $query]) {}
95
	 *
96
	 * The callback must return a boolean
97
	 *
98
	 * @param mixed $query (optional)
99
	 * @param callable $callback
100
	 * @return boolean
101
	 */
102
	public function search() {
103
		if (func_num_args() == 1) {
104
			$callback = func_get_arg(0);
105
		} else {
106
			$query = func_get_arg(0);
107
			$callback = func_get_arg(1);
108
		}
109
110
		foreach ($this->collection as $element) {
111
			$return = func_num_args() == 1 ? $callback($element) : $callback($element, $query);
112
113
			if ($return) {
114
				return true;
115
			}
116
		}
117
		return false;
118
	}
119
120
	/**
121
	 * Searches the collection with a given callback and returns the first element if found.
122
	 *
123
	 * The callback function takes one or two parameters:
124
	 *
125
	 *     function ($element [, $query]) {}
126
	 *
127
	 * The callback must return a boolean
128
	 *
129
	 * @param mixed $query OPTIONAL the provided query
130
	 * @param callable $callback the callback function
131
	 * @return mixed|null the found element or null if it hasn't been found
132
	 */
133
	public function find() {
134
		if (func_num_args() == 1) {
135
			$callback = func_get_arg(0);
136
		} else {
137
			$query = func_get_arg(0);
138
			$callback = func_get_arg(1);
139
		}
140
141
		foreach ($this->collection as $element) {
142
			$return = func_num_args() == 1 ? $callback($element) : $callback($element, $query);
143
144
			if ($return) {
145
				return $element;
146
			}
147
		}
148
149
		return null;
150
	}
151
152
	/**
153
	 * Searches the collection with a given callback and returns the last element if found.
154
	 *
155
	 * The callback function takes one or two parameters:
156
	 *
157
	 *     function ($element [, $query]) {}
158
	 *
159
	 * The callback must return a boolean
160
	 *
161
	 * @param mixed $query OPTIONAL the provided query
162
	 * @param callable $callback the callback function
163
	 * @return mixed|null the found element or null if it hasn't been found
164
	 */
165
	public function findLast() {
166
		if (func_num_args() == 1) {
167
			$callback = func_get_arg(0);
168
		} else {
169
			$query = func_get_arg(0);
170
			$callback = func_get_arg(1);
171
		}
172
173
		$reverse = array_reverse($this->collection, true);
174
		foreach ($reverse as $element) {
175
			$return = func_num_args() == 1 ? $callback($element) : $callback($element, $query);
176
177
			if ($return) {
178
				return $element;
179
			}
180
		}
181
182
		return null;
183
	}
184
185
	/**
186
	 * Searches the collection with a given callback and returns all matching elements.
187
	 *
188
	 * The callback function takes one or two parameters:
189
	 *
190
	 *     function ($element [, $query]) {}
191
	 *
192
	 * The callback must return a boolean
193
	 *
194
	 * @param mixed $query OPTIONAL the provided query
195
	 * @param callable $callback the callback function
196
	 * @return mixed|null the found element or null if it hasn't been found
197
	 */
198
	public function findAll() {
199
		if (func_num_args() == 1) {
200
			$callback = func_get_arg(0);
201
		} else {
202
			$query = func_get_arg(0);
203
			$callback = func_get_arg(1);
204
		}
205
206
		$collection = [];
207
		foreach ($this->collection as $k => $element) {
208
			$return = func_num_args() == 1 ? $callback($element) : $callback($element, $query);
209
210
			if ($return) {
211
				$collection[$k] = $element;
212
			}
213
		}
214
215
		return new static($collection);
216
	}
217
218
	/**
219
	 * Internal sort function
220
	 *
221
	 * @param array $collection the collection on which is operated on
222
	 * @param Comparator|callable|null $cmp the compare function
223
	 * @param callable $usort the sort function for user passed $cmd
224
	 * @param callable $sort the default sort function
225
	 */
226
	protected function doSort(&$collection, $cmp, callable $usort, callable $sort) {
227
		if (is_callable($cmp)) {
228
			$usort($collection, $cmp);
229
		} else if ($cmp instanceof Comparator) {
230
			$usort($collection, function($a, $b) use ($cmp) {
231
				return $cmp->compare($a, $b);
232
			});
233
		} else {
234
			$sort($collection);
235
		}
236
	}
237
238
	/**
239
	 * @internal
240
	 */
241
	public function rewind() {
242
		return reset($this->collection);
243
	}
244
245
	/**
246
	 * @internal
247
	 */
248
	public function current() {
249
		return current($this->collection);
250
	}
251
252
	/**
253
	 * @internal
254
	 */
255
	public function key() {
256
		return key($this->collection);
257
	}
258
259
	/**
260
	 * @internal
261
	 */
262
	public function next() {
263
		return next($this->collection);
264
	}
265
266
	/**
267
	 * @internal
268
	 */
269
	public function valid() {
270
		return key($this->collection) !== null;
271
	}
272
273
	/**
274
	 * @internal
275
	 */
276
	public function __clone() {
277
		return new static($this->collection);
278
	}
279
280
}