index/src/Routing/RouteCallable.php

73 lines
1.6 KiB
PHP

<?php
// RouteCallable.php
// Created: 2022-01-20
// Updated: 2022-02-02
namespace Index\Routing;
/**
* Stack of callables and arguments for route responses.
*/
class RouteCallable {
private array $callables;
private array $args;
/**
* @internal
*/
public function __construct(array $callables, array $args) {
$this->callables = $callables;
$this->args = $args;
}
/**
* Callables in order that they should be executed.
*
* @return array Sequential list of callables.
*/
public function getCallables(): array {
return $this->callables;
}
/**
* Arguments to be sent to each callable.
*
* @return array Sequential argument list for the callables.
*/
public function getArguments(): array {
return $this->args;
}
/**
* Runs all callables and returns their returns as an array.
*
* @return array Results from the callables.
*/
public function runAll(): array {
$results = [];
foreach($this->callables as $callable) {
$result = $callable(...$this->args);
if($result !== null)
$results[] = $result;
}
return $results;
}
/**
* Runs all callables unless one returns something.
*
* @return mixed Result from the returning callable.
*/
public function run(): mixed {
foreach($this->callables as $callable) {
$result = $callable(...$this->args);
if($result !== null)
return $result;
}
return null;
}
}