Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
meta/src/Some.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
83 lines (71 sloc)
1.31 KB
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
<?php | |
declare(strict_types=1); | |
namespace Bag2\Meta; | |
use Closure; | |
/** | |
* @implements Option<T> | |
* @template T | |
*/ | |
final class Some implements Option | |
{ | |
/** | |
* @phan-var T | |
* @psalm-var T | |
*/ | |
private $value; | |
/** | |
* @param mixed $value | |
* @phan-param T $value | |
* @psalm-param T $value | |
*/ | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
public function isDefined(): bool | |
{ | |
return true; | |
} | |
public function isEmpty(): bool | |
{ | |
return false; | |
} | |
/** | |
* @psalm-return T | |
*/ | |
public function get() | |
{ | |
return $this->value; | |
} | |
/** | |
* @template E | |
* @param mixed $else | |
* @phan-param E $else (@phan-unused-param) | |
* @psalm-param E $else | |
* @phan-return T | |
* @psalm-return T | |
* @phan-suppress PhanTemplateTypeNotUsedInFunctionReturn | |
*/ | |
public function getOrElse($else) | |
{ | |
return $this->value; | |
} | |
/** | |
* @phan-return T | |
* @psalm-return T | |
*/ | |
public function getOrNull() | |
{ | |
return $this->value; | |
} | |
/** | |
* @psalm-template E | |
* @psalm-param Closure(T): E | |
* @psalm-return Some<E> | |
* @return Some | |
*/ | |
public function map(Closure $f) | |
{ | |
return new Some($f($this->value)); | |
} | |
} |