Something went wrong. Try again.
Rust-style Option and Result Classes for PHP
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263<?php
namespace Ciarancoza\OptionResult;
use Ciarancoza\OptionResult\Exceptions\UnwrapErrException;use Ciarancoza\OptionResult\Exceptions\UnwrapOkException;
/** * Result<T, E> represents a success (`ok`) or an error (`err`) * * @template T * @template E */class Result{ /** * Creates an `ok` result * * @param T $value * @return Result<T,never> */ public static function Ok(mixed $value = true): static { return new static($value, true); }
/** * Creates an `err` result * * @param E $value * @return Result<never,E> */ public static function Err(mixed $value): static { return new static($value, false); }
/** @param T $value */ private function __construct( protected mixed $value, protected bool $isOk, ) {}
/** Returns `true` if the result is an `ok` result. */ public function isOk(): bool { return $this->isOk; }
/** Returns `true` if the result is an `err` result. */ public function isErr(): bool { return ! $this->isOk(); }
/** * Returns `$and` if `ok`, otherwise returns the current `err` * * @template V * * @param Result<V,E> $and * @return Result<V,E> */ public function and(self $and): Result { if ($this->isErr()) { return Result::Err($this->unwrapErr()); }
return $and; }
/** * Calls `$then` on contained value if `ok`, otherwise returns the current `err` * * @template U * * @param callable(T): Result<U,E> $then Function to transform the value * @return Result<U,E> */ public function andThen(callable $then): Result { if ($this->isErr()) { return Result::Err($this->unwrapErr()); }
return $then($this->unwrap()); }
/** * Throws UnwrapErrException with a custom message if `err`, otherwise returns the inner value * * @return T * * @throws UnwrapErrException */ public function expect(string $msg): mixed { if ($this->isErr()) { throw new UnwrapErrException($msg); }
return $this->unwrap(); }
/** * Throws UnwrapOkException with a custom message if `ok`, otherwise returns the inner error value * * @return E * * @throws UnwrapOkException */ public function expectErr(string $msg): mixed { if ($this->isOk()) { throw new UnwrapOkException($msg); }
return $this->unwrapErr(); }
/** * Returns `Some(T)` if `ok`, or `None` if `err` * * @return Option<T> */ public function getOk(): Option { if ($this->isErr()) { return Option::None(); }
return Option::Some($this->value); }
/** * Returns `Some(E)` if `err`, or `None` if `ok` * * @return Option<E> */ public function getErr(): Option { if ($this->isOk()) { return Option::None(); }
return Option::Some($this->value); }
/** * Returns the contained value if `ok`, otherwise throws UnwrapErrException * * @return T The contained value * * @throws UnwrapErrException */ public function unwrap(): mixed { if ($this->isErr()) { throw new UnwrapErrException; }
return $this->value; }
/** * Returns the contained value if `err`, otherwise throws UnwrapOkException * * @return E The contained error value * * @throws UnwrapOkException */ public function unwrapErr(): mixed { if ($this->isOk()) { throw new UnwrapOkException; }
return $this->value; }
/** * Returns the contained `ok` value or a provided default. * * @param V $or * @return T|V */ public function unwrapOr(mixed $or): mixed { if ($this->isOk()) { return $this->unwrap(); }
return is_callable($or) ? $or() : $or; }
/** * Returns the contained `ok` value or computes from closure with error value * * @param callable(E): V $fn * @return T|V */ public function unwrapOrElse(callable $fn): mixed { if ($this->isOk()) { return $this->unwrap(); }
return $fn($this->value); }
/** * If `ok`, transform the value with `$fn` * * @template U * * @param callable(T): U $fn Function to transform the value * @return Result<U,E> */ public function map(callable $fn): Result { if ($this->isErr()) { return Result::Err($this->value); }
return Result::Ok($fn($this->value)); }
/** * Calls `fn` on a contained value if `ok`, or returns $or if `err` * * @template V $or * @template U * * @param callable(T): U $fn Function to transform the value * @return V|U */ public function mapOr(mixed $or, callable $fn): mixed { return match (true) { $this->isOk() => $fn($this->unwrap()), $this->isErr() => is_callable($or) ? $or() : $or, }; }
/** * If `err`, transform the error value with `$fn` * * @template U * * @param callable(E): U $fn Function to transform the value * @return Result<T,U> */ public function mapErr(callable $fn): Result { if ($this->isOk()) { return Result::Ok($this->value); }
return Result::Err($fn($this->unwrapErr())); }}