zweifisch/match

PHP的模式匹配

v0.0.3 2013-09-03 13:01 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:52:23 UTC


README

PHP的模式匹配

通过composer安装

composer require 'zweifisch/match:*'

解构值

$array = [1,[2,[3,4]]];
extract(\match\destruct(['a',['b',['c','d']]], $array)) or die("match failed");
echo "$a $b $c $d"; // "1 2 3 4"

传递给函数

$input = ['method'=>'foo', 'params'=>['bar']];
$pattern = ['method'=>'func', 'params'=>['arg1']];
$result = \match\let($pattern, $input, function($arg1, $func){
	return "$func $arg1";
});
// "foo bar"

解构多个值

$input = ['method'=>'foo', 'params'=>['bar']];
$result = \match\let(
	['method'=>'func', 'params'=>['arg1']], $input
	'now', time(),
	function($func, $arg1, $now){
		return "$func $arg1 $now";
	}
);

测试

composer install --dev
vendor/bin/phpunit -c tests