From e3fac8b24f56113f7cb96af14958c0dd16330f54 Mon Sep 17 00:00:00 2001 From: Nicolas Giraud Date: Sat, 20 Jul 2024 23:41:07 +0200 Subject: [PATCH] Allow `int` and `null` as possible types for `$key` in `Option::fromArraysValue` (#74) --- src/PhpOption/Option.php | 4 ++-- tests/PhpOption/Tests/OptionTest.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PhpOption/Option.php b/src/PhpOption/Option.php index 172924c..91fab9c 100644 --- a/src/PhpOption/Option.php +++ b/src/PhpOption/Option.php @@ -62,13 +62,13 @@ public static function fromValue($value, $noneValue = null) * @template S * * @param array|ArrayAccess|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 */ 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(); } diff --git a/tests/PhpOption/Tests/OptionTest.php b/tests/PhpOption/Tests/OptionTest.php index d386ff2..4e31b92 100644 --- a/tests/PhpOption/Tests/OptionTest.php +++ b/tests/PhpOption/Tests/OptionTest.php @@ -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