Skip to content

Commit

Permalink
Added a From class with Option creators
Browse files Browse the repository at this point in the history
  • Loading branch information
prewk committed Nov 18, 2017
1 parent 1d61ee4 commit 51bd1e0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 11 deletions.
5 changes: 2 additions & 3 deletions helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @codeCoverageIgnore
* @param mixed $value
* @return Prewk\Option\Ok
* @return Prewk\Option\Some
*/
function some($value): Prewk\Option\Some {
return new Prewk\Option\Some($value);
Expand All @@ -23,8 +23,7 @@ function some($value): Prewk\Option\Some {
* Represent a lack of value
*
* @codeCoverageIgnore
* @param mixed $value
* @return Prewk\Option\Err
* @return Prewk\Option\None
*/
function none(): Prewk\Option\None {
return new Prewk\Option\None;
Expand Down
16 changes: 8 additions & 8 deletions phpspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ suites:
prewk_suite:
namespace: Prewk
psr4_prefix: Prewk
extensions:
LeanPHP\PhpSpec\CodeCoverage\CodeCoverageExtension:
format:
- clover
output:
clover: build/logs/clover.xml
whitelist:
- src
#extensions:
# LeanPHP\PhpSpec\CodeCoverage\CodeCoverageExtension:
# format:
# - clover
# output:
# clover: build/logs/clover.xml
# whitelist:
# - src
47 changes: 47 additions & 0 deletions spec/Option/FromSpec.php
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);
}
}
54 changes: 54 additions & 0 deletions src/Option/From.php
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;
}
}

0 comments on commit 51bd1e0

Please sign in to comment.