Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
|
ea5a2132c3 | ||
|
3db4f9cddb | ||
|
06aa489fa8 |
12 changed files with 10 additions and 205 deletions
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
Copyright (c) 2024 Joby Elliott
|
Copyright (c) 2024 Joby Elliott
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
|
|
@ -1,119 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
|
||||||
*
|
|
||||||
* 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 Joby\Toolbox\Arrays;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
class ArrayFunctions
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Mostly this behaves like the built-in min function, but it has an
|
|
||||||
* optional parameter to control whether null values are considered high or
|
|
||||||
* low. By default they are considered low.
|
|
||||||
*
|
|
||||||
* @template T
|
|
||||||
* @param iterable<T|null> $data
|
|
||||||
* @param bool $null_high
|
|
||||||
* @return T|null
|
|
||||||
*/
|
|
||||||
public static function min(iterable $data, bool $null_high = false): mixed
|
|
||||||
{
|
|
||||||
$values = [];
|
|
||||||
foreach ($data as $value) {
|
|
||||||
// if nulls are low, they are the lowest value possible so short-circuit if we find one
|
|
||||||
if (!$null_high && is_null($value)) return null;
|
|
||||||
$values[] = $value;
|
|
||||||
}
|
|
||||||
if (empty($values)) throw new Exception("Minimum is undefined if there are no values");
|
|
||||||
$values = array_unique($values);
|
|
||||||
if ($null_high) {
|
|
||||||
usort(
|
|
||||||
$values,
|
|
||||||
function ($a, $b) {
|
|
||||||
if ($a === $b) return 0;
|
|
||||||
if (is_null($a)) return 1;
|
|
||||||
if (is_null($b)) return -1;
|
|
||||||
return $a <=> $b;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
usort(
|
|
||||||
$values,
|
|
||||||
function ($a, $b) {
|
|
||||||
if ($a === $b) return 0;
|
|
||||||
if (is_null($a)) return -1;
|
|
||||||
if (is_null($b)) return 1;
|
|
||||||
return $a <=> $b;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return $values[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mostly this behaves like the built-in max function, but it has an
|
|
||||||
* optional parameter to control whether null values are considered high or
|
|
||||||
* low. By default they are considered low.
|
|
||||||
*
|
|
||||||
* @template T
|
|
||||||
* @param iterable<T|null> $data
|
|
||||||
* @param bool $null_high
|
|
||||||
* @return T|null
|
|
||||||
*/
|
|
||||||
public static function max(iterable $data, bool $null_high = false): mixed
|
|
||||||
{
|
|
||||||
$values = [];
|
|
||||||
foreach ($data as $value) {
|
|
||||||
// if nulls are high, they are the highest value possible so short-circuit if we find one
|
|
||||||
if ($null_high && is_null($value)) return null;
|
|
||||||
$values[] = $value;
|
|
||||||
}
|
|
||||||
if (empty($values)) throw new Exception("Minimum is undefined if there are no values");
|
|
||||||
$values = array_unique($values);
|
|
||||||
if ($null_high) {
|
|
||||||
usort(
|
|
||||||
$values,
|
|
||||||
function ($a, $b) {
|
|
||||||
if ($a === $b) return 0;
|
|
||||||
if (is_null($a)) return 1;
|
|
||||||
if (is_null($b)) return -1;
|
|
||||||
return $a <=> $b;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
usort(
|
|
||||||
$values,
|
|
||||||
function ($a, $b) {
|
|
||||||
if ($a === $b) return 0;
|
|
||||||
if (is_null($a)) return -1;
|
|
||||||
if (is_null($b)) return 1;
|
|
||||||
return $a <=> $b;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return end($values);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
|
||||||
*
|
|
||||||
* 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 Joby\Toolbox\Arrays;
|
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
|
|
||||||
class ArrayFunctionsTest extends TestCase
|
|
||||||
{
|
|
||||||
|
|
||||||
public function testMin()
|
|
||||||
{
|
|
||||||
$array = [1, 2, 3, 4, 5];
|
|
||||||
$this->assertEquals(1, ArrayFunctions::min($array));
|
|
||||||
$array = [5, 4, 3, 2, 1];
|
|
||||||
$this->assertEquals(1, ArrayFunctions::min($array));
|
|
||||||
// by default nulls are low, and should be returned as if they are the lowest value
|
|
||||||
$array = [1, 2, 3, 4, 5, null];
|
|
||||||
$this->assertEquals(null, ArrayFunctions::min($array));
|
|
||||||
// if nulls are high, they should be skipped because they aren't the min
|
|
||||||
$this->assertEquals(1, ArrayFunctions::min($array, true));
|
|
||||||
// if the array only contains a null value it should be returned either way
|
|
||||||
$array = [null];
|
|
||||||
$this->assertEquals(null, ArrayFunctions::min($array));
|
|
||||||
$this->assertEquals(null, ArrayFunctions::min($array, true));
|
|
||||||
// should behave alphabetically for strings
|
|
||||||
$array = ['a', 'b', 'c', 'd', 'e'];
|
|
||||||
$this->assertEquals('a', ArrayFunctions::min($array));
|
|
||||||
$array = ['e', 'd', 'c', 'b', 'a'];
|
|
||||||
$this->assertEquals('a', ArrayFunctions::min($array));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testMax()
|
|
||||||
{
|
|
||||||
$array = [1, 2, 3, 4, 5];
|
|
||||||
$this->assertEquals(5, ArrayFunctions::max($array));
|
|
||||||
$array = [5, 4, 3, 2, 1];
|
|
||||||
$this->assertEquals(5, ArrayFunctions::max($array));
|
|
||||||
// by default nulls are low, and should be treated as if they are the lowest value
|
|
||||||
$array = [1, 2, 3, 4, 5, null];
|
|
||||||
$this->assertEquals(5, ArrayFunctions::max($array));
|
|
||||||
// if nulls are high, they should be returned as if they are the highest value
|
|
||||||
$this->assertEquals(null, ArrayFunctions::max($array, true));
|
|
||||||
// if the array only contains a null value it should be returned either way
|
|
||||||
$array = [null];
|
|
||||||
$this->assertEquals(null, ArrayFunctions::max($array));
|
|
||||||
$this->assertEquals(null, ArrayFunctions::max($array, true));
|
|
||||||
// should behave alphabetically for strings
|
|
||||||
$array = ['a', 'b', 'c', 'd', 'e'];
|
|
||||||
$this->assertEquals('e', ArrayFunctions::max($array));
|
|
||||||
$array = ['e', 'd', 'c', 'b', 'a'];
|
|
||||||
$this->assertEquals('e', ArrayFunctions::max($array));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joby's PHP Toolbox: https://code.byjoby.com/php-toolbox/
|
* Joby's PHP Toolbox: https://go.joby.lol/phptoolbox
|
||||||
* MIT License: Copyright (c) 2024 Joby Elliott
|
* MIT License: Copyright (c) 2024 Joby Elliott
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
|
Loading…
Reference in a new issue