MDL-79890 core: Add polyfill for array_keys with filter
This is a Moodle version of a proposed function `array_keys_filter`. Since this method has not yet been created in upstream PHP, I have elected to name it with a Moodle prefix as the signature is not known. In the future, if and when this method is created, we can replace its content with new method and deprecate it.
This commit is contained in:
@@ -11037,3 +11037,29 @@ function site_is_public() {
|
||||
function exceeds_password_length(string $password, int $pepperlength = 0): bool {
|
||||
return (strlen($password) > (MAX_PASSWORD_CHARACTERS + $pepperlength));
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper to replace PHP 8.3 usage of array_keys with two args.
|
||||
*
|
||||
* There is an indication that this will become a new method in PHP 8.4, but that has not happened yet.
|
||||
* Therefore this non-polyfill has been created with a different naming convention.
|
||||
* In the future it can be deprecated if a core PHP method is created.
|
||||
*
|
||||
* https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#array_keys
|
||||
*
|
||||
* @param array $array
|
||||
* @param mixed $filter The value to filter on
|
||||
* @return array
|
||||
*/
|
||||
function moodle_array_keys_filter(array $array, mixed $filter, bool $strict): array {
|
||||
return array_keys(array_filter(
|
||||
$array,
|
||||
function($value, $key) use ($filter, $strict): bool {
|
||||
if ($strict) {
|
||||
return $value === $filter;
|
||||
}
|
||||
return $value == $filter;
|
||||
},
|
||||
ARRAY_FILTER_USE_BOTH,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -5445,4 +5445,87 @@ EOT;
|
||||
$this->assertEquals($expected, is_proxybypass($url));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the moodle_array_keys_filter method behaves in the same way
|
||||
* that array_keys behaved before Moodle 8.3.
|
||||
*
|
||||
* @dataProvider moodle_array_keys_filter_provider
|
||||
* @param array $array
|
||||
* @param mixed $filter
|
||||
* @param bool $strict
|
||||
* @param array $expected
|
||||
* @covers ::moodle_array_keys_filter
|
||||
*/
|
||||
public function test_moodle_array_keys_filter(
|
||||
array $array,
|
||||
mixed $filter,
|
||||
bool $strict,
|
||||
array $expected,
|
||||
): void {
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
moodle_array_keys_filter($array, $filter, $strict),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for moodle_array_keys_filter tests.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function moodle_array_keys_filter_provider(): array {
|
||||
return [
|
||||
[['a', 'b', 'c'], 'b', false, [1]],
|
||||
[
|
||||
[
|
||||
'alpha' => 'a',
|
||||
'bravo' => 'b',
|
||||
'charlie' => 'c',
|
||||
],
|
||||
'b',
|
||||
false,
|
||||
['bravo'],
|
||||
],
|
||||
[
|
||||
[
|
||||
'zero' => 0,
|
||||
'one' => 1,
|
||||
'true' => true,
|
||||
],
|
||||
'1',
|
||||
false,
|
||||
['one', 'true'],
|
||||
],
|
||||
[
|
||||
[
|
||||
'zero' => 0,
|
||||
'one' => 1,
|
||||
'true' => true,
|
||||
],
|
||||
true,
|
||||
false,
|
||||
['one', 'true'],
|
||||
],
|
||||
[
|
||||
[
|
||||
'zero' => 0,
|
||||
'one' => 1,
|
||||
'true' => true,
|
||||
],
|
||||
true,
|
||||
true,
|
||||
['true'],
|
||||
],
|
||||
[
|
||||
[
|
||||
'zero' => 0,
|
||||
'one' => 1,
|
||||
'true' => true,
|
||||
],
|
||||
1,
|
||||
true,
|
||||
['one'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user