xtompie/result
Composer 安装命令:
composer require xtompie/result
包简介
Functional handling failures, input errors
README 文档
README
Simple class for handling failures, input errors
use Xtompie\Result\Result; $rand = rand(0, 1); $result = $rand%2 == 0 ? Result::ofSuccess($rand) : Result::ofErrorMsg("the number $rand is not even"); if ($result->success()) { echo "OK: {$result->value()}"; } else if ($result->fail()) { echo "Error: {$result->errors()->first()->message()}"; }
Requiments
PHP >= 8.0
Installation
Using composer
composer require xtompie/result
Docs
Result is in state success or fail. Success optionaly contains value. Fail optionaly contains errors. Error can have message, key and space. Message is for human readable text. Key is for error idetify for programs. Space is for idetify property or path of error.
Creation
Result::ofSuccess(); // success without value Result::ofValue(mixed $value); // success with value Result::ofFail(); // fail without errors Result::ofError(Error $error); // fail with one error Result::ofErrorMsg(?string $message, ?string $key = null, ?string $space = null); // fail with one error Result::ofErrors(ErrorCollection $errors); // fail with errors Result::ofCombine(Result ...$results): // combined many results, fail when any of results fail // when fail errors are merged // when success, first value is used
Usage example
namespace App\User\Application\Service\CreateUser; use Xtompie\Result\Result; class CreateUserResult extends Result { public static function ofUserId(strign $userId): static { return parent::ofValue($userId); } public function userId(): string { return $this->value(); } } class CreateUserService { public function __invoke(string $email): Result { if (strlen($email) === 0) { return Result::ofErrorMsg('Email required', 'required', 'email'); } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { return Result::ofErrorMsg('Invalid email', 'email', 'email'); } if ($this->dao->exists('user', ['email' => $email])) { return Result::ofErrorMsg('Email exists', 'exists', 'email'); } $id = $this->dao->insert('user', ['email' => $email]); return Result::ofUserId($id); } } namespace App\User\UI\Controller; use App\User\Application\Service\CreateUser\CreateUserService; use App\User\UI\Request\Request; class ApiUsersPostController { public function __construct( protected CreateUserService $createUserService, protected Request $request, ) {} public function __invoke() { $result = ($this->createUserService)((string)$this->request->input('email')); if ($result->fail()) { return [ 'success' => false, 'error' => [ 'msg' => $result->errors()->first()?->message(), 'key' => $result->errors()->first()?->key(), 'space' => $result->errors()->first()?->space(), ], ]; } return [ 'success' => true, 'body' => [ 'id' => $result->userId(), ], ]; } }
xtompie/result 适用场景与选型建议
xtompie/result 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.49k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「error」 「result」 「fail」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xtompie/result 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xtompie/result 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xtompie/result 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Get more out of your test suite by getting it to work with you when tests fail. Screenshots and more. Works with Goutte and MinkExtension.
Retry fragile operations in case of failure with different delay strategies and possibility to skip/terminate execution
Result type capturing success or failure as a value for controlled error handling
A PHPUnit result printer, variant of the TestDoxCli Printer, that reduces the test failure message size for better readability
Rust like enum Result and Option.
Bundle extension that installs and loads Semantic MediaWiki and associated extensions
统计信息
- 总下载量: 8.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-01