1
|
|
|
<?php |
2
|
|
|
namespace phootwork\collection; |
3
|
|
|
|
4
|
|
|
use phootwork\lang\Comparator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Abstract class for all list-like collections |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
abstract class AbstractList extends AbstractCollection { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Iterative reduction of this collection with the help of a callback function. The callback |
14
|
|
|
* function takes two parameters, the first is the carry, the second the current item, with this |
15
|
|
|
* signature: mixed callback(mixed $carry, mixed $item) |
16
|
|
|
* |
17
|
|
|
* @param callable $callback the callback function |
18
|
|
|
* @param mixed $fallback the default value, that will be returned when the list is empty |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public function reduce(callable $callback, $fallback = null) { |
22
|
|
|
return array_reduce($this->collection, $callback, $fallback); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sorts the collection. |
27
|
|
|
* |
28
|
|
|
* @param Comparator|callable $cmp |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function sort($cmp = null) { |
32
|
|
|
$this->doSort($this->collection, $cmp, 'usort', 'sort'); |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Reverses the order of all elements |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function reverse() { |
43
|
|
|
$this->collection = array_reverse($this->collection); |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sorts the collection in reverse order |
49
|
|
|
* |
50
|
|
|
* @see #sort |
51
|
|
|
* @see #reverse |
52
|
|
|
* @param Comparator|callable $cmp |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function reverseSort($cmp = null) { |
56
|
|
|
return $this->sort($cmp)->reverse(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Iterates the collection and calls the callback function with the current item as parameter |
61
|
|
|
* |
62
|
|
|
* @param callable $callback |
63
|
|
|
*/ |
64
|
|
|
public function each(callable $callback) { |
65
|
|
|
foreach ($this->collection as $item) { |
66
|
|
|
$callback($item); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Searches the collection with a given callback and returns the index for the first element if found. |
72
|
|
|
* |
73
|
|
|
* The callback function takes one or two parameters: |
74
|
|
|
* |
75
|
|
|
* function ($element [, $query]) {} |
76
|
|
|
* |
77
|
|
|
* The callback must return a boolean |
78
|
|
|
* |
79
|
|
|
* @param mixed $query OPTIONAL the provided query |
80
|
|
|
* @param callable $callback the callback function |
81
|
|
|
* @return int|null the index or null if it hasn't been found |
82
|
|
|
*/ |
83
|
|
|
public function findIndex() { |
84
|
|
|
if (func_num_args() == 1) { |
85
|
|
|
$callback = func_get_arg(0); |
86
|
|
|
} else { |
87
|
|
|
$query = func_get_arg(0); |
88
|
|
|
$callback = func_get_arg(1); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$index = func_num_args() == 1 ? $this->find($callback) : $this->find($query, $callback); |
92
|
|
|
if ($index !== null) { |
93
|
|
|
$index = $this->indexOf($index); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $index; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Searches the collection with a given callback and returns the index for the last element if found. |
101
|
|
|
* |
102
|
|
|
* The callback function takes one or two parameters: |
103
|
|
|
* |
104
|
|
|
* function ($element [, $query]) {} |
105
|
|
|
* |
106
|
|
|
* The callback must return a boolean |
107
|
|
|
* |
108
|
|
|
* @param mixed $query OPTIONAL the provided query |
109
|
|
|
* @param callable $callback the callback function |
110
|
|
|
* @return int|null the index or null if it hasn't been found |
111
|
|
|
*/ |
112
|
|
|
public function findLastIndex() { |
113
|
|
|
if (func_num_args() == 1) { |
114
|
|
|
$callback = func_get_arg(0); |
115
|
|
|
} else { |
116
|
|
|
$query = func_get_arg(0); |
117
|
|
|
$callback = func_get_arg(1); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$index = func_num_args() == 1 ? $this->findLast($callback) : $this->findLast($query, $callback); |
121
|
|
|
if ($index !== null) { |
122
|
|
|
$index = $this->indexOf($index); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $index; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|