Skip to content

Commit

Permalink
Merge branch '1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Dec 11, 2019
2 parents f4e7a6a + 77fbb6d commit e67920e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/PhpOption/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace PhpOption;

use ArrayAccess;
use IteratorAggregate;

/**
Expand Down Expand Up @@ -56,14 +57,14 @@ public static function fromValue($value, $noneValue = null)
* array, or the array's value at the given key is null, None is returned.
* Otherwise, Some is returned wrapping the value at the given key.
*
* @param mixed $array A potential array value.
* @param mixed $array A potential array or \ArrayAccess value.
* @param string $key The key to check.
*
* @return Option
*/
public static function fromArraysValue($array, $key)
{
if (!is_array($array) || !isset($array[$key])) {
if (!(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
return None::create();
}

Expand Down
34 changes: 32 additions & 2 deletions tests/PhpOption/Tests/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpOption\Tests;

use ArrayAccess;
use LogicException;
use PhpOption\LazyOption;
use PhpOption\None;
use PhpOption\Option;
Expand Down Expand Up @@ -30,6 +32,10 @@ public function testFromArraysValue()
$this->assertEquals(None::create(), Option::fromArraysValue(['foo' => 'bar'], 'baz'));
$this->assertEquals(None::create(), Option::fromArraysValue(['foo' => null], 'foo'));
$this->assertEquals(new Some('foo'), Option::fromArraysValue(['foo' => 'foo'], 'foo'));

$object = new SomeArrayObject();
$object['foo'] = 'foo';
$this->assertEquals(new Some('foo'), Option::fromArraysValue($object, 'foo'));
}

public function testFromReturn()
Expand Down Expand Up @@ -69,7 +75,7 @@ public function testOrElseWithNoneAsFirst()
public function testOrElseWithLazyOptions()
{
$throws = function () {
throw new \LogicException('Should never be called.');
throw new LogicException('Should never be called.');
};

$a = new Some('a');
Expand All @@ -81,7 +87,7 @@ public function testOrElseWithLazyOptions()
public function testOrElseWithMultipleAlternatives()
{
$throws = new LazyOption(function () {
throw new \LogicException('Should never be called.');
throw new LogicException('Should never be called.');
});
$returns = new LazyOption(function () {
return new Some('foo');
Expand Down Expand Up @@ -121,5 +127,29 @@ public function testLiftDegenerate()

$this->assertEquals(None::create(), $fL1());
$this->assertEquals(Some::create(null), $fL2());
}

class SomeArrayObject implements ArrayAccess
{
private $data = [];

public function offsetExists($offset)
{
return isset($this->data[$offset]);
}

public function offsetGet($offset)
{
return $this->data[$offset];
}

public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}

public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
}

0 comments on commit e67920e

Please sign in to comment.