yaroslawww / php-pipe-passage
Alternative implementation 'Chain of Responsibility' pattern, using pipe.
1.0.0
2021-11-08 08:26 UTC
Requires
- php: >=8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.10
This package is auto-updated.
Last update: 2024-11-26 16:57:28 UTC
README
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);
}
}