2017 © Pedro Peláez
 

library otherwise

Functional when-otherwise conditionals for PHP.

image

skollro/otherwise

Functional when-otherwise conditionals for PHP.

  • Monday, April 23, 2018
  • by skollro
  • Repository
  • 2 Watchers
  • 6 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Functional when-otherwise conditionals

Latest Version Software License Build Status StyleCI, (*1)

This package allows to replace PHP conditionals by an easy functional match-when-otherwise syntax., (*2)

$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:, (*3)

``` bash composer require skollro/otherwise, (*4)


## 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`. ```php 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., (*5)

$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., (*6)

$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., (*7)

$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., (*8)

$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., (*9)

// 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., (*10)

The Versions

23/04 2018

dev-master

9999999-dev

Functional when-otherwise conditionals for PHP.

  Sources   Download

MIT

The Development Requires

by Simon Kollross

23/04 2018

dev-extending-otherwise

dev-extending-otherwise

Functional when-otherwise conditionals for PHP.

  Sources   Download

MIT

The Development Requires

by Simon Kollross

23/04 2018

v1.2.0

1.2.0.0

Functional when-otherwise conditionals for PHP.

  Sources   Download

MIT

The Development Requires

by Simon Kollross

05/04 2018

v1.1.1

1.1.1.0

Functional when-otherwise conditionals for PHP.

  Sources   Download

MIT

The Development Requires

by Simon Kollross

05/04 2018

v1.1.0

1.1.0.0

Functional when-otherwise conditionals for PHP.

  Sources   Download

MIT

The Development Requires

by Simon Kollross

30/03 2018

v1.0.1

1.0.1.0

Functional when-otherwise conditionals for PHP.

  Sources   Download

MIT

The Development Requires

by Simon Kollross

30/03 2018

v1.0.0

1.0.0.0

Functional when-otherwise conditionals for PHP.

  Sources   Download

MIT

The Development Requires

by Simon Kollross