Skip to content

Commit e3fac8b

Browse files
authored
Allow int and null as possible types for $key in Option::fromArraysValue (#74)
1 parent 647f42e commit e3fac8b

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/PhpOption/Option.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ public static function fromValue($value, $noneValue = null)
6262
* @template S
6363
*
6464
* @param array<string|int,S>|ArrayAccess<string|int,S>|null $array A potential array or \ArrayAccess value.
65-
* @param string $key The key to check.
65+
* @param string|int|null $key The key to check.
6666
*
6767
* @return Option<S>
6868
*/
6969
public static function fromArraysValue($array, $key)
7070
{
71-
if (!(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
71+
if ($key === null || !(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
7272
return None::create();
7373
}
7474

tests/PhpOption/Tests/OptionTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ public function testFromArraysValue(): void
3131
self::assertEquals(None::create(), Option::fromArraysValue(null, 'bar'));
3232
self::assertEquals(None::create(), Option::fromArraysValue(['foo' => 'bar'], 'baz'));
3333
self::assertEquals(None::create(), Option::fromArraysValue(['foo' => null], 'foo'));
34+
self::assertEquals(None::create(), Option::fromArraysValue(['foo' => 'bar'], null));
3435
self::assertEquals(new Some('foo'), Option::fromArraysValue(['foo' => 'foo'], 'foo'));
36+
self::assertEquals(new Some('foo'), Option::fromArraysValue([13 => 'foo'], 13));
3537

3638
$object = new SomeArrayObject();
3739
$object['foo'] = 'foo';
3840
self::assertEquals(new Some('foo'), Option::fromArraysValue($object, 'foo'));
41+
42+
$object = new SomeArrayObject();
43+
$object[13] = 'foo';
44+
self::assertEquals(new Some('foo'), Option::fromArraysValue($object, 13));
3945
}
4046

4147
public function testFromReturn(): void

0 commit comments

Comments
 (0)