alexeyshockov/pattern-matcher
最新稳定版本:v0.1.1
Composer 安装命令:
composer require alexeyshockov/pattern-matcher
包简介
Pattern matching for PHP.
README 文档
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.' ));
统计信息
- 总下载量: 362
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-02