alexeyshockov/pattern-matcher

Pattern matching for PHP.

v0.1.1 2019-02-11 13:06 UTC

This package is auto-updated.

Last update: 2024-12-12 03:42:20 UTC


README

Simple pattern matching technique from functional languages. For PHP.

Examples

With Option from schmittjoh/php-option:

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.'
    ));

Or with ColadaX:

use PatternMatcher\matcher;
use PatternMatcher\all;
use Colada\x;

matcher()
    ->addCase(all(x()->isActive(), x()->hasFriends()), )

Or pure:

$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.'
    ));