Skip to content

Instantly share code, notes, and snippets.

@Pierozi
Created February 17, 2017 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pierozi/78ccb81525ea5a29729a8518d63903f0 to your computer and use it in GitHub Desktop.
Save Pierozi/78ccb81525ea5a29729a8518d63903f0 to your computer and use it in GitHub Desktop.
HoaOption
<?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();
@Hywan
Copy link

Hywan commented Feb 17, 2017

  • isNone should be defined as return !$this->isSome(), simply.
  • Why checking against the empty string too? Only null.
  • What about all the other methods? Should be a pain to implement each time :-/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment