1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace YaLinqo; |
4
|
|
|
|
5
|
|
|
use YaLinqo; |
6
|
|
|
|
7
|
|
|
trait EnumerableGeneration |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Cycles through the source sequence. |
11
|
|
|
* <p><b>Syntax</b>: cycle (source) |
12
|
|
|
* <p>Source keys are discarded. |
13
|
|
|
* @param array|\Iterator|\IteratorAggregate|Enumerable $source Source sequence. |
14
|
|
|
* @throws \InvalidArgumentException If source is not array or Traversible or Enumerable. |
15
|
|
|
* @throws \UnexpectedValueException If source contains no elements (checked during enumeration). |
16
|
|
|
* @return Enumerable Endless list of items repeating the source sequence. |
17
|
|
|
* @package YaLinqo\Generation |
18
|
|
|
*/ |
19
|
|
|
public static function cycle ($source) |
20
|
|
|
{ |
21
|
|
|
$source = self::from($source); |
22
|
|
|
|
23
|
|
|
return new self(function () use ($source) { |
|
|
|
|
24
|
|
|
$isEmpty = true; |
25
|
|
|
while (true) { |
26
|
|
|
foreach ($source as $v) { |
27
|
|
|
yield $v; |
28
|
|
|
$isEmpty = false; |
29
|
|
|
} |
30
|
|
|
if ($isEmpty) |
31
|
|
|
throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
32
|
|
|
} |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns an empty sequence. |
38
|
|
|
* <p><b>Syntax</b>: emptyEnum () |
39
|
|
|
* @return Enumerable |
40
|
|
|
* @package YaLinqo\Generation |
41
|
|
|
*/ |
42
|
|
|
public static function emptyEnum () |
43
|
|
|
{ |
44
|
|
|
return new self(new \EmptyIterator, false); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Converts source into Enumerable sequence. |
49
|
|
|
* <p><b>Syntax</b>: from (source) |
50
|
|
|
* <p>Result depends on the type of source: |
51
|
|
|
* <ul> |
52
|
|
|
* <li><b>array</b>: Enumerable from ArrayIterator; |
53
|
|
|
* <li><b>Enumerable</b>: Enumerable source itself; |
54
|
|
|
* <li><b>Iterator</b>: Enumerable from Iterator; |
55
|
|
|
* <li><b>IteratorAggregate</b>: Enumerable from Iterator returned from getIterator() method; |
56
|
|
|
* <li><b>Traversable</b>: Enumerable from the result of foreach over source. |
57
|
|
|
* </ul> |
58
|
|
|
* @param array|\Iterator|\IteratorAggregate|\Traversable|Enumerable $source Value to convert into Enumerable sequence. |
59
|
|
|
* @throws \InvalidArgumentException If source is not array or Traversible or Enumerable. |
60
|
|
|
* @return Enumerable |
61
|
|
|
* @package YaLinqo\Generation |
62
|
|
|
*/ |
63
|
|
|
public static function from ($source) |
64
|
|
|
{ |
65
|
|
|
$it = null; |
66
|
|
|
if ($source instanceof Enumerable) |
67
|
|
|
return $source; |
68
|
|
|
else if (is_array($source)) |
69
|
|
|
$it = new \ArrayIterator($source); |
70
|
|
|
elseif ($source instanceof \Iterator) |
71
|
|
|
$it = $source; |
72
|
|
|
elseif ($source instanceof \IteratorAggregate) |
73
|
|
|
$it = $source->getIterator(); |
74
|
|
|
elseif ($source instanceof \Traversable) |
75
|
|
|
$it = self::fromTraversable($source); |
76
|
|
|
if ($it !== null) { |
77
|
|
|
return new self($it, false); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
throw new \InvalidArgumentException('source must be array or Traversable.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private static function fromTraversable ($source) |
83
|
|
|
{ |
84
|
|
|
foreach ($source as $k => $v) |
85
|
|
|
yield $k => $v; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Generates a sequence by mimicking a for loop. |
90
|
|
|
* <p><b>Syntax</b>: generate (funcValue {(v, k) ==> value} [, seedValue [, funcKey {(v, k) ==> key} [, seedKey]]]) |
91
|
|
|
* <p>If seedValue is null, the first value will be the result of calling funcValue on seedValue and seedKey. The same applies for seedKey. |
92
|
|
|
* @param callable $funcValue {(v, k) ==> value} State update function to run on value after every iteration of the generator loop. Default: value. |
93
|
|
|
* @param mixed $seedValue Initial state of the generator loop for values. Default: null. |
94
|
|
|
* @param callable|null $funcKey {(v, k) ==> key} State update function to run on key after every iteration of the generator loop. Default: increment. |
95
|
|
|
* @param mixed $seedKey Initial state of the generator loop ofr keys. Default: 0. |
96
|
|
|
* @return Enumerable |
97
|
|
|
* @package YaLinqo\Generation |
98
|
|
|
*/ |
99
|
|
|
public static function generate ($funcValue, $seedValue = null, $funcKey = null, $seedKey = null) |
100
|
|
|
{ |
101
|
|
|
$funcValue = Utils::createLambda($funcValue, 'v,k'); |
102
|
|
|
$funcKey = Utils::createLambda($funcKey, 'v,k', false); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return new self(function () use ($funcValue, $funcKey, $seedValue, $seedKey) { |
|
|
|
|
105
|
|
|
$key = $seedKey === null ? ($funcKey ? $funcKey($seedValue, $seedKey) : 0) : $seedKey; |
106
|
|
|
$value = $seedValue === null ? $funcValue($seedValue, $seedKey) : $seedValue; |
107
|
|
|
yield $key => $value; |
108
|
|
|
while (true) { |
109
|
|
|
list($value, $key) = [ |
110
|
|
|
$funcValue($value, $key), |
111
|
|
|
$funcKey ? $funcKey($value, $key) : $key + 1, |
112
|
|
|
]; |
113
|
|
|
yield $key => $value; |
114
|
|
|
} |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Generates a sequence of integral numbers to infinity. |
120
|
|
|
* <p><b>Syntax</b>: toInfinity ([start [, step]]) |
121
|
|
|
* @param int $start The first integer in the sequence. Default: 0. |
122
|
|
|
* @param int $step The difference between adjacent integers. Default: 1. |
123
|
|
|
* @return Enumerable |
124
|
|
|
* @package YaLinqo\Generation |
125
|
|
|
*/ |
126
|
|
|
public static function toInfinity ($start = 0, $step = 1) |
127
|
|
|
{ |
128
|
|
|
return new self(function () use ($start, $step) { |
|
|
|
|
129
|
|
|
$value = $start - $step; |
130
|
|
|
while (true) |
131
|
|
|
yield $value += $step; |
132
|
|
|
}); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Searches subject for all matches to the regular expression given in pattern and enumerates them in the order specified by flags. After the first match is found, the subsequent searches are continued on from end of the last match. |
137
|
|
|
* <p><b>Syntax</b>: matches (subject, pattern [, flags]) |
138
|
|
|
* @param string $subject The input string. |
139
|
|
|
* @param string $pattern The pattern to search for, as a string. |
140
|
|
|
* @param int $flags Can be a combination of the following flags: PREG_PATTERN_ORDER, PREG_SET_ORDER, PREG_OFFSET_CAPTURE. Default: PREG_SET_ORDER. |
141
|
|
|
* @return Enumerable |
142
|
|
|
* @see preg_match_all |
143
|
|
|
* @package YaLinqo\Generation |
144
|
|
|
*/ |
145
|
|
|
public static function matches ($subject, $pattern, $flags = PREG_SET_ORDER) |
146
|
|
|
{ |
147
|
|
|
return new self(function () use ($subject, $pattern, $flags) { |
|
|
|
|
148
|
|
|
preg_match_all($pattern, $subject, $matches, $flags); |
149
|
|
|
return $matches !== false ? self::from($matches)->getIterator() : self::emptyEnum(); |
|
|
|
|
150
|
|
|
}); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Generates a sequence of integral numbers to negative infinity. |
155
|
|
|
* <p><b>Syntax</b>: toNegativeInfinity ([start [, step]]) |
156
|
|
|
* @param int $start The first integer in the sequence. Default: 0. |
157
|
|
|
* @param int $step The difference between adjacent integers. Default: 1. |
158
|
|
|
* @return Enumerable |
159
|
|
|
* @package YaLinqo\Generation |
160
|
|
|
*/ |
161
|
|
|
public static function toNegativeInfinity ($start = 0, $step = 1) |
162
|
|
|
{ |
163
|
|
|
return self::toInfinity($start, -$step); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns a sequence that contains a single element with a specified value. |
168
|
|
|
* <p><b>Syntax</b>: returnEnum (element) |
169
|
|
|
* @param mixed $element The single element in the resulting sequence. |
170
|
|
|
* @return Enumerable Observable sequence containing the single specified element. |
171
|
|
|
* @package YaLinqo\Generation |
172
|
|
|
*/ |
173
|
|
|
public static function returnEnum ($element) |
174
|
|
|
{ |
175
|
|
|
return self::repeat($element, 1); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Generates a sequence of integral numbers, beginning with start and containing count elements. |
180
|
|
|
* <p><b>Syntax</b>: range (start, count [, step]) |
181
|
|
|
* <p>Keys in the generated sequence are sequental: 0, 1, 2 etc. |
182
|
|
|
* <p>Example: range(3, 4, 2) = 3, 5, 7, 9. |
183
|
|
|
* @param int $start The value of the first integer in the sequence. |
184
|
|
|
* @param int $count The number of integers to generate. |
185
|
|
|
* @param int $step The difference between adjacent integers. Default: 1. |
186
|
|
|
* @return Enumerable A sequence that contains a range of integral numbers. |
187
|
|
|
* @package YaLinqo\Generation |
188
|
|
|
*/ |
189
|
|
|
public static function range ($start, $count, $step = 1) |
190
|
|
|
{ |
191
|
|
|
if ($count <= 0) |
192
|
|
|
return self::emptyEnum(); |
193
|
|
|
return new self(function () use ($start, $count, $step) { |
|
|
|
|
194
|
|
|
$value = $start - $step; |
195
|
|
|
while ($count-- > 0) |
196
|
|
|
yield $value += $step; |
197
|
|
|
}); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Generates a reversed sequence of integral numbers, beginning with start and containing count elements. |
202
|
|
|
* <p><b>Syntax</b>: rangeDown (start, count [, step]) |
203
|
|
|
* <p>Keys in the generated sequence are sequental: 0, 1, 2 etc. |
204
|
|
|
* <p>Example: rangeDown(9, 4, 2) = 9, 7, 5, 3. |
205
|
|
|
* @param int $start The value of the first integer in the sequence. |
206
|
|
|
* @param int $count The number of integers to generate. |
207
|
|
|
* @param int $step The difference between adjacent integers. Default: 1. |
208
|
|
|
* @return Enumerable A sequence that contains a range of integral numbers. |
209
|
|
|
* @package YaLinqo\Generation |
210
|
|
|
*/ |
211
|
|
|
public static function rangeDown ($start, $count, $step = 1) |
212
|
|
|
{ |
213
|
|
|
return self::range($start, $count, -$step); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Generates a sequence of integral numbers within a specified range from start to end. |
218
|
|
|
* <p><b>Syntax</b>: rangeTo (start, end [, step]) |
219
|
|
|
* <p>Keys in the generated sequence are sequental: 0, 1, 2 etc. |
220
|
|
|
* <p>Example: rangeTo(3, 9, 2) = 3, 5, 7, 9. |
221
|
|
|
* @param int $start The value of the first integer in the sequence. |
222
|
|
|
* @param int $end The value of the last integer in the sequence (not included). |
223
|
|
|
* @param int $step The difference between adjacent integers. Default: 1. |
224
|
|
|
* @throws \InvalidArgumentException If step is not a positive number. |
225
|
|
|
* @return Enumerable A sequence that contains a range of integral numbers. |
226
|
|
|
* @package YaLinqo\Generation |
227
|
|
|
*/ |
228
|
|
|
public static function rangeTo ($start, $end, $step = 1) |
229
|
|
|
{ |
230
|
|
|
if ($step <= 0) |
231
|
|
|
throw new \InvalidArgumentException(Errors::STEP_NEGATIVE); |
232
|
|
|
return new self(function () use ($start, $end, $step) { |
|
|
|
|
233
|
|
|
if ($start <= $end) { |
234
|
|
|
for ($i = $start; $i < $end; $i += $step) |
235
|
|
|
yield $i; |
236
|
|
|
} |
237
|
|
|
else { |
238
|
|
|
for ($i = $start; $i > $end; $i -= $step) |
239
|
|
|
yield $i; |
240
|
|
|
} |
241
|
|
|
}); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Generates an sequence that contains one repeated value. |
246
|
|
|
* <p><b>Syntax</b>: repeat (element) |
247
|
|
|
* <p>Generates an endless sequence that contains one repeated value. |
248
|
|
|
* <p><b>Syntax</b>: repeat (element, count) |
249
|
|
|
* <p>Generates a sequence of specified length that contains one repeated value. |
250
|
|
|
* <p>Keys in the generated sequence are sequental: 0, 1, 2 etc. |
251
|
|
|
* @param int $element The value to be repeated. |
252
|
|
|
* @param int $count The number of times to repeat the value in the generated sequence. Default: null. |
253
|
|
|
* @throws \InvalidArgumentException If count is less than 0. |
254
|
|
|
* @return Enumerable A sequence that contains a repeated value. |
255
|
|
|
* @package YaLinqo\Generation |
256
|
|
|
*/ |
257
|
|
|
public static function repeat ($element, $count = null) |
258
|
|
|
{ |
259
|
|
|
if ($count < 0) |
260
|
|
|
throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO); |
261
|
|
|
return new self(function () use ($element, $count) { |
|
|
|
|
262
|
|
|
for ($i = 0; $i < $count || $count === null; $i++) |
263
|
|
|
yield $element; |
264
|
|
|
}); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Split the given string by a regular expression. |
269
|
|
|
* <p><b>Syntax</b>: split (subject, pattern [, flags]) |
270
|
|
|
* @param string $subject The input string. |
271
|
|
|
* @param string $pattern The pattern to search for, as a string. |
272
|
|
|
* @param int $flags flags can be any combination of the following flags: PREG_SPLIT_NO_EMPTY, PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE. Default: 0. |
273
|
|
|
* @return Enumerable |
274
|
|
|
* @see preg_split |
275
|
|
|
* @package YaLinqo\Generation |
276
|
|
|
*/ |
277
|
|
|
public static function split ($subject, $pattern, $flags = 0) |
278
|
|
|
{ |
279
|
|
|
return new self( |
280
|
|
|
new \ArrayIterator(preg_split($pattern, $subject, -1, $flags)), |
|
|
|
|
281
|
|
|
false |
282
|
|
|
); |
283
|
|
|
} |
284
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.