diff --git a/ext/standard/array.c b/ext/standard/array.c index a2fef8b145283..735b8f9165e54 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -4866,7 +4866,7 @@ PHP_FUNCTION(array_change_key_case) zend_string *string_key; zend_string *new_key; zend_ulong num_key; - zend_long change_to_upper=0; + zend_long change_to_upper = PHP_CASE_LOWER; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ARRAY(array) @@ -4874,13 +4874,18 @@ PHP_FUNCTION(array_change_key_case) Z_PARAM_LONG(change_to_upper) ZEND_PARSE_PARAMETERS_END(); + if (change_to_upper != PHP_CASE_LOWER && change_to_upper != PHP_CASE_UPPER) { + zend_argument_value_error(2, "must be either CASE_LOWER or CASE_UPPER"); + RETURN_THROWS(); + } + array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(array))); ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, string_key, entry) { if (!string_key) { entry = zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry); } else { - if (change_to_upper) { + if (change_to_upper == PHP_CASE_UPPER) { new_key = zend_string_toupper(string_key); } else { new_key = zend_string_tolower(string_key); diff --git a/ext/standard/tests/array/array_change_key_case_flag_error.phpt b/ext/standard/tests/array/array_change_key_case_flag_error.phpt new file mode 100644 index 0000000000000..e83a21815c990 --- /dev/null +++ b/ext/standard/tests/array/array_change_key_case_flag_error.phpt @@ -0,0 +1,14 @@ +--TEST-- +array_change_key_case(): invalid $case argument +--FILE-- +getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +ValueError: array_change_key_case(): Argument #2 ($case) must be either CASE_LOWER or CASE_UPPER diff --git a/ext/standard/tests/array/array_change_key_case_variation.phpt b/ext/standard/tests/array/array_change_key_case_variation.phpt deleted file mode 100644 index c221a3f34101a..0000000000000 --- a/ext/standard/tests/array/array_change_key_case_variation.phpt +++ /dev/null @@ -1,50 +0,0 @@ ---TEST-- -Test array_change_key_case() function - 2 ---FILE-- - 1, "two" => 2, "THREE" => 3, "FOUR" => "four"); - -/* use 'case' argument other than CASE_LOWER & CASE_UPPER */ -try { - var_dump(array_change_key_case($item, "CASE_UPPER")); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} -var_dump(array_change_key_case($item, 5)); - -/* when keys are different in terms of only case */ -/* should return one value key pair with key being in lowercase */ -var_dump( array_change_key_case( array("ONE" => 1, "one" => 3, "One" => 4) ) ); - -/* should return one value key pair with key being in uppercase */ -var_dump( array_change_key_case( array("ONE" => 1, "one" => 2, "One" => 3), CASE_UPPER ) ); -var_dump( array_change_key_case( array("ONE" => 1, "one" => 1, "One" => 2), 5 ) ); - -echo "end\n"; -?> ---EXPECT-- -array_change_key_case(): Argument #2 ($case) must be of type int, string given -array(4) { - ["ONE"]=> - int(1) - ["TWO"]=> - int(2) - ["THREE"]=> - int(3) - ["FOUR"]=> - string(4) "four" -} -array(1) { - ["one"]=> - int(4) -} -array(1) { - ["ONE"]=> - int(3) -} -array(1) { - ["ONE"]=> - int(2) -} -end diff --git a/ext/standard/tests/array/array_change_key_case_variation4.phpt b/ext/standard/tests/array/array_change_key_case_variation4.phpt deleted file mode 100644 index 82d211451638a..0000000000000 --- a/ext/standard/tests/array/array_change_key_case_variation4.phpt +++ /dev/null @@ -1,132 +0,0 @@ ---TEST-- -Test array_change_key_case() function : usage variations - different int values for $case ---FILE-- - 'un', 'TWO' => 'deux', 'three' => 'trois'); -for ($i = -5; $i <=5; $i += 1){ - echo "\n-- \$sort argument is $i --\n"; - $temp = $input; - var_dump(array_change_key_case($temp, $i)); -} - -echo "Done"; -?> ---EXPECT-- -*** Testing array_change_key_case() : usage variations *** - --- $sort argument is -5 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is -4 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is -3 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is -2 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is -1 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is 0 -- -array(3) { - ["one"]=> - string(2) "un" - ["two"]=> - string(4) "deux" - ["three"]=> - string(5) "trois" -} - --- $sort argument is 1 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is 2 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is 3 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is 4 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} - --- $sort argument is 5 -- -array(3) { - ["ONE"]=> - string(2) "un" - ["TWO"]=> - string(4) "deux" - ["THREE"]=> - string(5) "trois" -} -Done