jimmyoak/optional
Composer 安装命令:
composer require jimmyoak/optional
包简介
README 文档
README
A port of Java 9's java.util.Optional improved for PHP.
Some examples
Basic usage
Example request class:
class Request { private $date; // ... constructor and getter ... }
Example code of optional:
$date = Optional::ofNullable($request->getDate()) ->map(function ($dateString) { return DateTime::createFromFormat('Y-m-d', $dateString); }) ->orElseGet(function () { return new \DateTime(); }); echo $date->format('Y-m-d');
Given this request:
$request = new Request('1992-10-07');
Program will output 1992-10-07.
If a null is specified in request:
$request = new Request(null);
Then program will output the default value, which in this case would be something like 2016-08-19
Instead of returning a default value we could just throw an exception in case no value:
$date = Optional::ofNullable($request->getDate()) ->map(function ($dateString) { return DateTime::createFromFormat('Y-m-d', $dateString); }) ->orElseThrow(new \Exception());
In case of:
$request = new Request(null);
Program will throw the specified exception, otherwise, will return specified parsed date.
or
We can chain Optionality:
$findInMemory = function () { ... return Optional::ofNullable(...); }; $findInDisc = function () { ... return Optional::ofNullable(...); }; $findRemotely = function () { ... return Optional::ofNullable(...); }; $optional = $findInMemory() ->or($findInDisc) ->or($findRemotely);
Callbacks will only execute if the last optional is empty. Example:
$findInMemory = function () { echo "Searching in memory...\n"; return Optional::empty(); }; $findInDisc = function () { echo "Searching in disc...\n"; return Optional::of("Awesome"); }; $findRemotely = function () { echo "Searching remotely..."; return Optional::of("Not so awesome"); }; $findInMemory() ->or($findInDisc) ->or($findRemotely) ->ifPresent(function ($value) { echo $value; });
Will output:
Searching in memory...
Searching in disc...
Awesome
filter
$request = new Request('1992-10-07'); $makeDateTime = function ($value) { return \DateTime::createFromFormat('Y-m-d', $value); }; $beforeNow = function (\DateTime $date) { return $date->getTimestamp() > (new \DateTime())->getTimestamp(); }; $date = Optional::ofNullable($request->getDate()) ->map($makeDateTime) ->filter($beforeNow) ->orElse(new \DateTime()); echo $date->format('Y-m-d');
Outputs (similar): 2016-08-19
With: $request = new Request('2030-01-01'); would output: 2030-01-01
With: $request = new Request(null); would output: 2016-08-19
ifPresent
Optional::ofNullable($request->getDate()) ->ifPresent(function ($value) { echo "It's present: " . $value; });
If value, it would output: It's present: 1992-10-07
Otherwise would do nothing.
ifPresentOrElse
Similar to ifPresent but with an else callback if optional is empty
Optional::ofNullable($request->getDate()) ->ifPresentOrElse(function ($value) { echo "It's present: " . $value; }, function() { echo 'No value is present'; });
If value, it would output: It's present: 1992-10-07
Otherwise, it would output: No value is present
Comparable
Returns true:
Optional::of('something')->equals(Optional::of('something'));
Returns false:
Optional::of(5)->equals(Optional::of('something'));
Returns true:
Optional::empty()->equals(Optional::empty());
Returns false:
Optional::empty()->equals(Optional::of(5));
__toString
echo (string) Optional::of('something');
Outputs: Optional[something]
echo (string) Optional::empty();
Outputs: Optional.empty
jimmyoak/optional 适用场景与选型建议
jimmyoak/optional 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.85k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2016 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「util」 「utilities」 「optional」 「jimmy」 「jimmyoak」 「optionals」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jimmyoak/optional 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jimmyoak/optional 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jimmyoak/optional 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Camilo3rd's PHP Utils Library
php optional
Collection of exception class and utilities
PHP-ClamAV library - to scan the file for viruses
Collection of methods that I or you usually use.
A collection of enhancements and utilities for the Silverstripe UserForms module
统计信息
- 总下载量: 6.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-08