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

[8.x] Change Arr::toCssClasses to Arr::conditionallyToString to be more abstract #38089

Closed
wants to merge 3 commits into from

Conversation

NickSdot
Copy link
Contributor

@NickSdot NickSdot commented Jul 21, 2021

The functionality introduced with #38016 is very useful. Thanks @danharrin! I might be a little late with this because it already got merged, and it's also maybe a little pedantic, but I still would like to try to make a point.

From my point of view the introduced array helper is too specific to a special situation. I see several use cased for the same functionality. If the framework starts to add helpers for each specific case, I could get bloated quite fast.

What does the array helper function actually do? It conditionally creates a string from a given array.

Hence, I would like to propose to change Arr::toCssClasses to Arr::conditionallyToString and add a little extra flexibility with the newly added parameter $separator.

Changes

Before:

public static function toCssClasses($array)
    {
        $classList = static::wrap($array);

        $classes = [];

        foreach ($classList as $class => $constraint) {
            if (is_numeric($class)) {
                $classes[] = $constraint;
            } elseif ($constraint) {
                $classes[] = $class;
            }
        }

        return implode(' ', $classes);
    }

After:

public static function conditionallyToString($array, $separator = ' ')
    {
        $array = static::wrap($array);

        $values = [];

        foreach ($array as $key => $value) {
            if (is_numeric($key)) {
                $values[] = $value;
            } elseif ($value) {
                $values[] = $key;
            }
        }

        return implode($separator, $values);
    }

Benefit

Function is re-usable to conditionally create any kind of string from an array.

Let's say I want to create a - separated string from an array. I can use:

$classes = Arr::conditionallyToString([
            'we',
            'do',
            'not' => false,
            'want',
            'to' => true,
            'build',
            'something' => true,
            'slug',
            'like'
        ], '-');

        // result: 'we-do-want-to-build-something-slug-like

Or I want to have a , separated list of people attending to an event:

        $values = Arr::conditionallyToString([
            'Taylor',
            'John',
            'Sponge Bob' => false,
            'Dave' => true,
        ], ', ');

        // result: Taylor, John, Dave'

Breaking change or not?

Technically this is a breaking change, because the new function just got merged and released. One could argue that no one is using the new function now.

In worst case there could be a non documented alias from Arr::toCssClasses to the more abstract Arr::conditionallyToString. This would not bloat the framework less in the specific case, but would enable developers to use a properly named function for teir own purpose.

Notes

  1. Blade stuff is changed to the new function name
  2. Tests are changed to the new function name and a cases for the new $separator parameter got added.
  3. This pull request requires changes in the newly added documentation items (@class Blade directive and array helper docs#7194)
  4. For better comparability I did not yet move the function to be in alphabetical order in Arr::class. Happy to do that in case the PR is considered to be merged

@taylorotwell
Copy link
Member

No plans to change this for now.

@NickSdot NickSdot deleted the conditionally-to-string branch June 10, 2023 05:23
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.

2 participants