定制 sassnowski/option 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sassnowski/option

Composer 安装命令:

composer require sassnowski/option

包简介

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

README 文档

README

Build Status Coverage Status Code Climate SensioLabsInsight

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

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.

Installation

Install the package through composer:

$ composer require sassnowski/option

That’s it! Now you can use it in your code.

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.

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.

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.

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

a -> b

into a function of type

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.

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.

Option::flatMap($func)

Todo

Option::getOrElse($default)

Todo

Option::orElse($alternative)

Todo

Option::isDefined()

This function simply returns true if the value is anything other than null in which case it returns false.

License

MIT

sassnowski/option 适用场景与选型建议

sassnowski/option 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sassnowski/option 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sassnowski/option 我们能提供哪些服务?
定制开发 / 二次开发

基于 sassnowski/option 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-03