collection icon indicating copy to clipboard operation
collection copied to clipboard

Add compatibility with Doctrine Collection interface

Open strider2038 opened this issue 7 years ago • 8 comments

Is your feature request related to a problem? Please describe.

On our projects we use strictly typed collections for Doctrine. I think it will be nice if AbstractCollection class can implement methods from Doctrine Collection interface.

Describe the solution you'd like

I suggest to add methods to AbstractCollection and/or AbstractArray that will be compatible with Collection

  • public function removeElement($element)
  • public function containsKey($key)
  • public function get($key)
  • public function getKeys()
  • public function getValues()
  • public function set($key, $value)
  • public function key()
  • public function current()
  • public function next()
  • public function exists(Closure $p)
  • public function forAll(Closure $p)
  • public function partition(Closure $p)
  • public function indexOf($element)
  • public function slice($offset, $length = null)

Then user of the library can do something like this.

class ConcreteClassCollection extends Ramsey\Collection\AbstractCollection implements Doctrine\Common\Collections\Collection {
    public function getType(): string
    {
        return ConcreteClass::class;
    }
}

Describe alternatives you've considered

Maybe better approach is to add a trait?

class ConcreteClassCollection extends Ramsey\Collection\AbstractCollection implements Doctrine\Common\Collections\Collection {
    use DoctrineCollectionTrait;

    public function getType(): string
    {
        return ConcreteClass::class;
    }
}

strider2038 avatar Mar 29 '19 08:03 strider2038

If your goal is to have a Doctrine Collection compatible interface, why not use that package instead? What would be the point if having the same interface as a different package?

shadowhand avatar Mar 29 '19 13:03 shadowhand

@shadowhand because there are no strictly typed collections in Doctrine package (no classes like AbstractCollection or AbstractMap) and we have to implement this intermediate logic in our projects. I like this library and I think it is good example of OOP.

strider2038 avatar Mar 29 '19 13:03 strider2038

A trait would be a reasonable approach, if the maintainers think this is a good idea.

shadowhand avatar Mar 29 '19 13:03 shadowhand

I would be open to accepting a trait to make it compatible with Doctrine.

ramsey avatar Mar 29 '19 14:03 ramsey

@strider2038, the best way is to create your own class Collection (inherit it from Ramsey\Collection\AbstractCollection) and add the necessary methods to it.

laxity7 avatar Apr 19 '19 21:04 laxity7

+1 to this!

tyteen4a03 avatar Jul 14 '23 15:07 tyteen4a03

I noticed some incompatibilities between this library and doctrine/collections and have raised an issue on Doctrine's side: https://github.com/doctrine/collections/issues/378

tyteen4a03 avatar Jul 31 '23 09:07 tyteen4a03

It seems like Doctrine has reasons to only accept Closures: https://github.com/doctrine/collections/pull/321#issuecomment-1227747872

@ramsey would you be up for tightening the type for 3.0?

tyteen4a03 avatar Aug 01 '23 09:08 tyteen4a03