PHP code example of yarimadam / phpcor

1. Go to this page and download the library: Download yarimadam/phpcor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yarimadam / phpcor example snippets


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!';
    }
}

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);
    }
}

$cor = new ChainOfResponsibility();

$cor->registerHandler(new StringHandler());
$cor->registerHandler(new ArrayHandler());

$subject = 'Hi there, i\'m a string!';

$cor->processThroughChain($subject);

// fully qualified class name
$cor->getResponsibleHandler();

// output from the handler - if any
$cor->getResponsibleHandlerOutput();