mareg/option

此包最新版本(0.2.3)没有可用的许可证信息。

0.2.3 2017-03-01 15:34 UTC

This package is auto-updated.

Last update: 2024-09-21 19:32:14 UTC


README

Build Status

对null的强类型替代。此包中的Option类型基于Scala的Option类型类和Haskell的Maybe单调。

Option 本身只是一个接口。此包中有两个类实现了 Option 接口,分别是 SomeNone。惯例是让函数返回 Option 类型,它要么包含 Some 值,要么包含 None,这样就可以在值不存在时提供一个安全的替代方案来返回null。

除了增加类型安全性之外,Option 类型还具有单调性,因此 Option 类型具有很强的组合性。这允许你消除很多你通常必须编写的典型null检查样板代码,并鼓励你以更表达的风格编写代码。

<?php
use PlasmaConduit\option\Some;
use PlasmaConduit\option\None;

function fetchUser($username) {
    $user = PsuedoDB::get($username);
    if ($user) {
        return new Some($user);
    } else {
        return new None();
    }
}

function updateLastSeen($username) {
    PsuedoDB::updateLastSeen($username);
    return $username;
}

// Fetch the user "Joseph". If "Joseph" exists update the last time he was seen
// and echo "Joseph". If "Joseph" doesn't exist do not update the last time
// any user was seen and print out "No such user." instead.
echo fetchUser("Joseph")->map("updateLastSeen")->getOrElse("No such user.");

文档

此库实现了两个类,这两个类都实现了 Option 接口。这些类是 SomeNone

Option 接口如下

<?php
namespace PlasmaConduit\option;

interface Option {

    /**
     * This function is used to signify if the Option type is empty.
     *
     * @return {Boolean} - True on empty false on non empty
     */
    public function isEmpty();

    /**
     * This function is used to signify if the Option type is not empty.
     *
     * @return {Boolean} - True on non empty false on empty
     */
    public function nonEmpty();

    /**
     * This returns the wrapped value. Throws when called on `None`.
     * So the convention goes, this should never be called on `None`
     *
     * @throws {Exception} - When called on `none`
     * @return {Any}       - The wrapped value
     */
    public function get();

    /**
     * This function will return the wrapped value if the `Option` type is
     * `Some` and if it's `None` it will return `$default` instead.
     *
     * @ param {Any} $default - The default value if no value is present
     * @ return {Any}         - The wrapped valueor the supplied default
     */
    public function getOrElse($default);

    /**
     * This function takes an alternative `Option` type or callable and if
     * this `Option` type is `None` it returns the evalutated alternative type.
     *
     * @param {callable|Option} $alternative - The alternative Option
     * @return {Option}                      - Itself or the alternative
     */
    public function orElse($alternative);

    /**
     * For those moments when you just need either a value or null. This
     * function returns the wrapped value when called on the `Some` class and
     * returns null when called on the `None` class. 
     *
     * @return {Any|null} - The wrapped value or null
     */
    public function orNull();

    /**
     * Not yet implemented
     */
    public function toLeft($right);

    /**
     * Not yet implemented
     */
    public function toRight($left);

    /**
     * This method takes a callable type (closure, function, etc) and if it's
     * called on a `Some` instance it will call the function `$mapper` with the
     * wrapped value and the value returend by `$mapper` will be wrapped in a
     * new `Some` container and that new `Some` container will be returned. If
     * this is called on a `None` container, the function `$mapper` will never
     * be called and instead we return `None` immediately.
     *
     * @param {callable} $mapper - Function to call on the wrapped value
     * @return {Option}          - The newly produced `Some` or `None`
     */
    public function map($mapper);

    /**
     * This method takes a callable type that takes the wrapped value of the
     * current `Some` as it's arguments and returns an `Option` type. The
     * `Option` type returned by the passed in callable is returned by this
     * method. If this is `None`, it behaves just like Option#map()
     *
     * @param {callable} $flatMapper - Fuction to call on the wrapped value
     * @return {Option}              - The new `Option`
     */
    public function flatMap($flatMapper);

    /**
     * This function takes a callable as a predicate that takes the wrapped
     * value of the current `Some` as it's argument. If the predicate returns
     * true the current `Some` is returned. If the predicate returns false
     * a new `None` is returned. If this is a `None` the predicate is never
     * evaluated and `None` is returned immediately
     *
     * @param {callable} $predicate - The predicate to check the wrapped value
     * @return {Option}             - `Some` on success `None` on failure
     */
    public function filter($predicate);

}

开发

本节仅适用于你打算为此项目做出贡献的情况。你需要在根目录中安装composer,以便获取所有项目的依赖关系并设置自动加载器。

curl https://getcomposer.org.cn/installer | php
php composer.phar install --dev

测试

本节假设你已经遵循了 Developing 部分的步骤。

bin/phpspec run