iannsp/phpmatch

1.0.1 2017-04-30 04:47 UTC

This package is not auto-updated.

Last update: 2024-05-17 18:35:21 UTC


README

Build Status

How to

  • install: composer require 'iannsp/phpmatch'
  • use:
    use phpmatch;
    use \phpmatch\Match as M;// to made easier use domains constants.

Match Function

Matches start with match function. This function receive the values you need
test the matches.

Simple Scalar Matching

    $a = 1;
    $b = 1;
    match($a)->with($b); // true.
    match($variableUnsigned)->with(null); // true

Array Matching

    $a = [1   , 2, 3];
    $b = ["anything", 2, 3];

    // because $b[0]
    match($a)->with($b); // false 

    // you can ignore $b[0] using M::_
    $c = [M::_, 2, 3];
    match($c)->with($b)); // true

    // and replace $d[0] with $b[0] using M::r
    $d = [M::r, 2, 3];
    match($d)->with($b); // true
    echo $d[0]; // "anything"

    //you can do the same with associative arrays.
    $e = ["first"=> M::r, 2, 3];
    match($e)->with(["first"=>"anything", 2, 3]);// true
    echo $e["first"]; // "anything" 

    // THE MATCH PARAM NEED BE A VARIABLE.
    match([null,2,3])->with($b); // Fatal error:Only variables can be passed by reference

TODO

  • Case
$a = 1;
match($a)
    ->incase(   1   )->doIt(function(){ echo "a is 1";})
    ->incase([1,2,3])->doIt(function(){echo "a is array [1,2,3];});
  • Guards
$a = 1;
match($a)
    ->incase(   1   )->when(true)->doIt(function(){ echo "a is 1";})
    ->incase([1,"y"=> M::r,3])->when($y > 10)->doIt(function(){echo "a is array [1,2,3];});