-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a From class with Option creators
- Loading branch information
Showing
4 changed files
with
111 additions
and
11 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
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
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,47 @@ | ||
<?php | ||
|
||
namespace spec\Prewk\Option; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
|
||
class FromSpec extends ObjectBehavior | ||
{ | ||
function it_creates_a_some_from_a_set_thing() | ||
{ | ||
$this::nullable("foo")->unwrap()->shouldBe("foo"); | ||
} | ||
|
||
function it_creates_a_none_from_an_unset_thing() | ||
{ | ||
$this::nullable(null)->isNone()->shouldBe(true); | ||
} | ||
|
||
function it_creates_a_some_from_an_existing_key() | ||
{ | ||
$this::key(["foo" => 123], "foo")->unwrap()->shouldBe(123); | ||
$this::key(["foo" => null], "foo")->unwrap()->shouldBe(null); | ||
} | ||
|
||
function it_creates_a_none_from_a_missing_key() | ||
{ | ||
$this::key(["foo" => 123], "bar")->isNone()->shouldBe(true); | ||
} | ||
|
||
function it_creates_a_some_from_a_non_empty_value() | ||
{ | ||
$this::emptyable(["something"])->unwrap()->shouldBe(["something"]); | ||
$this::emptyable(true)->unwrap()->shouldBe(true); | ||
$this::emptyable(1)->unwrap()->shouldBe(1); | ||
$this::emptyable("1")->unwrap()->shouldBe("1"); | ||
} | ||
|
||
function it_creates_a_none_from_an_empty_value() | ||
{ | ||
$this::emptyable("")->isNone()->shouldBe(true); | ||
$this::emptyable(null)->isNone()->shouldBe(true); | ||
$this::emptyable(false)->isNone()->shouldBe(true); | ||
$this::emptyable(0)->isNone()->shouldBe(true); | ||
$this::emptyable("0")->isNone()->shouldBe(true); | ||
$this::emptyable([])->isNone()->shouldBe(true); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
/** | ||
* From | ||
* | ||
* Helper class for creating Options from things | ||
* @author Oskar Thornblad | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prewk\Option; | ||
|
||
use Prewk\Option; | ||
|
||
/** | ||
* Helper class for creating Options from things | ||
*/ | ||
class From | ||
{ | ||
/** | ||
* Create a Some<T> if T is something using isset(T), None otherwise | ||
* | ||
* @param $thing T|null | ||
* @return Option Option<T> | ||
*/ | ||
public static function nullable($thing): Option | ||
{ | ||
return isset($thing) ? new Some($thing) : new None; | ||
} | ||
|
||
/** | ||
* Create a Some<V> from C[K] if it exists using array_key_exists(C, K), None otherwise | ||
* | ||
* @param array $coll C | ||
* @param $key K | ||
* @return Option Option<V> | ||
*/ | ||
public static function key(array $coll, $key): Option | ||
{ | ||
|
||
return array_key_exists($key, $coll) ? new Some($coll[$key]) : new None; | ||
} | ||
|
||
/** | ||
* Create a Some<T> if T is non-empty using empty(T), None otherwise | ||
* | ||
* @param $thing T|[]|null|0 | ||
* @return Option Option<T> | ||
*/ | ||
public function emptyable($thing): Option | ||
{ | ||
return !empty($thing) ? new Some($thing) : new None; | ||
} | ||
} |