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

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

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

jefvda/php-result-monad

最新稳定版本:1.0.2

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

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固