2017 © Pedro Peláez
 

library result

Result object for PHP inspired by Rust

image

prewk/result

Result object for PHP inspired by Rust

  • Monday, March 19, 2018
  • by prewk
  • Repository
  • 1 Watchers
  • 5 Stars
  • 3,235 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 14 Versions
  • 7 % Grown

The README.md

PHP Result Object Build Status Coverage Status

A PHP implementation of Rust's Result type with roughly the same API., (*1)

Version information

Version 4.x.x requires PHP 8.1.0+. Make sure you match the versions for this and the Option library if you use both., (*2)

Installation

composer require prewk/result

Overview

use Prewk\Result;
use Prewk\Result\{Ok, Err};

function someApiCall(): Result {
    // ...
    if ($apiCallSuccesful) {
        return new Ok($results);
    } else {
        return new Err($error);
    }
}

function anotherApiCall(): Result {
    // ...
    if ($apiCallSuccesful) {
        return new Ok($results);
    } else {
        return new Err($error);
    }
}

// Fallback to value
$value = someApiCall()->unwrapOr(null);

// Fallback to result and throw an exception if both fail
$value = someApiCall()->orElse(function($err) {
    return anotherApiCall();
})->unwrap();

// Throw custom exception on error
$value = someApiCall()->expect(new Exception("Oh noes!"));

Helpers

Optional global helper functions exist to simplify result object construction:, (*3)

ok(); // new Prewk\Result\Ok(null);
ok($val); // new Prewk\Result\Ok($val);
err($e); // new Prewk\Result\Err($e);

Add the following to your composer.json:, (*4)

{
  "autoload": {
    "files": ["vendor/prewk/result/helpers.php"]
  }
}

API deviations from Rust

Exceptions

If an Err containing an Exception is unwrapped, that Exception will be thrown. Otherwise a generic ResultException will be thrown., (*5)

Gotchas

Note that or and and will be evaluated immediately:, (*6)

// This will call all three api calls regardless of successes/errors
$this
    ->apiCall()
    ->or(anotherApiCall())
    ->and(thirdApiCall());

See andThen and orElse for lazy evaluation., (*7)

License

MIT & Apache 2.0, (*8)

The Versions

19/03 2018

dev-master

9999999-dev

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

19/03 2018

2.0.0

2.0.0.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

19/03 2018

dev-generics

dev-generics

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

03/02 2018

1.2.0

1.2.0.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

03/02 2018

dev-snowball-with-internal-state

dev-snowball-with-internal-state

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

23/11 2017

1.1.0

1.1.0.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

04/09 2017

1.0.2

1.0.2.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

04/09 2017

1.0.1

1.0.1.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

31/07 2017

1.0.0

1.0.0.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

06/07 2017

0.0.5

0.0.5.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

28/04 2017

0.0.4

0.0.4.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

28/04 2017

0.0.3

0.0.3.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Requires

 

The Development Requires

28/04 2017

0.0.2

0.0.2.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Development Requires

19/04 2017

0.0.1

0.0.1.0

Result object for PHP inspired by Rust

  Sources   Download

(MIT and Apache-2.0)

The Development Requires