1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-node |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-node/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-node |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataNode; |
15
|
|
|
|
16
|
|
|
use Graze\DataStructure\Collection\Collection; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class NodeCollection |
21
|
|
|
* |
22
|
|
|
* A Collection of DataNodes that can be acted upon by a flow |
23
|
|
|
* |
24
|
|
|
* @package Graze\DataFlow\Node |
25
|
|
|
*/ |
26
|
|
|
class NodeCollection extends Collection implements NodeCollectionInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param mixed $value |
30
|
|
|
* |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
11 |
|
public function add($value) |
34
|
|
|
{ |
35
|
11 |
|
if (!($value instanceof NodeInterface)) { |
36
|
1 |
|
throw new InvalidArgumentException("The specified value does not implement NodeInterface"); |
37
|
|
|
} |
38
|
10 |
|
return parent::add($value); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param callable $fn |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
2 |
|
public function apply(callable $fn) |
47
|
|
|
{ |
48
|
2 |
|
foreach ($this->items as &$item) { |
49
|
2 |
|
$out = call_user_func($fn, $item); |
50
|
2 |
|
if (isset($out) && ($out instanceof NodeInterface)) { |
51
|
1 |
|
$item = $out; |
52
|
1 |
|
} |
53
|
2 |
|
} |
54
|
|
|
|
55
|
2 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* On clone, clone all flows too |
60
|
|
|
*/ |
61
|
1 |
|
public function __clone() |
62
|
|
|
{ |
63
|
1 |
|
foreach ($this->items as &$item) { |
64
|
1 |
|
$item = clone $item; |
65
|
1 |
|
} |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
1 |
|
public function __toString() |
72
|
|
|
{ |
73
|
1 |
|
return "NodeCollection"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
1 |
|
public function getClone() |
80
|
|
|
{ |
81
|
1 |
|
return clone $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retrieve the first element that matches the optional callback |
86
|
|
|
* |
87
|
|
|
* @param callable|null $fn |
88
|
|
|
* @param mixed|null $default |
89
|
|
|
* |
90
|
|
|
* @return mixed|null |
91
|
|
|
*/ |
92
|
3 |
|
public function first(callable $fn = null, $default = null) |
93
|
|
|
{ |
94
|
3 |
|
if (is_null($fn)) { |
95
|
1 |
|
return count($this->items) > 0 ? reset($this->items) : $default; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
foreach ($this->getIterator() as $value) { |
99
|
2 |
|
if (call_user_func($fn, $value)) { |
100
|
1 |
|
return $value; |
101
|
|
|
} |
102
|
2 |
|
} |
103
|
|
|
|
104
|
1 |
|
return $default; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Receive the last element that matches the optional callback |
109
|
|
|
* |
110
|
|
|
* @param callable|null $fn |
111
|
|
|
* @param mixed|null $default |
112
|
|
|
* |
113
|
|
|
* @return mixed|null |
114
|
|
|
*/ |
115
|
3 |
|
public function last(callable $fn = null, $default = null) |
116
|
|
|
{ |
117
|
3 |
|
if (is_null($fn)) { |
118
|
1 |
|
return count($this->items) > 0 ? end($this->items) : $default; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
foreach (array_reverse($this->items) as $value) { |
122
|
2 |
|
if (call_user_func($fn, $value)) { |
123
|
1 |
|
return $value; |
124
|
|
|
} |
125
|
2 |
|
} |
126
|
|
|
|
127
|
1 |
|
return $default; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|