Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path, path_or, prop, prop_or, props functions #187

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions src/Functional/Functional.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down
35 changes: 35 additions & 0 deletions src/Functional/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright (C) 2019 by Sergei Kolesnikov <[email protected]>
*
* 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);
}
42 changes: 42 additions & 0 deletions src/Functional/PathOr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright (C) 2019 by Sergei Kolesnikov <[email protected]>
*
* 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
);
}
35 changes: 35 additions & 0 deletions src/Functional/Prop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright (C) 2019 by Sergei Kolesnikov <[email protected]>
*
* 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);
}
45 changes: 45 additions & 0 deletions src/Functional/PropOr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright (C) 2019 by Sergei Kolesnikov <[email protected]>
*
* 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;
}
}
41 changes: 41 additions & 0 deletions src/Functional/Props.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright (C) 2019 by Sergei Kolesnikov <[email protected]>
*
* 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;
}
73 changes: 73 additions & 0 deletions tests/Functional/PathOrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright (C) 2019 by Sergei Kolesnikov <[email protected]>
*
* 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'])],
];
}
}
Loading