定制 tekord/php-result 二次开发

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

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

tekord/php-result

最新稳定版本:v0.9

Composer 安装命令:

composer require tekord/php-result

包简介

Result object for PHP inspired by the Rust programming language

README 文档

README

Result object for PHP inspired by the Rust programming language.

This object is useful when you want your errors to be returned instead of being thrown. Several helper functions make it easy to use.

PHP Version Support Packagist version License

Installation

Install the package via Composer:

composer require tekord/php-result

Usage

Example:

class ErrorInfo { public $code; public $message; public $context; public function __construct($code, $message, $context = []) { $this->code = $code; $this->message = $message; $this->context = $context; } } function createOrder(User $user, $products): Result { if (!$user->isVerified()) return Result::fail( new ErrorInfo("unverified_user", "Unverified users are not allowed to order", [ 'user' => $user ]) ); if ($user->hasDebts()) return Result::fail( new ErrorInfo("user_has_debts", "Users with debts are not allowed to order new items", [ 'user' => $user ]) ); if ($products->isEmpty()) return Result::fail( new ErrorInfo("no_products", "Products cannot be empty") ); // Create a new order here... return Result::success($newOrder); } // ... $createOrderResult = createOrder($user, $productsFromCart); // This will throw a panic exception if the result contains an error $newOrder = $createOrderResult->unwrap(); // - OR - // You can check if the result is OK and make a decision on it if ($createOrderResult->isOk()) { $newOrder = $createOrderResult->ok; // ... } else { throw new DomainException($createOrderResult->error->message); } // - OR - // Get a default value if the result contains an error $newOrder = $createOrderResult->unwrapOrDefault(new Order());

You can extend the Result class to override the exception class (note the phpDoc):

/**  * @tempate OkType  * @tempate ErrorType  *  * @extends Result<OkType, ErrorType>  */ class CustomResult extends Result { static $panicExceptionClass = DomainException::class; }

Testing

composer test

Security

If you discover any security related issues, please email cyrill@tekord.space instead of using the issue tracker.

统计信息

  • 总下载量: 11.77k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固