承接 skd/result 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

skd/result

Composer 安装命令:

composer require skd/result

包简介

Implementing the Result Object pattern in PHP

README 文档

README

This package allows you to work with the results of operations, including errors, in an object-oriented style, without using exceptions. For example, when using the 'Always valid domain model' approach. Result object allows you to accumulate errors and return them all at once (Notification pattern is inside). At the same time, the code becomes more understandable and logical.

Installation

composer require skd/result

Result

The Result class is an immutable generic class (using @template annotation) with 2 states: Ok and Error. The OK state means that there are no errors inside and there is some value (success operation result). The Error state means that an error or several errors were received during the operation.

Use @return Result<type_or_class> annotation to tell the IDE and static analysis tool what type value is inside;

Ok state

Use static method Result::ok to return success operation result with some value inside

use Skd\Result\Result;

// some code
return Skd\Result\Result::ok($value);

Use method Result::getValue(): T to get the value. Note that calling this method on Error state will throw an exception. To prevent that you have to check the result state by calling one of methods Result::isOk(): bool or Result::isError(): bool before.

if($result->isOk()) {
    $value = $result->getValue();
}

Error state

Use static method Result:error(Notification $errors) to return result with an error (errors). The non-empty Notification object must be passed as an argument. Passing an empty Notification object (no errors inside) will throw an exception.

use Skd\Result\Result;
use Skd\Result\Error\Notification;

// some code

$error = new Error(...);
$errors = new Notification($error);

return Skd\Result\Result::error($errors);

Use method Result::getErrors(): Notification to get an error (errors). Note that calling that method on Ok state will throw an exception. You can't change Notification object after Result object is initialized

$result = Result::error(new Notification(...))

// $errors here is a cloned copy, you cannot change the list of errors in the $result object
$errors = $result->getErrors();

Notification

Notification class is a Notification pattern realisation. It can be initialized in two ways: as an empty list or a list with one error inside

use Skd\Result\Error\Notification;
use Skd\Result\Error\Error;

// empty
$emptyNotification = new Notification();

// with one error
$error = new Error('code', 'message');
$notificationWithOneError = new Notification($error);

Use method Notification::hasErrors(): bool to check if there are error/errors inside the notification object

use Skd\Result\Error\Notification;

$errors = new Notification();
$errors->hasErrors(); // false

$errors = new Notification($error);
$errors->hasErrors(); // true

Use method Notification::hasError(Error $error): bool to check if the specific error is inside the notification object. It can be useful in Unit tests.

use Skd\Result\Error\Notification;

$errors = new Notification($error);

$errors->hasError($error); // true
$errors->hasError($anotherError); // false

Errors Accumulating

Errors can be accumulated in a Notification object.

Use Notification::addError(Error $error): void to add some error in the list

use Skd\Result\Error\Error;
use Skd\Result\Error\Notification;

$errors = new Notification();
// it can be initialized in both ways
$errors = new Notification($someError);

$errors->addError($anotherError);

Notification objects can be merged as well. It can be useful when you have to accumulate two or more results with errors. Note that only left object will be changed.

use Skd\Result\Error\Notification;

$notification = new Notification($error);
$anotherNotification = new Notification($anotherError);

$notification->merge($anotherNotification);

$notification->hasError($error); // true
$notification->hasError($anotherError); // true

// but
$anotherNotification->hasError($error); // false

Example:

Some Value Object class

use Skd\Result\Error\Error;
use Skd\Result\Error\Notification;
use Skd\Result\Result;

class SomeClass
{
    private function __construct(public readonly mixed $someField)
    {
    }

    /**
     * @return Result<SomeClass>
     */
    public static function create(mixed $someValue): Result
    {
        $errors = new Notification();
        
        // some validation
        if (...) {
            $errors->addError();
        }
        
        // another validation (multiple errors)
        if (...) {
            $errors->addError(new Error('anotherCode', 'Another error message'));
        }
        
        if ($errors->hasErrors()) {
            return Result::error($errors)
        }
        
        return Result::ok(new self($someValue));
    }
}

You can replace new Error('code', 'Error message') with static factory:

final class SomeErrorsFactory
{
  public static function invalidValue(): DomainError
  {
    return new Error('code', 'Error message');
  }
}

Unit tests

public function testInvalidValue(): void
{
  $result = SomeClass::create($invalidValue);

  $this->assertTrue($result->isError());
  $this->assertTrue($result->getErrors()->hasError(SomeErrorsFactory::invalidValue()));
}

skd/result 适用场景与选型建议

skd/result 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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