yaroslawww/php-pipe-passage

Alternative implementation 'Chain of Responsibility' pattern, using pipe.

1.0.0 2021-11-08 08:26 UTC

This package is auto-updated.

Last update: 2024-10-26 16:47:08 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

Installation

Install the package via composer:

composer require yaroslawww/php-pipe-passage

Usage

 $pipe = Pipe::make([
    function ($entity, \Closure $next) {
        $entity->test = 'test value';

        return $next($entity);
    },
    function ($entity, \Closure $next) {
        $entity->test2 = 'second test value';

        return $next($entity);
    },
])
            ->next(SetNameHandler::class)
            ->next(new SetCompanyHandler('web company'))
            ->next(function ($entity, \Closure $next) {
                $entity->test3 = 'third test value';

                return $next($entity);
            });

$entityObject = $pipe->pass(new \stdClass());

$this->assertEquals('test value', $entityObject->test);
$this->assertEquals('second test value', $entityObject->test2);
$this->assertEquals('third test value', $entityObject->test3);
$this->assertEquals('default', $entityObject->name);
$this->assertEquals('web company', $entityObject->company);

Example of handler

class ExamplePipeSection implements PipeSection {
    
    public function handle($entity, \Closure $next)
    {
        // There will be some functionality

        return $next($entity);
    }
    
}

Credits

  • Think Studio