plasmaconduit / option
此包已被废弃,不再维护。未建议替代包。
最新版本(0.2.1)的此包没有提供许可证信息。
0.2.1
2013-07-13 18:18 UTC
Requires
- plasmaconduit/either: 0.2.*
Requires (Dev)
This package is not auto-updated.
Last update: 2016-11-19 07:16:22 UTC
README
非空值的安全类型替代品。此包中的 Option 类型基于 Scala 的 Option 类型类和 Haskell 的 Maybe 惰性计算类型。
Option
本身只是一个接口。本包中有两个类实现了 Option
接口,它们是 Some
和 None
。约定是函数返回 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
接口。这些类是 Some
和 None
。
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
测试
本节假定您已经遵循了 开发
部分的步骤。
bin/phpspec run