https://github.com/zweifisch/php-pattern-matching
pattern matching for php
https://github.com/zweifisch/php-pattern-matching
Last synced: 4 months ago
JSON representation
pattern matching for php
- Host: GitHub
- URL: https://github.com/zweifisch/php-pattern-matching
- Owner: zweifisch
- Created: 2013-06-22T03:56:46.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-09-03T13:03:02.000Z (over 11 years ago)
- Last Synced: 2024-11-24T21:05:10.850Z (6 months ago)
- Language: PHP
- Size: 129 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# match [](https://travis-ci.org/zweifisch/php-pattern-matching)
pattern matching for php
install via composer
```sh
composer require 'zweifisch/match:*'
```destruct value
```php
$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"
```passing to a function
```php
$input = ['method'=>'foo', 'params'=>['bar']];
$pattern = ['method'=>'func', 'params'=>['arg1']];
$result = \match\let($pattern, $input, function($arg1, $func){
return "$func $arg1";
});
// "foo bar"
```destruct multiple values
```php
$input = ['method'=>'foo', 'params'=>['bar']];
$result = \match\let(
['method'=>'func', 'params'=>['arg1']], $input
'now', time(),
function($func, $arg1, $now){
return "$func $arg1 $now";
}
);
```## tests
```sh
composer install --dev
vendor/bin/phpunit -c tests
```