2017 © Pedro Peláez
 

library pattern-matcher

Pattern matching for PHP.

image

alexeyshockov/pattern-matcher

Pattern matching for PHP.

  • Tuesday, September 8, 2015
  • by alexeyshockov
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

pattern-matcher

Simple pattern matching technique from functional languages. For PHP., (*1)

Examples

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)

The Versions

08/09 2015

dev-master

9999999-dev http://github.com/alexeyshockov/pattern-matcher

Pattern matching for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

collections functional