Skip to content

An implementation of the chain-of-responsibility pattern in PHP.

License

Notifications You must be signed in to change notification settings

ocrampete16/php-chain

Repository files navigation

PHP Chain

Build Status StyleCI Maintainability Test Coverage

An implementation of the chain-of-responsibility pattern in PHP.

Installation

composer require marcopetersen/php-chain

Usage

<?php

use MarcoPetersen\Chain\Chain;
use MarcoPetersen\Chain\Link;

// First we define the links that will be part of our chain...
class AddOne extends Link
{
    public function execute($number)
    {
        return $this->next($number + 1);
    }
}

class EndChain extends Link
{
    public function execute($payload)
    {
        return $payload;
    }
}

// ...after which we chain them up together.
$chain = (new Chain())
    ->then(new AddOne()) // you can pass in instances...
    ->then(AddOne::class) // ...or just the FQCN, if you prefer.
    ->then(EndChain::class) // To end the chain, just don't call `next`.
    ->then(AddOne::class) // This won't get called.

$chain->execute(1); // 3

About

An implementation of the chain-of-responsibility pattern in PHP.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages