Option / Some / None
The Option type models an optional value. Option is the abstract base class with the static factories, Some holds a value, and None represents the absence of one. All three implement Raxos\Foundation\Contract\OptionInterface.
See the Option type concept page for a guided introduction.
Option
namespace Raxos\Foundation\Option;
abstract readonly class Option implements OptionInterfaceBase class for optional values, extended by Some and None. It provides the static factories used to build options.
Static methods
public static function some(mixed $value): OptionInterfaceReturns a new Some wrapping the given value.
public static function none(): OptionInterfaceReturns the shared None instance.
public static function fromValue(mixed $value, mixed $none = null): OptionInterfaceWraps a value in Some, or returns None if the value is identical to $none. If the value is already an option, it is returned as is.
public static function fromCallable(callable $fn, mixed $none = null): OptionInterfaceCalls the function and wraps the result via fromValue().
OptionInterface
The contract implemented by every option. Each method behaves differently for Some and None.
public function get(): mixed;
public function getOrElse(mixed $fallback): mixed;
public function getOrInvoke(callable $fn): mixed;
public function getOrThrow(Throwable|callable $err): mixed;
public function map(callable $map): OptionInterface;
public function filter(callable $predicate): OptionInterface;
public function accept(mixed $value): OptionInterface;
public function reject(mixed $value): OptionInterface;
public function orElse(OptionInterface|callable $fallback): OptionInterface;
public function orThrow(Throwable|callable $err): OptionInterface;Some
namespace Raxos\Foundation\Option;
final readonly class Some extends Option implements DebuggableInterfaceRepresents an option that holds a value. Every accessor returns the wrapped value.
public function get(): mixedReturns the wrapped value.
public function map(callable $map): OptionApplies the callable to the value and wraps the result in a new Some.
public function filter(callable $predicate): OptionInterfaceReturns itself if the predicate is true, otherwise None.
None
namespace Raxos\Foundation\Option;
final readonly class None extends Option implements DebuggableInterfaceRepresents the absence of a value. Most accessors throw or fall back to a supplied default. None is created and shared through Singleton, so every Option::none() returns the same object.
public function get(): mixedThrows OptionException::noValue().
public function getOrElse(mixed $fallback): mixedReturns the given fallback value.
public function getOrThrow(Throwable|callable $err): mixedThrows the given exception, or the result of the given callable.
OptionException
namespace Raxos\Foundation\Option;
final class OptionException extends ExceptionOptionException::noValue(): thrown byNone::get().OptionException::notAnOption(): thrown byNone::orElse()when a fallback callable does not return anOptionInterface.
Example
<?php
declare(strict_types=1);
use Raxos\Foundation\Option\Option;
function findUser(int $id): OptionInterface
{
return Option::fromValue(lookup($id));
}
$name = findUser(1)
->map(static fn(array $user): string => $user['name'])
->getOrElse('Guest');