Last active
June 13, 2016 11:07
-
-
Save taueres/66a1d767e6aff5a76e7e8518dbca503c to your computer and use it in GitHub Desktop.
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 | |
class Optional | |
{ | |
private $value; | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
public function map(callable $func) | |
{ | |
if (null === $this->value) { | |
return $this; | |
} | |
$result = $func($this->value); | |
return new Optional($result); | |
} | |
public function flatMap(callable $func) | |
{ | |
if (null === $this->value) { | |
return $this; | |
} | |
return $func($this->value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment