Created
February 17, 2017 10:21
-
-
Save Pierozi/78ccb81525ea5a29729a8518d63903f0 to your computer and use it in GitHub Desktop.
HoaOption
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 | |
namespace Hoa\Option; | |
interface Option | |
{ | |
public function isSome() : bool; | |
public function isNone() : bool; | |
} | |
class OptString implements Option | |
{ | |
/** | |
* @var string | |
*/ | |
protected $value; | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
public function isSome() : bool | |
{ | |
return null !== $this->value && '' !== $this->value; | |
} | |
public function isNone() : bool | |
{ | |
return null === $this->value || '' === $this->value; | |
} | |
public function __toString() : string | |
{ | |
return $this->value; | |
} | |
} | |
class MyClass | |
{ | |
/** | |
* @var OptString | |
*/ | |
protected $title; | |
public function testSome() | |
{ | |
$this->title = new OptString('Foo Bar Baz'); | |
if ($this->title->isSome()) { | |
echo "I got a string ! ", "\n", $this->title, "\n\n"; | |
} | |
} | |
public function testNone() | |
{ | |
$this->title = new OptString(null); | |
if ($this->title->isNone()) { | |
echo "Sorry I've nothing to say", "\n\n"; | |
} | |
} | |
} | |
$sut = new MyClass(); | |
$sut->testSome(); | |
$sut->testNone(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
isNone
should be defined asreturn !$this->isSome()
, simply.null
.