yarimadam / phpcor
Simple chain of responsibility implementation written in PHP.
0.1.1
2019-01-29 09:00 UTC
Requires
- php: >7
Requires (Dev)
- phpunit/phpunit: ^7.4
- squizlabs/php_codesniffer: ^3.3
This package is auto-updated.
Last update: 2024-09-05 02:43:44 UTC
README
Description
Yarimadam/COR package is a simple chain of responsibility implementation written in PHP. It allows you to define handlers for each contex of your subjects and helps you with better modularizing your conditionals by decreasing cyclomatic/npath complexity in your applications.
Installation
If you are crazy enough not to use composer, just download/clone it.
Otherwise, install with composer.
composer require yarimadam/phpcor
Quick Start
Define Handlers
Create respective handlers for each contex of your subject, by extending \Yarimadam\COR\AbstractHandler
superclass.
Handler Examples
Execute when passed subject is type of a "string":
class StringHandler extends AbstractHandler { protected function isResponsible($subject): bool { return is_string($subject); } protected function process($subject): void { // echo the output echo 'We have a string here!'; } }
Execute when passed subject is type of an "array".
class ArrayHandler extends AbstractHandler { protected function isResponsible($subject): bool { return is_array($subject); } protected function process($subject): void { $output = []; foreach($subject as $item) { $output[] = $item; } // don't echo, set as handler output instead $this->chain->setResponsibleHandlerOutput($output); } }
Define Flow
$cor = new ChainOfResponsibility(); $cor->registerHandler(new StringHandler()); $cor->registerHandler(new ArrayHandler()); $subject = 'Hi there, i\'m a string!'; $cor->processThroughChain($subject);
Get Results
// fully qualified class name $cor->getResponsibleHandler(); // output from the handler - if any $cor->getResponsibleHandlerOutput();