ciarancoza/option-result 问题修复 & 功能扩展

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

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

ciarancoza/option-result

最新稳定版本:v0.10.0

Composer 安装命令:

composer require ciarancoza/option-result

包简介

Rust-like Option and Result Classes for PHP

README 文档

README

"So you can take down ~20% of the internet, but with PHP"

This library contains two classes: Option and Result.

Option<T> represents an optional value. An option may be some or none, where some(T) contains a value and none does not.

Result<T,E> represents a success (ok(T)) or an error (err(E)).

Installation

composer require ciarancoza/option-result

Usage

Option

function findUser(int $id): Option {
    $user = User::find($id);
    return $user ? Option::Some($user) : Option::None();
}

function getUserTheme(int $userId): string {
    return findUser($userId)
        ->map(fn ($user) => $user->theme)
        ->map(fn ($theme) => strtolower($theme))
        ->unwrapOr('auto');
}

Result

function fetchOrgData(int $id): Result {
    try {
        $response = Http::get("/api/orgs/{$id}");
        if ($response->failed()) return Result::Err("API request failed: " . $response->status());
        return Result::Ok($response->json());
    } catch (Exception $e) {
        return Result::Err("Connection error: " . $e->getMessage());
    }
}

function getActiveEmails(int $orgId): array {
    return fetchOrgData($orgId)
        ->map(fn ($org) => $org->getEmails())
        ->map(fn ($emails) => $emails['active'])
        ->mapErr(fn ($error) => "Failed to get active emails: " . $error)
        ->mapErr(fn ($error) => Log::error($error))
        ->unwrapOr([]);
}

You can view the generated documentation for more usage details.

Road Map

  • Result
    • TODO
      • inspect()
      • inspectErr()
      • or()
      • tryCatch()
    • Refactor for v1

Contributing

Go for it! There are plenty of useful Option / Result features in Rust we could implement in this library.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-22