Files
core/src/Option/None.php
T

119 lines
1.8 KiB
PHP
Raw Normal View History

2026-07-01 18:35:39 +03:00
<?php
namespace Modules\Core\Option;
2026-08-31 13:16:13 +03:00
use RuntimeException;
use Exception;
2026-07-01 18:35:39 +03:00
use EmptyIterator;
/**
* @extends Option<mixed>
*/
final class None extends Option
{
/** @var None|null */
private static $instance;
/** @return None */
public static function create(): self
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function get()
{
2026-08-31 13:16:13 +03:00
throw new RuntimeException("None has no value.");
2026-07-01 18:35:39 +03:00
}
public function getOrCall($callable)
{
return $callable();
}
public function getOrElse($default)
{
return $default;
}
2026-08-31 13:16:13 +03:00
public function getOrThrow(Exception $ex)
2026-07-01 18:35:39 +03:00
{
throw $ex;
}
public function isEmpty(): bool
{
return true;
}
public function isDefined(): bool
{
return false;
}
public function orElse(Option $else)
{
return $else;
}
public function ifDefined($callable)
{
// no-op
}
public function forAll($callable)
{
return $this;
}
public function map($callable)
{
return $this;
}
public function flatMap($callable)
{
return $this;
}
public function filter($callable)
{
return $this;
}
public function filterNot($callable)
{
return $this;
}
public function select($value)
{
return $this;
}
public function reject($value)
{
return $this;
}
public function getIterator(): EmptyIterator
{
return new EmptyIterator();
}
public function foldLeft($initialValue, $callable)
{
return $initialValue;
}
public function foldRight($initialValue, $callable)
{
return $initialValue;
}
private function __construct() {}
}