From 53012c9a046829c55b65b338ba9ca7aee88cf350 Mon Sep 17 00:00:00 2001 From: Sergei Kolesnikov Date: Sat, 8 Jun 2019 17:19:59 +0300 Subject: [PATCH] Add path, path_or, prop, prop_or, props functions --- composer.json | 5 ++ src/Functional/Functional.php | 25 ++++++++++ src/Functional/Path.php | 35 ++++++++++++++ src/Functional/PathOr.php | 42 +++++++++++++++++ src/Functional/Prop.php | 35 ++++++++++++++ src/Functional/PropOr.php | 45 ++++++++++++++++++ src/Functional/Props.php | 41 ++++++++++++++++ tests/Functional/PathOrTest.php | 73 ++++++++++++++++++++++++++++ tests/Functional/PathTest.php | 71 ++++++++++++++++++++++++++++ tests/Functional/PropOrTest.php | 84 +++++++++++++++++++++++++++++++++ tests/Functional/PropTest.php | 68 ++++++++++++++++++++++++++ tests/Functional/PropsTest.php | 56 ++++++++++++++++++++++ 12 files changed, 580 insertions(+) create mode 100644 src/Functional/Path.php create mode 100644 src/Functional/PathOr.php create mode 100644 src/Functional/Prop.php create mode 100644 src/Functional/PropOr.php create mode 100644 src/Functional/Props.php create mode 100644 tests/Functional/PathOrTest.php create mode 100644 tests/Functional/PathTest.php create mode 100644 tests/Functional/PropOrTest.php create mode 100644 tests/Functional/PropTest.php create mode 100644 tests/Functional/PropsTest.php diff --git a/composer.json b/composer.json index 2b9656c3..62ad5090 100644 --- a/composer.json +++ b/composer.json @@ -82,10 +82,15 @@ "src/Functional/PartialMethod.php", "src/Functional/PartialRight.php", "src/Functional/Partition.php", + "src/Functional/Path.php", + "src/Functional/PathOr.php", "src/Functional/Pick.php", "src/Functional/Pluck.php", "src/Functional/Poll.php", "src/Functional/Product.php", + "src/Functional/Prop.php", + "src/Functional/PropOr.php", + "src/Functional/Props.php", "src/Functional/Ratio.php", "src/Functional/ReduceLeft.php", "src/Functional/ReduceRight.php", diff --git a/src/Functional/Functional.php b/src/Functional/Functional.php index 56217192..38bef0b7 100644 --- a/src/Functional/Functional.php +++ b/src/Functional/Functional.php @@ -299,6 +299,16 @@ final class Functional */ const partition = '\Functional\partition'; + /** + * @see \Functional\path + */ + const path = '\Functional\path'; + + /** + * @see \Functional\path_or + */ + const path_or = '\Functional\path_or'; + /** * @see \Functional\pick */ @@ -319,6 +329,21 @@ final class Functional */ const product = '\Functional\product'; + /** + * @see \Functional\prop + */ + const prop = '\Functional\prop'; + + /** + * @see \Functional\prop_or + */ + const prop_or = '\Functional\prop_or'; + + /** + * @see \Functional\props + */ + const props = '\Functional\props'; + /** * @see \Functional\ratio */ diff --git a/src/Functional/Path.php b/src/Functional/Path.php new file mode 100644 index 00000000..b964630c --- /dev/null +++ b/src/Functional/Path.php @@ -0,0 +1,35 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional; + +/** + * Retrieve the value at a given path. + * + * @param array $path The path to use + * @param array|object $item The object or array to retrieve the nested property from + * @return mixed The value at the given path or null if it not exists + */ +function path(array $path, $item) +{ + return path_or(null, $path, $item); +} diff --git a/src/Functional/PathOr.php b/src/Functional/PathOr.php new file mode 100644 index 00000000..964ea8d6 --- /dev/null +++ b/src/Functional/PathOr.php @@ -0,0 +1,42 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional; + +/** + * Retrieve the value at a given path. + * + * @param mixed $default Default value if the value at specified path not found + * @param array $path The path to use + * @param array|object $item The object or array to retrieve the nested property from + * @return mixed The value at the given path or the default if it not exists + */ +function path_or($default, array $path, $item) +{ + return \array_reduce( + $path, + function ($item, $key) use ($default) { + return prop_or($default, $key, $item); + }, + $item + ); +} diff --git a/src/Functional/Prop.php b/src/Functional/Prop.php new file mode 100644 index 00000000..78390d48 --- /dev/null +++ b/src/Functional/Prop.php @@ -0,0 +1,35 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional; + +/** + * Returns the indicated property of the item, if it exists, or null, if not. + * + * @param mixed $key The property name + * @param array|object $item The object or array to query + * @return mixed The value + */ +function prop($key, $item) +{ + return prop_or(null, $key, $item); +} diff --git a/src/Functional/PropOr.php b/src/Functional/PropOr.php new file mode 100644 index 00000000..db5e19dc --- /dev/null +++ b/src/Functional/PropOr.php @@ -0,0 +1,45 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional; + +/** + * Returns the indicated property of the item, if it exists, or the default value, if not. + * + * @param mixed $default Default value if the property not found + * @param mixed $key The property name + * @param array|object $item The object or array to query + * @return mixed The value + */ +function prop_or($default, $key, $item) +{ + switch (true) { + case \is_array($item) || $item instanceof \ArrayAccess: + return $item[$key] ?? $default; + + case \is_object($item) && isset($item->$key): + return $item->$key ?? $default; + + default: + return $default; + } +} diff --git a/src/Functional/Props.php b/src/Functional/Props.php new file mode 100644 index 00000000..af824d19 --- /dev/null +++ b/src/Functional/Props.php @@ -0,0 +1,41 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional; + +/** + * Acts as multiple prop: array of keys in, array of values out. Preserves order. + * + * @param array $keys The property names to fetch + * @param array|object $item The object or array to query + * @return mixed The value + */ +function props(array $keys, $item) +{ + $result = []; + + foreach ($keys as $key) { + $result[] = prop($key, $item); + } + + return $result; +} diff --git a/tests/Functional/PathOrTest.php b/tests/Functional/PathOrTest.php new file mode 100644 index 00000000..2b87a470 --- /dev/null +++ b/tests/Functional/PathOrTest.php @@ -0,0 +1,73 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional\Tests; + +use function Functional\path_or; + +class PathOrTest extends AbstractTestCase +{ + const DEFAULT = 'amazing default value'; + + /** + * @dataProvider provider + * + * @param $expected + * @param $path + * @param $item + */ + public function testPropOr($expected, $path, $item) + { + $this->assertEquals($expected, path_or(static::DEFAULT, $path, $item)); + } + + public function provider() + { + $object = new class { + private $a = 5; + public $b = 5; + }; + + $objectTestGetter = new class { + public function __get($propertyName) + { + throw new \RuntimeException($propertyName); + } + }; + + return [ + ['42', ['a'], (object)['a' => '42', 'b' => '69']], + ['42', ['a'], ['a' => '42', 'b' => '69']], + ['69', ['42', 'a'], ['42' => ['a' => '69']]], + ['69', ['42', 0], ['42' => ['69']]], + [static::DEFAULT, ['a'], ['b' => '69']], + [static::DEFAULT, ['a'], $object], + [static::DEFAULT, ['b', 'x'], $object], + [static::DEFAULT, ['anyField'], $objectTestGetter], + [static::DEFAULT, ['anyField', 'deep'], $objectTestGetter], + ['a', [0], \SplFixedArray::fromArray(['a', 'b', 'c'])], + [static::DEFAULT, ['nonExisting'], new \ArrayObject(['a', 'b', 'c'])], + ['42', ['first', 'a'], new \ArrayObject(['first' => (object)['a' => '42', 'b' => '69'], 'b', 'c'])], + [static::DEFAULT, ['first', 'a', 'x'], new \ArrayObject(['first' => (object)['a' => '42', 'b' => '69'], 'b', 'c'])], + ]; + } +} diff --git a/tests/Functional/PathTest.php b/tests/Functional/PathTest.php new file mode 100644 index 00000000..e7dbbc86 --- /dev/null +++ b/tests/Functional/PathTest.php @@ -0,0 +1,71 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional\Tests; + +use function Functional\path; + +class PathTest extends AbstractTestCase +{ + /** + * @dataProvider provider + * + * @param $expected + * @param $path + * @param $item + */ + public function testPropOr($expected, $path, $item) + { + $this->assertEquals($expected, path($path, $item)); + } + + public function provider() + { + $object = new class { + private $a = 5; + public $b = 5; + }; + + $objectTestGetter = new class { + public function __get($propertyName) + { + throw new \RuntimeException($propertyName); + } + }; + + return [ + ['42', ['a'], (object)['a' => '42', 'b' => '69']], + ['42', ['a'], ['a' => '42', 'b' => '69']], + ['69', ['42', 'a'], ['42' => ['a' => '69']]], + ['69', ['42', 0], ['42' => ['69']]], + [null, ['a'], ['b' => '69']], + [null, ['a'], $object], + [null, ['b', 'x'], $object], + [null, ['anyField'], $objectTestGetter], + [null, ['anyField', 'deep'], $objectTestGetter], + ['a', [0], \SplFixedArray::fromArray(['a', 'b', 'c'])], + [null, ['nonExisting'], new \ArrayObject(['a', 'b', 'c'])], + ['42', ['first', 'a'], new \ArrayObject(['first' => (object)['a' => '42', 'b' => '69'], 'b', 'c'])], + [null, ['first', 'a', 'x'], new \ArrayObject(['first' => (object)['a' => '42', 'b' => '69'], 'b', 'c'])], + ]; + } +} diff --git a/tests/Functional/PropOrTest.php b/tests/Functional/PropOrTest.php new file mode 100644 index 00000000..516cdbb6 --- /dev/null +++ b/tests/Functional/PropOrTest.php @@ -0,0 +1,84 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional\Tests; + +use function Functional\prop_or; + +class PropOrTest extends AbstractTestCase +{ + const DEFAULT = 'amazing default value'; + + /** + * @dataProvider provider + * + * @param $expected + * @param $key + * @param $item + */ + public function testPropOr($expected, $key, $item) + { + $this->assertEquals($expected, prop_or(static::DEFAULT, $key, $item)); + } + + public function testPropOrException() + { + $this->expectExceptionMessage('Ha ha!'); + + $object = new class { + public function __isset($propertyName) + { + throw new \RuntimeException('Ha ha!'); + } + }; + + prop_or(static::DEFAULT, 'any', $object); + } + + public function provider() + { + $object = new class { + private $a = 5; + public $b = 5; + }; + + $objectTestGetter = new class { + public function __get($propertyName) + { + throw new \RuntimeException($propertyName); + } + }; + + return [ + ['42', 'a', (object)['a' => '42', 'b' => '69']], + ['42', 'a', ['a' => '42', 'b' => '69']], + ['69', 1, ['42', '69']], + [static::DEFAULT, 'a', ['b' => '69']], + [static::DEFAULT, 'a', $object], + [5, 'b', $object], + [static::DEFAULT, 'anyField', $objectTestGetter], + ['a', 0, \SplFixedArray::fromArray(['a', 'b', 'c'])], + [static::DEFAULT, 'nonExisting', new \ArrayObject(['a', 'b', 'c'])], + ['a', 'first', new \ArrayObject(['first' => 'a', 'b', 'c'])], + ]; + } +} diff --git a/tests/Functional/PropTest.php b/tests/Functional/PropTest.php new file mode 100644 index 00000000..3577c00c --- /dev/null +++ b/tests/Functional/PropTest.php @@ -0,0 +1,68 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional\Tests; + +use function Functional\prop; + +class PropTest extends AbstractTestCase +{ + /** + * @dataProvider provider + * + * @param $expected + * @param $key + * @param $item + */ + public function testPropOr($expected, $key, $item) + { + $this->assertEquals($expected, prop($key, $item)); + } + + public function provider() + { + $object = new class { + private $a = 5; + public $b = 5; + }; + + $objectTestGetter = new class { + public function __get($propertyName) + { + throw new \RuntimeException($propertyName); + } + }; + + return [ + ['42', 'a', (object)['a' => '42', 'b' => '69']], + ['42', 'a', ['a' => '42', 'b' => '69']], + ['69', 1, ['42', '69']], + [null, 'a', ['b' => '69']], + [null, 'a', $object], + [5, 'b', $object], + [null, 'anyField', $objectTestGetter], + ['a', 0, \SplFixedArray::fromArray(['a', 'b', 'c'])], + [null, 'nonExisting', new \ArrayObject(['a', 'b', 'c'])], + ['a', 'first', new \ArrayObject(['first' => 'a', 'b', 'c'])], + ]; + } +} diff --git a/tests/Functional/PropsTest.php b/tests/Functional/PropsTest.php new file mode 100644 index 00000000..12d04106 --- /dev/null +++ b/tests/Functional/PropsTest.php @@ -0,0 +1,56 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +namespace Functional\Tests; + +use function Functional\props; + +class PropsTest extends AbstractTestCase +{ + /** + * @dataProvider provider + * + * @param $expected + * @param $keys + * @param $item + */ + public function testPropOr($expected, $keys, $item) + { + $this->assertEquals($expected, props($keys, $item)); + } + + public function provider() + { + $object = new class { + private $a = 5; + public $b = 5; + }; + + return [ + [['42', '69'], [0, 1], ['42', '69']], + [['69', '42'], [1, 0], ['42', '69']], + [[null, 5, null], ['a', 'b', 'c'], $object], + [[5], ['b'], $object], + [[], [], ['42', '69']], + ]; + } +}