laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Check for Traversable before Jsonable in EnumeratesValues::getArrayableItems() #2556

Open bradleybensmith opened 3 years ago

bradleybensmith commented 3 years ago

Description:

Shouldn't EnumeratesValues::getArrayableItems() check for Traversable before Jsonable? The Jsonable and JsonSerializable result in an array of stdClass objects where Traversable would preserve the correct class objects.

https://github.com/laravel/framework/blob/8.x/src/Illuminate/Collections/Traits/EnumeratesValues.php#L923

        } elseif ($items instanceof Jsonable) {
            return json_decode($items->toJson(), true);
        } elseif ($items instanceof JsonSerializable) {
            return (array) $items->jsonSerialize();
        } elseif ($items instanceof Traversable) {
            return iterator_to_array($items);
        }

As a workaround, I'm currently having to call iterator_to_array when instantiating a Collection since the (3rd party) class I'm using only implements JsonSerializable and IteratorAggregate (Traversable).

driesvints commented 3 years ago

Heya, thanks for submitting this.

This seems like a feature request or an improvement so I'm moving this to the ideas repository instead. It's best to post these in the ideas repository in the future to get support for your idea. After that you may send a PR to the framework. Please only use the laravel/framework issue tracker to report bugs and issues with the framework.

Thanks!