for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace PhpJsonRpc\Common\Chain;
/**
* Based on "Chain of Responsibility" pattern
*/
class Chain
{
* @var Chain
private $next;
* @var callable
private $callback;
* Chain constructor.
private function __construct()
$this->next = null;
$this->callback = null;
}
* @return Chain
public static function createBase(): Chain
return new Chain();
* @param callable $callback
*
public static function createWith(callable $callback): Chain
$element = new Chain();
$element->callback = $callback;
return $element;
* @param Chain $element
* @return $this
public function add(Chain $element)
if ($this->next) {
$this->next->add($element);
} else {
$this->next = $element;
return $this;
* @param Container $container
* @return mixed
public function handle(Container $container): Container
// Execute callback and pass result next chain
if (is_callable($this->callback)) {
$result = call_user_func($this->callback, $container);
if (!$this->next) {
return $result;
return $this->next->handle($result);
// End of chain
return $container;
// Execute next chain
return $this->next->handle($container);