Add toPhpOption; convert both to mostly non-static

This commit is contained in:
2024-07-28 14:15:24 -04:00
parent 7d25b9ea28
commit 5a8a41a660
5 changed files with 581 additions and 567 deletions

View File

@@ -8,6 +8,7 @@ declare(strict_types=1);
namespace BitBadger\InspiredByFSharp;
use Error;
use InvalidArgumentException;
/**
@@ -25,7 +26,7 @@ use InvalidArgumentException;
*/
readonly class Option
{
/** @var ?T $value The value for this option */
/** @var T|null $value The value for this option */
private mixed $value;
/**
@@ -49,6 +50,138 @@ readonly class Option
};
}
/**
* Does this option have a `None` value?
*
* @return bool True if the option is `None`, false if it is `Some`
*/
public function isNone(): bool
{
return is_null($this->value);
}
/**
* Does this option have a `Some` value?
*
* @return bool True if the option is `Some`, false if it is `None`
*/
public function isSome(): bool
{
return !$this->isNone();
}
/**
* Get the value, or a default value, from an option
*
* @param T $default The default value to return if the option is `None`
* @return T The `Some` value, or the default value if the option is `None`
*/
public function getOrDefault(mixed $default): mixed
{
return $this->value ?? $default;
}
/**
* Get the value, or return the value of a callable function
*
* @template U The return type of the callable provided
* @param callable(): U $f The callable function to use for `None` options
* @return T|mixed The value if `Some`, the result of the callable if `None`
*/
public function getOrCall(callable $f): mixed
{
return $this->value ?? $f();
}
/**
* Map this optional value to another value
*
* @template U The type of the mapping function
* @param callable(T): U $f The mapping function
* @return Option<U> A `Some` instance with the transformed value if `Some`, `None` otherwise
*/
public function map(callable $f): self
{
return $this->isSome() ? self::Some($f($this->get())) : $this;
}
/**
* Execute a function on the value (if it exists)
*
* @param callable(T): void $f The function to call
*/
public function iter(callable $f): void
{
if ($this->isSome()) {
$f($this->value);
}
}
/**
* Transform an option into `None` if it does not match the given function
*
* @param callable(T): bool $f The filter function to run
* @return Option<T> The option, if it was `Some` and the function returned true; `None` otherwise
*/
public function filter(callable $f): self
{
return match (true) {
$this->isNone() => $this,
default => $f($this->value) ? $this : self::None(),
};
}
/**
* Does the option have the given value?
*
* @param T $value The value to be checked
* @param bool $strict True for strict equality (`===`), false for loose equality (`==`)
* @return bool True if the value matches, false if not; `None` always returns false
*/
public function is(mixed $value, bool $strict = true): bool
{
return match (true) {
$this->isNone() => false,
default => $strict ? $this->value === $value : $this->value == $value,
};
}
/**
* Safely retrieve the optional value as a nullable value
*
* @return T|null The value for `Some` instances, `null` for `None` instances
*/
public function unwrap(): mixed
{
return $this->value;
}
/**
* Tap into the `Result` for a secondary action, returning the result
*
* @param callable(Option<T>): mixed $f The function to run (return value is ignored)
* @return Option<T> The same option provided
*/
public function tap(callable $f): Option
{
$f($this);
return $this;
}
/**
* Convert this to a PhpOption option
*
* @return mixed An option from the PhpOption library
*/
public function toPhpOption(): mixed
{
return match (true) {
$this->isNone() && class_exists('PhpOption\None') => call_user_func('PhpOption\None::create'),
class_exists('PhpOption\Some') => call_user_func('PhpOption\Some::create', $this->value),
default => throw new Error('PhpOption types could not be found'),
};
}
/**
* Create a `Some` option with the given value
*
@@ -83,136 +216,8 @@ readonly class Option
{
return match (true) {
is_object($value) && is_a($value, 'PhpOption\Option') =>
$value->isDefined() ? self::Some($value->get()) : self::None(),
$value->isDefined() ? self::Some($value->get()) : self::None(),
default => new self($value),
};
}
/**
* Does this option have a `None` value?
*
* @param Option<T> $it The option in question
* @return bool True if the option is `None`, false if it is `Some`
*/
public static function isNone(Option $it): bool
{
return is_null($it->value);
}
/**
* Does this option have a `Some` value?
*
* @param Option<T> $it The option in question
* @return bool True if the option is `Some`, false if it is `None`
*/
public static function isSome(Option $it): bool
{
return !self::isNone($it);
}
/**
* Get the value, or a default value, from an option
*
* @param T $default The default value to return if the option is `None`
* @param Option<T> $it The option in question
* @return T The `Some` value, or the default value if the option is `None`
*/
public static function defaultValue(mixed $default, Option $it): mixed
{
return self::isSome($it) ? $it->get() : $default;
}
/**
* Get the value, or return the value of a callable function
*
* @template U The return type of the callable provided
* @param callable(): U $f The callable function to use for `None` options
* @param Option<T> $it The option in question
* @return T|mixed The value if `Some`, the result of the callable if `None`
*/
public static function getOrCall(callable $f, Option $it): mixed
{
return self::isSome($it) ? $it->get() : $f();
}
/**
* Map this optional value to another value
*
* @template U The type of the mapping function
* @param callable(T): U $f The mapping function
* @param Option<T> $it The option in question
* @return Option<U> A `Some` instance with the transformed value if `Some`, `None` otherwise
*/
public static function map(callable $f, Option $it): self
{
return self::isSome($it) ? self::Some($f($it->get())) : self::None();
}
/**
* Execute a function on the value (if it exists)
*
* @param callable(T): void $f The function to call
* @param Option<T> $it The option in question
*/
public static function iter(callable $f, Option $it): void
{
if (self::isSome($it)) {
$f($it->get());
}
}
/**
* Transform an option into `None` if it does not match the given function
*
* @param callable(T): bool $f The filter function to run
* @param Option<T> $it The option in question
* @return Option<T> The option, if it was `Some` and the function returned true; `None` otherwise
*/
public static function filter(callable $f, Option $it): self
{
return match (true) {
self::isNone($it) => self::None(),
default => $f($it->get()) ? self::Some($it->get()) : self::None(),
};
}
/**
* Does the option have the given value?
*
* @param T $value The value to be checked
* @param Option<T> $it The option in question
* @param bool $strict True for strict equality (`===`), false for loose equality (`==`)
* @return bool True if the value matches, false if not; `None` always returns false
*/
public static function is(mixed $value, Option $it, bool $strict = true): bool
{
return match (true) {
self::isNone($it) => false,
default => $strict ? $it->value === $value : $it->value == $value,
};
}
/**
* Safely retrieve the optional value as a nullable value
*
* @param Option<T> $it The option in question
* @return ?T The value for `Some` instances, `null` for `None` instances
*/
public static function unwrap(Option $it): mixed
{
return self::isSome($it) ? $it->get() : null;
}
/**
* Tap into the `Result` for a secondary action, returning the result
*
* @param callable(Option<T>): mixed $f The function to run (return value is ignored)
* @param Option<T> $it The option against which the function should be run
* @return Option<T> The same option provided
*/
public static function tap(callable $f, Option $it): Option
{
$f($it);
return $it;
}
}