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

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

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

pldin601/php-result

Composer 安装命令:

composer require pldin601/php-result

包简介

Abstraction that represends ok/fail result primitives

README 文档

README

Build Status Code Climate Test Coverage Issue Count

Result is an abstraction that can be used for returning and propagating errors. Result can be ok, representing success and containing a value, or fail, representing error and containing an error value.

Inspired by Rust's module std::result.

Functions

use Result as R;

R\ok('foo');
R\fail($value);

R\resultify($callable, ...$args);
R\notNull($callable, ...$args);
R\tryCatch($callable, $exceptionTransformCallable, ...$args);

R\isOk($result);
R\isFail($result);

R\ifOk($result, $callable);
R\ifFail($result, $callable);

R\getOrThrow($result, $exceptionClass);

R\bind($result, $callable);
R\pipeline(...$callables);

Pipeline example

use Result as R;


$readFile = function($filename) {
    return R\with($filename, 'file_exists', 'file_get_contents', function () {
        return "Can't read the file.";
    });
}

$proceedFile = function($content) {
    $transform = function ($exception) {
        return $exception->getMessage();
    };

    return R\tryCatch('doSomethingWithContent', $transform, $content);
}

$saveFile = function($filename) {
    return function ($content) use ($filename) {
        $bytesWritten = file_put_contents($filename, $content);

        return $bytesWritten === false
            ? R\fail("Can't save the file!")
            : R\ok();
    }
}

$pipeline = R\pipeline($readFile, $proceedFile, $saveFile('/tmp/output_file'));

$result = $pipeline('/tmp/input_file');

R\ifOk($result, function () {
    echo 'File successfully saved.';
});

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固