Skip to content
Functional when-otherwise conditionals for PHP
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitattributes
.gitignore
.styleci.yml
.travis.yml
LICENSE.md
README.md
composer.json
phpunit.xml

README.md

Functional when-otherwise conditionals

Latest Version Software License Build Status StyleCI

This package allows to replace PHP conditionals by an easy functional match-when-otherwise syntax.

$result = match([1, 2, 3])
    ->when(function ($value) {
        return in_array(2, $value);
    }, '2 was found')
    ->otherwise('2 was not found');

// $result is "2 was found"

Install

You can install this package via composer:

composer require skollro/otherwise

Usage

Every conditional match consists out of one or multiple when and one otherwise to provide values for each path.

match($value, ...$params): Match

This package provides a helper function match. The first value is the value to match against. You can pass a variable amount of $params which are passed to every callable that resolves a $result.

use Skollro\Otherwise\Match;
use function Skollro\Otherwise\match;

$match = match($value);
$match = Match::value($value);

when($condition, $result): Match

$condition is a bool, a callable or a value to compare against (using ==). $result takes either some value or a callable for lazy evaluation. More specific conditions have to be defined first because the first match is the final result.

$result = match('A', 'Some value')
    ->when('B', function ($value) {
        return "{$value} is always false: A != B";
    })
    ->when(true, function ($value, $param) {
        return "This is always true ({$param})";
    })
    ->when(function ($value) {
        return strlen($value) == 1;
    }, 'This is not the first match')
    ->otherwise('B');

// $result is "This is always true (Some value)" because it's the first condition that evaluates to true

whenInstanceOf($type, $result): Match

This is just a shortcut method for $value instanceof A. $type is anything that can be on the left side of an instanceof operator. $result takes either some value or a callable for lazy evaluation. More specific conditions have to be defined first because the first match is the final result.

$result = match(new A)
    ->whenInstanceOf(B::class, 'This is false')
    ->whenInstanceOf(A::class, 'This is true')
    ->when(function ($value) {
        return $value instanceof A;
    }, 'This is not the first match')
    ->otherwise('C');

// $result is "This is true" because it's the first condition that evaluates to true

whenThrow($condition, $result): Match

$condition is a bool, a callable or a value to compare against (using ==). $result takes either an exception class name, an exception instance or a callable that returns an exception. More specific conditions have to be defined first because the first match throws the exception instantly.

$result = match('A')
    ->when(false, 'This is always false')
    ->whenThrow('A', Exception::class)
    ->otherwise('C');

// Exception is thrown

otherwise($value)

$value is of type callable or some value. Supplies the default value if no when has evaluated to true before.

$result = match('A')
    ->when(false, 'This is always false')
    ->otherwise('B');

// $result is "B"

$result = match('A', 'Some value')
    ->when(false, 'This is always false')
    ->otherwise(function ($value, $param) {
        return "{$value} ({$param})";
    });

// $result is "A (Some value)"

$result = match('A')
    ->when(false, 'This is always false')
    ->otherwise('strlen');

// $result is 1

otherwiseThrow($value)

Throws an exception if no when has evaluated to true before. It takes an exception class name, an exception instance or a callable that returns an exception.

// recommended: an instance of the exception is only created if needed
$result = match('A')
    ->when(false, 'This is always false')
    ->otherwiseThrow(Exception::class);

$result = match('A', 'Some value')
    ->when(false, 'This is always false')
    ->otherwiseThrow(function ($value, $param) {
        throw new Exception("Message {$value} ({$param})");
    });

// not recommended
$result = match('A')
    ->when(false, 'This is always false')
    ->otherwiseThrow(new Exception);

License

The MIT License (MIT). Please see License File for more information.

You can’t perform that action at this time.