-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
splitting the logic into separate classes adds some niceness for certain possible check syntaxes and opens the type for extension in future
- Loading branch information
Showing
4 changed files
with
272 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace Frej\Optional; | ||
|
||
use Frej\Optional\Exception\OptionNoneUnwrappedException; | ||
|
||
/** | ||
* @template T | ||
* @extends Option<T> | ||
*/ | ||
class None extends Option | ||
{ | ||
protected static ?self $singleton = null; | ||
|
||
protected function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @template A | ||
* @return Option<A> | ||
*/ | ||
public static function make(): Option | ||
{ | ||
if (self::$singleton === null) { | ||
self::$singleton = new self(); | ||
} | ||
return self::$singleton; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function isSome(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function isNone(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
|
||
public function isEmpty(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function unwrap(null|string|\Throwable $error = null): mixed | ||
{ | ||
if ($error instanceof \Throwable) { | ||
throw $error; | ||
} | ||
throw new OptionNoneUnwrappedException($error ?? "Option is none"); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function unwrapOr(mixed $default): mixed | ||
{ | ||
if (is_callable($default)) { | ||
return $default(); | ||
} | ||
return $default; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function unwrapOrNull(): mixed | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function unwrapInto(callable $callback, null|string|\Throwable $error = null): void | ||
{ | ||
$this->unwrap($error); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function unwrapIntoOr(callable $callback, mixed $default): void | ||
{ | ||
$this->unwrapOr($default); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function filter(mixed $predicate): Option | ||
{ | ||
return None::make(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function map(callable $transformer): Option | ||
{ | ||
return None::make(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function mapOr(callable $transformer, mixed $default): Option | ||
{ | ||
if (is_callable($default)) { | ||
return Some::make($default()); | ||
} | ||
return Some::make($default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.