2017 © Pedro Peláez
 

library option

A basic PHP implementation of the Option data type as found in Scala.

image

sassnowski/option

A basic PHP implementation of the Option data type as found in Scala.

  • Thursday, July 7, 2016
  • by ksassnowski
  • Repository
  • 1 Watchers
  • 1 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Option Type for PHP

Build Status Coverage Status Code Climate SensioLabsInsight, (*1)

A PHP implementation of the Option data type from Scala (or Maybe from Haskell if you want)., (*2)

DISCLAIMER: This package is not intented to be used in production. Its intend was to serve as a coding exercise to myself about how I would implement and Option Type in PHP. If you want to use this in production I'd recommend you use https://github.com/schmittjoh/php-option instead., (*3)

Installation

Install the package through composer:, (*4)

$ composer require sassnowski/option

That’s it! Now you can use it in your code., (*5)

function divide($a, $b)
{
    if (0 === $b)
    {
        return Option::None();
    }

    return new Option($a / $b);
}

$result = divide(10, 5);
$result->get(); // 2

$result2 = divide(10, 0);
$result->isDefined(); // false

Summary

An Option represents an optional value, or in other words a value that may not exist. It is sometimes described as a List that contains a maximum of one item., (*6)

An Option is used in places where otherwise null might be used, e.g., the result of a Database Query. A more general way to put it is: A computation might return an Option if it is not defined for some inputs., (*7)

Option::map($func)

Using Options means that a lot of code needs to be aware of this data type. In order to still be able to reuse functions that operate on unwrapped values, this class provides a map function., (*8)

The purpose of the map function is to lift a function that normally operators on regular values to now work on Option values. Formally it turns a function of type, (*9)

a -> b

into a function of type, (*10)

Option a -> Option b

Example

// Note: This example uses PHP 7 type hinting. This is in no 
// way necessary for this package to work and is simply there 
// to illustrate the types that these functions are supposed to
// operate on.

function length(string $a): int
{
    return strlen($a);
}

$length1 = (new Option("abc"))->map('length');
$length1->isDefined(); // true
$length1->get(); // 3

// The length function never gets executed, since we're 
// dealing with an undefined value.
$length2 = Option::None->map('length');
$length2->isDefined(); // false
$length2->get(); // RuntimeException

The above example lifted the function length of type string -> int into a function of type Option string -> Option int. This means that we can still write and use functions that were written without optional values in mind and simply lift them to a function that can handle Options., (*11)

An important characteristic of the map method is, that the function that is being mapped over the option will never get executed if we’re dealing with an undefined value., (*12)

Option::flatMap($func)

Todo, (*13)

Option::getOrElse($default)

Todo, (*14)

Option::orElse($alternative)

Todo, (*15)

Option::isDefined()

This function simply returns true if the value is anything other than null in which case it returns false., (*16)

License

MIT, (*17)

The Versions

07/07 2016

dev-master

9999999-dev

A basic PHP implementation of the Option data type as found in Scala.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

05/05 2016

dev-develop

dev-develop

A basic PHP implementation of the Option data type as found in Scala.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

04/05 2016

0.2.0

0.2.0.0

A basic PHP implementation of the Option data type as found in Scala.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

03/05 2016

0.1.0

0.1.0.0

A basic PHP implementation of the Option data type as found in Scala.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires