lukasss93/smatch

Match for PHP 7.3 and PHP 7.4

Fund package maintenance!
Lukasss93

v1.0 2021-04-30 00:10 UTC

This package is auto-updated.

Last update: 2022-07-29 02:27:22 UTC


README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f536d617463682e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6c756b617373733933253246736d61746368267061747465726e3d636861726c696542726f776e267374796c653d7374796c655f31266465736372697074696f6e3d4d617463682b666f722b5048502b372e332b616e642b5048502b372e342b266d643d302673686f7757617465726d61726b3d3026666f6e7453697a653d313535707826696d616765733d6d656e752d616c742d32

Smatch

Version Packagist Downloads PHP GitHub Build Codecov

Match for PHP 7.3 and PHP 7.4

🚀 Installation

You can install the package using composer:

composer require lukasss93/smatch

👓 Usage

Structure of a smatch function

$result = smatch('apple')
    ->case('pear', 'tasty')
    ->case('apple', 'delicious')
    ->case('banana', 'yellow')
    ->get();

// $result = 'delicious'

Case methods value can accept a closure too.

$result = smatch('apple')
    ->case('pear', fn () => 'tasty')
    ->case('apple', fn () => 'delicious')
    ->case('banana', fn () => 'yellow')
    ->get();

// $result = 'delicious'

Case method may contain an array of values

$result = smatch('chair')
    ->case(['apple', 'pear', 'banana'], 'fruit')
    ->case(['table', 'chair'], 'furniture')
    ->get();

// $result = 'furniture'

A special case is the fallback pattern. This pattern matches anything that wasn't previously matched.

$result = smatch('strawberry')
    ->case('pear', 'tasty')
    ->case('apple', 'delicious')
    ->case('banana', 'yellow')
    ->fallback('invalid')
    ->get();
    
// $result = 'invalid'

Fallback method can accept a closure too.

$result = smatch('strawberry')
    ->case('pear', 'tasty')
    ->case('apple', 'delicious')
    ->case('banana', 'yellow')
    ->fallback(fn () => 'invalid')
    ->get();
    
// $result = 'invalid'

Note: Multiple fallback methods will override the last one.

A smatch function must be exhaustive. If the subject function is not handled by any case method an UnhandledSmatchException is thrown.

Example of an unhandled smatch function

try {
    $result = smatch('strawberry')
        ->case('pear', 'tasty')
        ->case('apple', 'delicious')
        ->case('banana', 'yellow')
        ->get();
} catch (UnhandledSmatchException $e){
    echo $e->getMessage();
}

// $e->getMessage() = Unhandled smatch value of type string

Using getOr method to handle missing fallback method

$result = smatch('car')
    ->case('pear', 'tasty')
    ->case('apple', 'delicious')
    ->case('banana', 'yellow')
    ->getOr(fn () => 'complex logic');

// $result = 'complex logic'

Using smatch function to handle non identity checks

It is possible to use a smatch function to handle non-identity conditional cases by using true as the subject function.

Using a generalized smatch function to branch on integer ranges

$age = 23;

$result = smatch(true)
    ->case($age >= 65, 'senior')
    ->case($age >= 25, 'adult')
    ->case($age >= 18, 'young adult')
    ->fallback('kid')
    ->get();

// $result = 'young adult'

Using a generalized smatch function to branch on string content

$text = 'Bienvenue chez nous';

$result = smatch(true)
    ->case(str_contains($text, 'Welcome') || str_contains($text, 'Hello'), 'en')
    ->case(str_contains($text, 'Bienvenue') || str_contains($text, 'Bonjour'), 'fr')
    ->get();

// $result = 'fr'

⚗️ Testing

composer test

📃 Changelog

Please see the CHANGELOG.md for more information on what has changed recently.

🏅 Credits

📖 License

Please see the LICENSE.md file for more information.