dev-master
9999999-dev http://github.com/alexeyshockov/pattern-matcherPattern matching for PHP.
MIT
The Requires
The Development Requires
collections functional
Pattern matching for PHP.
Simple pattern matching technique from functional languages. For PHP., (*1)
With Option from schmittjoh/php-option: ``` php use function PatternMatcher\option_matcher;, (*2)
$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; } }, (*3)
throw new InvalidArgumentException("Helper with type " . $class->getName() . " is not registered."); })
;, (*4)
$argument = $argumentDefinition->getClass() ->flatMap($matcher) ->getOrThrow(new InvalidArgumentException( 'Parameter $' . $argumentDefinition->getName() . ': type is missed or not supported.' ));, (*5)
Or with [ColadaX](https://github.com/alexeyshockov/colada-x): ``` php use PatternMatcher\matcher; use PatternMatcher\all; use Colada\x; matcher() ->addCase(all(x()->isActive(), x()->hasFriends()), )
Or pure: ``` 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; } }, (*6)
throw new InvalidArgumentException("Helper with type " . $class->getName() . " is not registered."); })
;, (*7)
$argument = $argumentDefinition->getClass() ->flatMap($matcher) ->getOrThrow(new InvalidArgumentException( 'Parameter $' . $argumentDefinition->getName() . ': type is missed or not supported.' )); ```, (*8)
Pattern matching for PHP.
MIT
collections functional