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

[11.x] Implement changeKeyCase to Arr helper #52040

Conversation

Anton5360
Copy link
Contributor

@Anton5360 Anton5360 commented Jul 6, 2024

Issue

It's nice when you might just use compact() built-in function, but sometimes you need the same keys as var names but not in camel case. Then, you have to handle it manually especially if you work with large dataset
[ 'my_long_var_name' => $myLongVarName, ... ]

Solution

Now we can simply use changeKeyCase method

    $array = ['MyKey' => 1];

    // Pass CASE_LOWER or CASE_UPPER to use built-in `array_change_key_case` function
    Arr::changeKeyCase($array, CASE_LOWER); // ['mykey' => 1]
    Arr::changeKeyCase($array, CASE_UPPER); // ['MYKEY' => 1]

    // Use another built-in php function if needed
    Arr::changeKeyCase($array, 'lcfirst'); // ['myKey' => 1]
    Arr::changeKeyCase($array, lcfirst(...)); // ['myKey' => 1]

    // Pass a method of Str helper
    Arr::changeKeyCase($array, 'snake'); // ['my_key' => 1]
    Arr::changeKeyCase($array, Str::snake(...)); // ['my_key' => 1]

    // You can take advantage of Str macros as well
    Str::macro('myCustomCaseMacro', fn ($value, $additionalParameter) => $value[0].'...'.$additionalParameter);
    Arr::changeKeyCase($array, 'myCustomCaseMacro', 'Addon'); // ['M...Addon' => 1]

    // It`s also possible to pass custom callback
    Arr::changeKeyCase($testArray, fn ($key) => "prefix_$key")) // ['prefix_MyKey' => 1]

    // If method for Str helper does not exist, exception is thrown
    Arr::changeKeyCase($testArray, 'NonExistedStrHelperMethod'); // BadMethodCallException is thrown

Particularly, this can be used in combination with built-in compact function

    $somethingHere = 1;
    $somethingElse = 1;
    $moreSomethingElse = 1;
        
    Arr::changeKeyCase(
        array: compact('somethingHere', 'somethingElse', 'moreSomethingElse'),
        case: Str::snake(...),
    );

Note

Pipelines might fail. I would like to hear your opinion about it firstly, then I`ll fix issues and finish this thing if the feedback is positive

@chu121su12
Copy link
Contributor

How about a more generic mapKey/mapKeys as there are map and mapWithKeys?

@Anton5360
Copy link
Contributor Author

How about a more generic mapKey/mapKeys as there are map and mapWithKeys?

We might discuss your suggestion, but I personally would like to go with changeKeyCase naming to keep reference to built-in function.

  • Arr::map -> array_map
  • Arr::filter -> array_filter
  • Arr::changeKeyCase -> array_change_key_case
    and so on

@chu121su12
Copy link
Contributor

Cool. That's another function--an old one too--I just learned!

@taylorotwell
Copy link
Member

We already have mapWithKeys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants