Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[12.x] Introduce Arr::from() #55715

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

Merged
merged 10 commits into from
May 12, 2025
Merged

Conversation

daniser
Copy link
Contributor

@daniser daniser commented May 12, 2025

This PR is a second attempt at extracting getArrayableItems() functionality into separate function (first PR).

Key differences from previous PR are:

  • Arr::from() won't wrap scalar values to follow single responsibility principle;
  • Enums are handled as regular objects (wrapping logic kept in getArrayableItems - see [8.x] Handle collection creation around a single enum #42839);
  • WeakMap instances are now convertable into lists (keys cannot be preserved for obvious reasons);
  • Arr::arrayable() helper function has been added to be used in pair with Arr:from() when needed.

As for WeakMap concern:
I am convinced that this issue valid for both collections and arrays - arrays cannot be created with objects as a keys.
We can handle this situation by throwing an exception (current behavior) or by omitting WeakMap keys during casting to array (this PR).
Let me know if you prefer to keep current behavior.

src/Illuminate/Collections/Arr.php Show resolved Hide resolved
src/Illuminate/Support/Str.php Outdated Show resolved Hide resolved
@taylorotwell taylorotwell merged commit 64c440f into laravel:12.x May 12, 2025
58 checks passed
@daniser daniser deleted the feature/array-from branch May 12, 2025 13:59
@rodrigopedra
Copy link
Contributor

Enums are handled as regular objects

Why?

@daniser
Copy link
Contributor Author

daniser commented May 13, 2025

Enums are handled as regular objects

Why?

I decided to distinguish between "casting to array" and "wrapping in array". I wanted to keep it simple without creating special case for enums where it should not belong (issue with enum was inability to adequately wrap it in array).
Now we have both Arr::from and Arr::wrap for these purposes; on the other hand, getArrayableItems method used during collection creation contains logic that decides what's need to be done and prefers wrapping over casting for enums.
Hope I explained it well.

@rodrigopedra
Copy link
Contributor

Hope I explained it well.

You did! Thanks a lot

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.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.