1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataNode; |
4
|
|
|
|
5
|
|
|
use Graze\DataStructure\Collection\Collection; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class NodeCollection |
10
|
|
|
* |
11
|
|
|
* A Collection of DataNodes that can be acted upon by a flow |
12
|
|
|
* |
13
|
|
|
* @package Graze\DataFlow\Node |
14
|
|
|
*/ |
15
|
|
|
class NodeCollection extends Collection implements NodeCollectionInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
9 |
|
public function add($value) |
21
|
|
|
{ |
22
|
9 |
|
if (!($value instanceof NodeInterface)) { |
23
|
1 |
|
throw new InvalidArgumentException("The specified value does not implement NodeInterface"); |
24
|
|
|
} |
25
|
8 |
|
return parent::add($value); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
2 |
|
public function apply($fn) |
32
|
|
|
{ |
33
|
2 |
|
foreach ($this->items as &$item) { |
34
|
2 |
|
$out = call_user_func($fn, $item); |
35
|
1 |
|
if (isset($out) && ($out instanceof NodeInterface)) { |
36
|
1 |
|
$item = $out; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* On clone, clone all flows too |
45
|
|
|
*/ |
46
|
|
|
public function __clone() |
47
|
|
|
{ |
48
|
|
|
foreach ($this->items as &$item) { |
49
|
|
|
$item = clone $item; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function __toString() |
57
|
|
|
{ |
58
|
|
|
return "NodeCollection"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function getClone() |
65
|
|
|
{ |
66
|
|
|
return clone $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param callable $fn |
71
|
|
|
* @param mixed|null $default |
72
|
|
|
* |
73
|
|
|
* @return NodeInterface|null |
74
|
|
|
*/ |
75
|
3 |
View Code Duplication |
public function first(callable $fn = null, $default = null) |
|
|
|
|
76
|
|
|
{ |
77
|
3 |
|
if (is_null($fn)) { |
78
|
1 |
|
return count($this->items) > 0 ? reset($this->items) : $default; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
foreach ($this->getIterator() as $value) { |
82
|
2 |
|
if (call_user_func($fn, $value)) { |
83
|
2 |
|
return $value; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $default; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param callable $fn |
92
|
|
|
* @param mixed|null $default |
93
|
|
|
* |
94
|
|
|
* @return NodeInterface|null |
95
|
|
|
*/ |
96
|
2 |
View Code Duplication |
public function last(callable $fn = null, $default = null) |
|
|
|
|
97
|
|
|
{ |
98
|
2 |
|
if (is_null($fn)) { |
99
|
1 |
|
return count($this->items) > 0 ? end($this->items) : $default; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
foreach (array_reverse($this->items) as $value) { |
103
|
1 |
|
if (call_user_func($fn, $value)) { |
104
|
1 |
|
return $value; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $default; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.