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

[5.2] A case with Arr:dot() #13009

Merged
merged 1 commit into from
Apr 4, 2016
Merged

[5.2] A case with Arr:dot() #13009

merged 1 commit into from
Apr 4, 2016

Conversation

themsaid
Copy link
Member

@themsaid themsaid commented Apr 4, 2016

Currently:

$output = Arr::dot(['emails' => []]);
// Returns an empty array $output == []

It skips the key if the value is an empty array, this has some effect on array validation as reported in: #13007.

This PR checks if a key inside Arr::dot has an empty value, and if so it just returns the key as is.

Arr::dot(['emails' => []]);
// Returns ['emails' => []]

I'm not sure if returning [] is breaking as it's not expected that Arr::dot() to have array values, thought of returning null or empty string.

If this is breaking, I believe Arr::dot() shouldn't be used in the validator and the validator to have its own method in 5.2 and fix Arr::dot() in 5.3.

What do you think?

@taylorotwell taylorotwell merged commit 4461e6f into laravel:5.2 Apr 4, 2016
@taylorotwell
Copy link
Member

I think this was just a bug. Array::dot is not really supposed to remove anything from the array at all.

@themsaid themsaid deleted the array-dot-dilemma branch April 24, 2016 09:52
@alexweissman
Copy link

Sorry to bring up such an old issue @themsaid , but I think your initial instinct was actually correct:

I'm not sure if returning [] is breaking as it's not expected that Arr::dot() to have array values, thought of returning null or empty string.

I have a method that uses array_dot to convert a collection to CSV format, but this patch is causing an unexpected behavior when my collection has an empty relationship. For example, I have a User with no Roles. array_dot($model) is giving me:

Array
(
    [id] => 1
    [user_name] => overmind
    [first_name] => Overmind
    [roles] => Array
        (
        )
)

When, what I expect is something like:

Array
(
    [id] => 1
    [user_name] => overmind
    [first_name] => Overmind
    [roles] => 
)

The CSV writer library I use isn't smart enough to handle array values, so we get an Array to string conversion notice and the unexpected "Array" value when this happens.

It's simple enough for me to loop through and check for this condition beforehand, but I still think that Arr::dot should be guaranteed to return a single-dimensional array. In fact, if you look at the documentation:

The array_dot function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth

Either null or an empty string would make more sense to me.

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