jefvda/php-result-monad 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

jefvda/php-result-monad

Composer 安装命令:

composer require jefvda/php-result-monad

包简介

A 'Result' monad type for php

README 文档

README

Overview

The PHP Result Monad library provides a simple and effective way to handle the results of actions, offering an alternative approach to exception handling without the need for throwing exceptions. The core component of this library is the Result class, which encapsulates the outcome of an action and can be created from either a successful value or an exception.

🔗 Installation

You can install this package via Composer:

composer require jefvda/php-result-monad

Result Class

Factory Methods

createFromValue

/**
 * @param mixed $value The value associated with the result.
 * @return Result The successful Result which includes the value.
 */
public static function createFromValue(mixed $value): self

-> Creates a successful Result instance with the provided value.

createFromException

/**
 * @param Exception $exception The exception that explains why the result was not successful.
 * @return Result The not successful Result that includes the exception.
 */
public static function createFromException(Exception $exception): self

-> Creates a not successful Result instance with the provided exception.

Public Methods

Getters

  • getValue -> Returns the value associated with the result.
  • getException -> Returns the exception associated with the result
  • isSuccess -> Returns a boolean indicating whether the action was successful.

match

/**
 * @param callable $valueCallback The callback for successful results.
 * @param callable $exceptionCallback The callback for unsuccessful results.
 * @return mixed The return value of the callback that has been called.
 */
public function match(callable $valueCallback, callable $exceptionCallback): mixed

-> Allows matching on the result, executing the appropriate callback based on success or failure.

Usage

Here's a brief example demonstrating the basic usage of the Result class:

// Create a successful result with a value
$successResult = Result::createFromValue('Hello, Result!');

// Create an unsuccessful result with an exception
$exception = new \Exception('Something went wrong.');
$failureResult = Result::createFromException($exception);

// Match on the results
$successMessage = $successResult->match(
    fn ($value) => 'Success: ' . $value,
    fn ($exception) => 'Failure: ' . $exception,
);

$failureMessage = $failureResult->match(
    fn ($value) => 'Success: ' . $value,
    fn ($exception) => 'Failure: ' . $exception,
);

echo $successMessage; // Will display 'Success: Hello, Result!' -> as the `Result` was successful
echo $failureMessage; // Will display 'Failure: Something went wrong.' -> as the `Result` was not successful

jefvda/php-result-monad 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-13