|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nozavroni/Collections |
|
4
|
|
|
* Just another collections library for PHP5.6+. |
|
5
|
|
|
* @version {version} |
|
6
|
|
|
* @copyright Copyright (c) 2017 Luke Visinoni <[email protected]> |
|
7
|
|
|
* @author Luke Visinoni <[email protected]> |
|
8
|
|
|
* @license https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Noz\Collection; |
|
11
|
|
|
|
|
12
|
|
|
use BadMethodCallException; |
|
13
|
|
|
|
|
14
|
|
|
use Countable; |
|
15
|
|
|
use Traversable; |
|
16
|
|
|
use SplFixedArray; |
|
17
|
|
|
|
|
18
|
|
|
use Noz\Contracts\Structure\Sequenceable; |
|
19
|
|
|
use Noz\Contracts\Immutable; |
|
20
|
|
|
use Noz\Contracts\Arrayable; |
|
21
|
|
|
use Noz\Contracts\Invokable; |
|
22
|
|
|
|
|
23
|
|
|
use Noz\Traits\IsImmutable; |
|
24
|
|
|
|
|
25
|
|
|
use function Noz\to_array; |
|
26
|
|
|
use function Noz\is_traversable; |
|
27
|
|
|
|
|
28
|
|
|
class Sequence implements |
|
29
|
|
|
Sequenceable, |
|
30
|
|
|
Immutable, |
|
31
|
|
|
Countable, |
|
32
|
|
|
Arrayable, |
|
33
|
|
|
Invokable |
|
34
|
|
|
{ |
|
35
|
|
|
use IsImmutable; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Fixed-size data storage array. |
|
39
|
|
|
* |
|
40
|
|
|
* @var SplFixedArray |
|
41
|
|
|
*/ |
|
42
|
|
|
private $data; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Sequence constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array|Traversable $data The data to sequence |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct($data) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->setData($data); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function setData($data) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!is_traversable($data)) { |
|
57
|
|
|
// @todo Maybe create an ImmutableException for this? |
|
58
|
|
|
throw new BadMethodCallException(sprintf( |
|
59
|
|
|
'Cannot %s, %s is immutable.', |
|
60
|
|
|
__METHOD__, |
|
61
|
|
|
__CLASS__ |
|
62
|
|
|
)); |
|
63
|
|
|
} |
|
64
|
|
|
$data = array_values(to_array($data)); |
|
65
|
|
|
$this->data = SplFixedArray::fromArray($data); |
|
66
|
|
|
// $size = count($data); |
|
|
|
|
|
|
67
|
|
|
// $this->data = new SplFixedArray($size); |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function getData() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->data->toArray(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Count elements of an object |
|
78
|
|
|
* @link http://php.net/manual/en/countable.count.php |
|
79
|
|
|
* @return int The custom count as an integer. |
|
80
|
|
|
* </p> |
|
81
|
|
|
* <p> |
|
82
|
|
|
* The return value is cast to an integer. |
|
83
|
|
|
* @since 5.1.0 |
|
84
|
|
|
*/ |
|
85
|
|
|
public function count() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->data->count(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function toArray() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->getData(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function __invoke() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->toArray(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Prepend item to collection. |
|
102
|
|
|
* Prepend an item to this collection (in place). |
|
103
|
|
|
* @param mixed $item Item to prepend to collection |
|
|
|
|
|
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/public function prepend($item) |
|
106
|
|
|
{ |
|
107
|
|
|
// TODO: Implement prepend() method. |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Append item to collection. |
|
112
|
|
|
* Append an item to this collection (in place). |
|
113
|
|
|
* @param mixed $item Item to append to collection |
|
114
|
|
|
* @return $this |
|
|
|
|
|
|
115
|
|
|
*/ |
|
116
|
|
|
public function append($item) |
|
117
|
|
|
{ |
|
118
|
|
|
// TODO: Implement append() method. |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Check that collection contains a value. |
|
123
|
|
|
* You may optionally pass in a callable to provide your own equality test. |
|
124
|
|
|
* @param mixed|callable $value The value to search for |
|
125
|
|
|
* @return mixed |
|
126
|
|
|
*/ |
|
127
|
|
|
public function contains($value) |
|
128
|
|
|
{ |
|
129
|
|
|
// TODO: Implement contains() method. |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Is collection empty? |
|
134
|
|
|
* You may optionally pass in a callback which will determine if each of the items within the collection are empty. |
|
135
|
|
|
* If all items in the collection are empty according to this callback, this method will return true. |
|
136
|
|
|
* @param callable $callback The callback |
|
|
|
|
|
|
137
|
|
|
* @return bool |
|
|
|
|
|
|
138
|
|
|
*/ |
|
139
|
|
|
public function isEmpty(callable $callback = null) |
|
140
|
|
|
{ |
|
141
|
|
|
// TODO: Implement isEmpty() method. |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Pipe collection through callback. |
|
146
|
|
|
* Passes entire collection to provided callback and returns the result. |
|
147
|
|
|
* @param callable $callback |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
|
|
public function pipe(callable $callback) |
|
151
|
|
|
{ |
|
152
|
|
|
// TODO: Implement pipe() method. |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Does every item return true? |
|
157
|
|
|
* If callback is provided, this method will return true if all items in collection cause callback to return true. |
|
158
|
|
|
* Otherwise, it will return true if all items in the collection have a truthy value. |
|
159
|
|
|
* @param callable|null $callback The callback |
|
160
|
|
|
* @return bool |
|
|
|
|
|
|
161
|
|
|
*/ |
|
162
|
|
|
public function every(callable $callback = null) |
|
163
|
|
|
{ |
|
164
|
|
|
// TODO: Implement every() method. |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Does every item return false? |
|
169
|
|
|
* This method is the exact opposite of "all". |
|
170
|
|
|
* @param callable|null $callback The callback |
|
171
|
|
|
* @return bool |
|
|
|
|
|
|
172
|
|
|
*/ |
|
173
|
|
|
public function none(callable $callback = null) |
|
174
|
|
|
{ |
|
175
|
|
|
// TODO: Implement none() method. |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get collection as a sequence. |
|
180
|
|
|
* @return array |
|
|
|
|
|
|
181
|
|
|
*/ |
|
182
|
|
|
public function toSeq() |
|
183
|
|
|
{ |
|
184
|
|
|
// TODO: Implement toSeq() method. |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get collection as a dictionary. |
|
189
|
|
|
* @return array |
|
|
|
|
|
|
190
|
|
|
*/ |
|
191
|
|
|
public function toDict() |
|
192
|
|
|
{ |
|
193
|
|
|
// TODO: Implement toDict() method. |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Get collection as a set. |
|
198
|
|
|
* @return array |
|
|
|
|
|
|
199
|
|
|
*/ |
|
200
|
|
|
public function toSet() |
|
201
|
|
|
{ |
|
202
|
|
|
// TODO: Implement toSet() method. |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get collection as a map. |
|
207
|
|
|
* @return array |
|
|
|
|
|
|
208
|
|
|
*/ |
|
209
|
|
|
public function toMap() |
|
210
|
|
|
{ |
|
211
|
|
|
// TODO: Implement toMap() method. |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get collection as a list. |
|
216
|
|
|
* @return array |
|
|
|
|
|
|
217
|
|
|
*/ |
|
218
|
|
|
public function toList() |
|
219
|
|
|
{ |
|
220
|
|
|
// TODO: Implement toList() method. |
|
221
|
|
|
} |
|
222
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.