Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Add ->conjoin() Method To Collection #568

Closed
mikebronner opened this issue May 5, 2017 · 8 comments
Closed

[Proposal] Add ->conjoin() Method To Collection #568

mikebronner opened this issue May 5, 2017 · 8 comments

Comments

@mikebronner
Copy link

mikebronner commented May 5, 2017

I would like to propose adding an ->conjoin($collection) method that behaves similarly to ->merge(), but does not overwrite items with the same key. Please forgive me if this is already doable with a collection method, but I couldn't find one.

Would be happy to write up a PR for this. Here's a sample of what I had in mind:

<?php namespace App\Services;

use Illuminate\Support\Collection;

class CustomizedCollection extends Collection
{
    public function conjoin(Collection $source) : self
    {
        $source->each(function ($item) {
            $this->push($item);
        });
        
        return $this;
    }
}
@mikebronner mikebronner changed the title [Proposal] Add ->add() Method To Collection [Proposal] Add ->inject() Method To Collection May 5, 2017
@mikebronner mikebronner changed the title [Proposal] Add ->inject() Method To Collection [Proposal] Add ->conjoin() Method To Collection May 5, 2017
@Dylan-DPC-zz
Copy link

Dylan-DPC-zz commented May 5, 2017

What will happen if both have same index?

@Garbee
Copy link

Garbee commented May 5, 2017

@Dylan-DPC Well, if it is not overwriting like the existing method then it must... skip over it and keep the base collections value?

@sisve
Copy link

sisve commented May 5, 2017

Uhm. So you have $a and $b, and want to $a->merge($b) without overwriting stuff? Wouldn't that be the same result as $b->merge($a)?

@mikebronner
Copy link
Author

mikebronner commented May 5, 2017

@Dylan-DPC @Garbee No, it adds all items, even if the indexes are the same, since push doesn't care about the content (whereas merge inspects the keys). I have implemented this in production already, and thought to share this here. Granted, its not a common use-case, as it only happens when mixing different types of objects in a collection that have the same id.

@sisve Not really, because doing $b->merge($a) will also overwrite any keys that exist in both collections.

@Garbee
Copy link

Garbee commented May 5, 2017

Ah, so you want to entirely ignore the keys and create a new collection of only the values. So a built-in way of doing collect(...$collection, ...$another) or collect($collection, $another)->collapse().

@Garbee
Copy link

Garbee commented May 5, 2017

Or you could also do $collection->prepend($another)->flatten(). Either way, the requested functionality is simply a minor syntax improvement. Go ahead and submit a PR, you already have the code written. Although, I would recommend making the method not mutate the current object but create a new collection to return.

@mikebronner
Copy link
Author

mikebronner commented May 6, 2017

@Garbee no, I don't want to ignore keys, I want to allow for duplicate keys, but different values. but good idea on not mutating, thanks :).

Update: I see now what you meant with keys, sorry got that confused. Right, I am ignoring keys, and only adding objects. However, the merge function overwrites items if the numerical keys match. Using push just adds it to the end of the collection, ignoring the key.

Adding this method would allow direct collection-to-collection manipulation, without having to iterate.

@mikebronner
Copy link
Author

mikebronner commented May 25, 2017

PR was merged in. Thanks for the feedback everyone! :) Method was renamed to concat().

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

No branches or pull requests

4 participants