承接 texthtml/maybe 相关项目开发

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

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

texthtml/maybe

最新稳定版本:v0.8.0

Composer 安装命令:

composer require texthtml/maybe

包简介

Option & Result types

README 文档

README

Help yourself by not returning null, false, -1 or throwing exception for everything by using Option & Result instead.

Using those makes it harder to make mistake, make it easy to do common operations on unknown returned values and can help static analysis tool detect incorrect logic in PHP code.

Installation

composer req texthtml/maybe

Documentation

Read the documentation full API description, detailed explainations and usages.

Usage

Option

TH\Maybe\Option is a type that represents an optional value: every Option is either Some and contains a value, or None, and does not.

/**
 * @return Option<float>
 */
function divide(float $numerator, float $denominator): Option {
    return match ($denominator) {
        0.0 => Option\none(),
        default => Option\some($numerator / $denominator),
    };
}

// The return value of the function is an option
$result = divide(2.0, 3.0);

// Pattern match to retrieve the value
if ($result->isSome()) {
    // The division was valid
    echo "Result: {$result->unwrap()}";
} else {
    // The division was invalid
    echo "Cannot divide by 0";
}

Result

TH\Maybe\Result is a type that represents either success (Ok), containing the result of an operation or failure (Err), containing the reason of the failure.

/**
 * @return Result<int,string>
 */
function parse_version(string $header): Result {
    return match ($header[0] ?? null) {
        null => Result\err("invalid header length"),
        "1" => Result\ok(1),
        "2" => Result\ok(2),
        default => Result\err("invalid version"),
    };
}

$version = parse_version("1.x");
if ($version->isOk()) {
    echo "working with version: {$version->unwrap()}";
} else {
    echo "error parsing header: {$version->unwrapErr()}";
}
// @prints working with version: 1

统计信息

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

GitHub 信息

  • Stars: 18
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-17