定制 samueldmonteiro/result-type 二次开发

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

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

samueldmonteiro/result-type

最新稳定版本:1.1.0

Composer 安装命令:

composer require samueldmonteiro/result-type

包简介

Result Type for PHP

README 文档

README

Maintainer PHP from Packagist Software License Total Downloads

The result-type library introduces the Result Pattern to PHP, inspired by languages like Rust and Go. It provides a structured way to handle success and error outcomes explicitly, reducing reliance on exceptions. This approach makes your code more predictable, maintainable, and clear, ensuring every operation's result is properly managed.

A biblioteca result-type traz o padrão Result para o PHP, inspirada em linguagens como Rust e Go. Ela oferece uma maneira estruturada de lidar com sucessos e erros de forma explícita, reduzindo a dependência de exceções. Essa abordagem torna seu código mais previsível, manutenível e claro, garantindo que o resultado de cada operação seja devidamente tratado.

Installation

composer require samueldmonteiro/result-type

Documentation

Generics

Since PHP does not yet have native support for generics, we can solve this problem using dockblocks, to define what type of data is being returned when the operation is successful:

Como o PHP ainda não tem suporte nativo para generics, podemos resolver esse problema usando dockblocks para definir que tipo de dado será retornado quando a operação for bem-sucedida:

Ex:

/**
 * @return Result<float>
 */
function safeDivide(float $value): Result
{
    return $value;
}

Usage

Using Class Instance:

<?php

use Samueldmonteiro\Result\Error;
use Samueldmonteiro\Result\Result;
use Samueldmonteiro\Result\Success;

/**
 * @return Result<float>
 */
function safeDivide(float $a, float $b): Result
{
    if ($b === 0.0) {
        // Return an error if division by zero
        return new Error('Division by zero is not allowed.');
    }

    return new Success($a / $b, 'Division successful.');
}

// Example usage
$result = safeDivide(10, 2);

if ($result->isSuccess()) {
    echo $result->getMessage() . PHP_EOL; 
    echo "Result: " . $result->getValue() . PHP_EOL;

} else {
    echo "Error: " . $result->getErrorMessage() . PHP_EOL; 
}

Using Static Methods:

/**
 * @return Result<float>
 */
function safeDivide(float $a, float $b): Result
{
    if ($b === 0.0) {
        return Result::error('Division by zero is not allowed.');
    }

    return Result::success(($a / $b, 'Division successful.');
}

Other Methods:

<?php

use Samueldmonteiro\Result\Success;

$result = New Error('error in application', 500, 'SERVER_ERROR');

if (!$result->isError()) {
    echo $result->getMessage() . PHP_EOL; 
    echo "Result: " . $result->getValue() . PHP_EOL;

} else {
    echo "Error: " . $result->getErrorMessage() . PHP_EOL;
    echo "Error Code: " . $result->getErrorCode() . PHP_EOL; 
    echo "Error Type: " . $result->getErrorType() . PHP_EOL; 

}

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

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