|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sergiors\Functional; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Sérgio Rafael Siqueira <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
class Collection implements \ArrayAccess, \IteratorAggregate, \Countable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
private $xs; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param array $xs |
|
17
|
|
|
*/ |
|
18
|
6 |
|
public function __construct(array $xs) |
|
19
|
|
|
{ |
|
20
|
6 |
|
$this->xs = $xs; |
|
21
|
6 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param array $xs |
|
25
|
|
|
* |
|
26
|
|
|
* @return Collection |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function concat($xs) |
|
29
|
|
|
{ |
|
30
|
1 |
|
return new self(concat($this->xs, $xs)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param callable $fn |
|
35
|
|
|
* |
|
36
|
|
|
* @return Collection |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function filter(callable $fn) |
|
39
|
|
|
{ |
|
40
|
1 |
|
return new self(filter($fn, $this->xs)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param callable $fn |
|
45
|
|
|
* |
|
46
|
|
|
* @return Collection |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function map(callable $fn) |
|
49
|
|
|
{ |
|
50
|
1 |
|
return new self(map($fn, $this->xs)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param callable $fn |
|
55
|
|
|
* |
|
56
|
|
|
* @return Collection |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function each(callable $fn) |
|
59
|
|
|
{ |
|
60
|
1 |
|
return new self(each($fn, $this->xs)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param mixed $x |
|
65
|
|
|
* |
|
66
|
|
|
* @return Collection |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function prepend($x) |
|
69
|
|
|
{ |
|
70
|
1 |
|
return new self(prepend($x, $this->xs)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param mixed $x |
|
75
|
|
|
* |
|
76
|
|
|
* @return Collection |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function append($x) |
|
79
|
|
|
{ |
|
80
|
1 |
|
return new self(append($x, $this->xs)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return Collection |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function sort() |
|
87
|
|
|
{ |
|
88
|
1 |
|
return new self(sort($this->xs)); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param callable $fn |
|
93
|
|
|
* @param mixed|null $initial |
|
94
|
|
|
* |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function reduce(callable $fn, $initial = null) |
|
98
|
|
|
{ |
|
99
|
1 |
|
return reduce($fn, $this->xs, $initial); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return int |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function count() |
|
106
|
|
|
{ |
|
107
|
1 |
|
return count($this->xs); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return \ArrayIterator |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function getIterator() |
|
114
|
|
|
{ |
|
115
|
1 |
|
return new \ArrayIterator($this->xs); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
3 |
|
public function toArray() |
|
122
|
|
|
{ |
|
123
|
3 |
|
return $this->xs; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param mixed $offset |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function offsetExists($offset) |
|
132
|
|
|
{ |
|
133
|
1 |
|
return has($offset, $this->xs); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param mixed $offset |
|
138
|
|
|
* |
|
139
|
|
|
* @return mixed |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function offsetGet($offset) |
|
142
|
|
|
{ |
|
143
|
1 |
|
return get($this->xs, $offset, null); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param mixed $offset |
|
148
|
|
|
* |
|
149
|
|
|
* @param mixed $x |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function offsetSet($offset, $x) |
|
152
|
|
|
{ |
|
153
|
1 |
|
if (null === $offset) { |
|
154
|
1 |
|
$this->xs[] = $x; |
|
155
|
1 |
|
return; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
$this->xs[$offset] = $x; |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param mixed $offset |
|
163
|
|
|
*/ |
|
164
|
1 |
|
public function offsetUnset($offset) |
|
165
|
|
|
{ |
|
166
|
1 |
|
if ($this->offsetExists($offset)) { |
|
167
|
1 |
|
unset($this->xs[$offset]); |
|
168
|
1 |
|
} |
|
169
|
1 |
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: