@@ 25-58 (lines=34) @@ | ||
22 | /** |
|
23 | * Apply a flow to each element of a NodeCollection |
|
24 | */ |
|
25 | class Each extends AbstractFlow |
|
26 | { |
|
27 | /** |
|
28 | * @var FlowInterface |
|
29 | */ |
|
30 | private $flow; |
|
31 | ||
32 | /** |
|
33 | * Each constructor. |
|
34 | * |
|
35 | * @param FlowInterface $flow |
|
36 | */ |
|
37 | public function __construct(FlowInterface $flow) |
|
38 | { |
|
39 | $this->flow = $flow; |
|
40 | } |
|
41 | ||
42 | /** |
|
43 | * @param NodeInterface $node |
|
44 | * |
|
45 | * @return NodeCollectionInterface |
|
46 | */ |
|
47 | public function flow(NodeInterface $node) |
|
48 | { |
|
49 | if (!($node instanceof NodeCollectionInterface)) { |
|
50 | throw new InvalidArgumentException("The supplied $node is not a NodeCollectionInterface"); |
|
51 | } |
|
52 | ||
53 | $map = new Map(function (NodeInterface $item) { |
|
54 | return $this->flow->flow($item); |
|
55 | }); |
|
56 | return $map->flow($node); |
|
57 | } |
|
58 | } |
|
59 |
@@ 24-56 (lines=33) @@ | ||
21 | /** |
|
22 | * Apply a callback to each element of a NodeCollection |
|
23 | */ |
|
24 | class Map extends AbstractFlow |
|
25 | { |
|
26 | /** |
|
27 | * @var callable |
|
28 | */ |
|
29 | private $callback; |
|
30 | ||
31 | /** |
|
32 | * First constructor. |
|
33 | * |
|
34 | * @param callable $callback |
|
35 | */ |
|
36 | public function __construct(callable $callback) |
|
37 | { |
|
38 | $this->callback = $callback; |
|
39 | } |
|
40 | ||
41 | /** |
|
42 | * @param NodeInterface $node |
|
43 | * |
|
44 | * @return NodeCollectionInterface |
|
45 | */ |
|
46 | public function flow(NodeInterface $node) |
|
47 | { |
|
48 | if (!($node instanceof NodeCollectionInterface)) { |
|
49 | throw new InvalidArgumentException("The supplied $node is not a NodeCollectionInterface"); |
|
50 | } |
|
51 | ||
52 | $class = get_class($node); |
|
53 | ||
54 | return new $class($node->map($this->callback)); |
|
55 | } |
|
56 | } |
|
57 |