Skip to content

Commit

Permalink
Allow int and null as possible types for $key in `Option::fromA…
Browse files Browse the repository at this point in the history
…rraysValue` (#74)
  • Loading branch information
niconoe- committed Jul 20, 2024
1 parent 647f42e commit e3fac8b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpOption/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public static function fromValue($value, $noneValue = null)
* @template S
*
* @param array<string|int,S>|ArrayAccess<string|int,S>|null $array A potential array or \ArrayAccess value.
* @param string $key The key to check.
* @param string|int|null $key The key to check.
*
* @return Option<S>
*/
public static function fromArraysValue($array, $key)
{
if (!(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
if ($key === null || !(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
return None::create();
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PhpOption/Tests/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ public function testFromArraysValue(): void
self::assertEquals(None::create(), Option::fromArraysValue(null, 'bar'));
self::assertEquals(None::create(), Option::fromArraysValue(['foo' => 'bar'], 'baz'));
self::assertEquals(None::create(), Option::fromArraysValue(['foo' => null], 'foo'));
self::assertEquals(None::create(), Option::fromArraysValue(['foo' => 'bar'], null));
self::assertEquals(new Some('foo'), Option::fromArraysValue(['foo' => 'foo'], 'foo'));
self::assertEquals(new Some('foo'), Option::fromArraysValue([13 => 'foo'], 13));

$object = new SomeArrayObject();
$object['foo'] = 'foo';
self::assertEquals(new Some('foo'), Option::fromArraysValue($object, 'foo'));

$object = new SomeArrayObject();
$object[13] = 'foo';
self::assertEquals(new Some('foo'), Option::fromArraysValue($object, 13));
}

public function testFromReturn(): void
Expand Down

0 comments on commit e3fac8b

Please sign in to comment.