1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pipes\Filter; |
4
|
|
|
|
5
|
|
|
trait StopIfTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Stops the iteration if the callback returns a true-ish value. |
9
|
|
|
* The current element will not be included. |
10
|
|
|
* |
11
|
|
|
* The passed callback will be invoked with the following argument: |
12
|
|
|
* |
13
|
|
|
* - $value (iterator's current()) |
14
|
|
|
* |
15
|
|
|
* If the second parameter is true the following arguments will be added |
16
|
|
|
* to the call: |
17
|
|
|
* |
18
|
|
|
* - $key (iterator's key()) |
19
|
|
|
* - $iterator (the iterator itself) |
20
|
|
|
* |
21
|
|
|
* @param $______callback |
22
|
|
|
* @param bool $______allArgs |
23
|
|
|
* |
24
|
|
|
* @return \Pipes\Pipe |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function stopIf($______callback, $______allArgs = false) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$iterator = $this->getIterator(); |
|
|
|
|
29
|
|
|
$pipe = $this; |
30
|
|
|
|
31
|
|
|
$generator = function () use ($pipe, $iterator, $______callback, $______allArgs) { |
32
|
|
|
foreach ($iterator as $key => $value) { |
33
|
|
|
if ($pipe->executeCallback($______callback, $______allArgs, $value, $key, $iterator) |
|
|
|
|
34
|
|
|
) { |
35
|
|
|
return; |
36
|
|
|
} // @codeCoverageIgnore |
37
|
|
|
yield $key => $value; |
38
|
|
|
} |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
return $this->chainWith($generator()); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Stops the iteration if the callback returns a true-ish value. |
46
|
|
|
* The current element will not be included. |
47
|
|
|
* |
48
|
|
|
* The passed callback will be invoked with the following argument: |
49
|
|
|
* |
50
|
|
|
* - $value (iterator's current()) |
51
|
|
|
* |
52
|
|
|
* If the second parameter is true the following arguments will be added |
53
|
|
|
* to the call: |
54
|
|
|
* |
55
|
|
|
* - $key (iterator's key()) |
56
|
|
|
* - $iterator (the iterator itself) |
57
|
|
|
* |
58
|
|
|
* @param $______callback |
59
|
|
|
* @param bool $______allArgs |
60
|
|
|
* |
61
|
|
|
* @return \Pipes\Pipe |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function continueIf($______callback, $______allArgs = false) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$iterator = $this->getIterator(); |
|
|
|
|
66
|
|
|
$pipe = $this; |
67
|
|
|
|
68
|
|
|
$generator = function () use ($pipe, $iterator, $______callback, $______allArgs) { |
69
|
|
|
foreach ($iterator as $key => $value) { |
70
|
|
|
if (!$pipe->executeCallback($______callback, $______allArgs, $value, $key, $iterator) |
|
|
|
|
71
|
|
|
) { |
72
|
|
|
return; |
73
|
|
|
} // @codeCoverageIgnore |
74
|
|
|
yield $key => $value; |
75
|
|
|
} |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
return $this->chainWith($generator()); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.