| @@ 25-54 (lines=30) @@ | ||
| 22 | /** |
|
| 23 | * Return the first element in a NodeCollection based on an option callback |
|
| 24 | */ |
|
| 25 | class First extends AbstractFlow |
|
| 26 | { |
|
| 27 | /** |
|
| 28 | * @var callable|null |
|
| 29 | */ |
|
| 30 | private $callback; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * First constructor. |
|
| 34 | * |
|
| 35 | * @param callable $callback |
|
| 36 | */ |
|
| 37 | public function __construct(callable $callback = null) |
|
| 38 | { |
|
| 39 | $this->callback = $callback; |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * @inheritDoc |
|
| 44 | */ |
|
| 45 | public function flow(NodeInterface $node) |
|
| 46 | { |
|
| 47 | if (!($node instanceof NodeCollectionInterface)) { |
|
| 48 | throw new InvalidArgumentException("The supplied $node is not a NodeCollectionInterface"); |
|
| 49 | } |
|
| 50 | $this->log(LogLevel::DEBUG, "Selecting the first entry from {node}", ['node' => $node]); |
|
| 51 | ||
| 52 | return $node->first($this->callback); |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| @@ 25-54 (lines=30) @@ | ||
| 22 | /** |
|
| 23 | * Return the last element in a NodeCollection based on an option callback |
|
| 24 | */ |
|
| 25 | class Last extends AbstractFlow |
|
| 26 | { |
|
| 27 | /** |
|
| 28 | * @var callable|null |
|
| 29 | */ |
|
| 30 | private $callback; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * First constructor. |
|
| 34 | * |
|
| 35 | * @param callable $callback |
|
| 36 | */ |
|
| 37 | public function __construct(callable $callback = null) |
|
| 38 | { |
|
| 39 | $this->callback = $callback; |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * @inheritDoc |
|
| 44 | */ |
|
| 45 | public function flow(NodeInterface $node) |
|
| 46 | { |
|
| 47 | if (!($node instanceof NodeCollectionInterface)) { |
|
| 48 | throw new InvalidArgumentException("The supplied $node is not a NodeCollectionInterface"); |
|
| 49 | } |
|
| 50 | $this->log(LogLevel::DEBUG, "Selecting the last entry from {node}", ['node' => $node]); |
|
| 51 | ||
| 52 | return $node->last($this->callback); |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||