PHP code example of alexeyshockov / pattern-matcher

1. Go to this page and download the library: Download alexeyshockov/pattern-matcher 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/ */

    

alexeyshockov / pattern-matcher example snippets

 php
use function PatternMatcher\option_matcher;

$matcher = option_matcher(function ($className, ReflectionClass $class) {
    return ($class->getName() == $className) || $class->isSubclassOf($className);
})
    ->addCase(InputInterface::class, $input)
    ->addCase(OutputInterface::class, $output)
    ->addCase(Output::class, new Output($output))
    ->addCase(HelperInterface::class, function (ReflectionClass $class) {
        foreach ($this->getHelperSet() as $helper) {
            if ($class->isInstance($helper)) {
                return $helper;
            }
        }

        throw new InvalidArgumentException("Helper with type " . $class->getName() . " is not registered.");
    })
;

$argument = $argumentDefinition->getClass()
    ->flatMap($matcher)
    ->getOrThrow(new InvalidArgumentException(
        'Parameter $' . $argumentDefinition->getName() . ': type is missed or not supported.'
    ));
 php
$matcher = (new PatternMatcher(function ($className, ReflectionClass $class) {
    return ($class->getName() == $className) || $class->isSubclassOf($className);
}))
    ->addCase(InputInterface::class, $input)
    ->addCase(OutputInterface::class, $output)
    ->addCase(Output::class, new Output($output))
    ->addCase(HelperInterface::class, function (ReflectionClass $class) {
        foreach ($this->getHelperSet() as $helper) {
            if ($class->isInstance($helper)) {
                return $helper;
            }
        }

        throw new InvalidArgumentException("Helper with type " . $class->getName() . " is not registered.");
    })
;

$argument = $argumentDefinition->getClass()
    ->flatMap($matcher)
    ->getOrThrow(new InvalidArgumentException(
        'Parameter $' . $argumentDefinition->getName() . ': type is missed or not supported.'
    ));