prewk/result
Composer 安装命令:
composer require prewk/result
包简介
Result object for PHP inspired by Rust
README 文档
README
A PHP implementation of Rust's Result type with roughly the same API.
Version information
Version 4.x.x requires PHP 8.1.0+. Make sure you match the versions for this and the Option library if you use both.
Installation
composer require prewk/result
Overview
use Prewk\Result; use Prewk\Result\{Ok, Err}; function someApiCall(): Result { // ... if ($apiCallSuccesful) { return new Ok($results); } else { return new Err($error); } } function anotherApiCall(): Result { // ... if ($apiCallSuccesful) { return new Ok($results); } else { return new Err($error); } } // Fallback to value $value = someApiCall()->unwrapOr(null); // Fallback to result and throw an exception if both fail $value = someApiCall()->orElse(function($err) { return anotherApiCall(); })->unwrap(); // Throw custom exception on error $value = someApiCall()->expect(new Exception("Oh noes!"));
Helpers
Optional global helper functions exist to simplify result object construction:
ok(); // new Prewk\Result\Ok(null); ok($val); // new Prewk\Result\Ok($val); err($e); // new Prewk\Result\Err($e);
Add the following to your composer.json:
{
"autoload": {
"files": ["vendor/prewk/result/helpers.php"]
}
}
API deviations from Rust
Exceptions
If an Err containing an Exception is unwrapped, that Exception will be thrown. Otherwise a generic ResultException will be thrown.
Gotchas
Note that or and and will be evaluated immediately:
// This will call all three api calls regardless of successes/errors $this ->apiCall() ->or(anotherApiCall()) ->and(thirdApiCall());
See andThen and orElse for lazy evaluation.
License
MIT & Apache 2.0
prewk/result 适用场景与选型建议
prewk/result 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 944.08k 次下载、GitHub Stars 达 121, 最近一次更新时间为 2017 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 prewk/result 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 prewk/result 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 944.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 121
- 点击次数: 23
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: (MIT or Apache-2.0) aac58a55dd66e99fb34345873f870dc8bfa07a54
- 更新时间: 2017-04-19