Add compatibility with Doctrine Collection interface
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;
}
}
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 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.
A trait would be a reasonable approach, if the maintainers think this is a good idea.
I would be open to accepting a trait to make it compatible with Doctrine.
@strider2038, the best way is to create your own class Collection (inherit it from Ramsey\Collection\AbstractCollection) and add the necessary methods to it.
+1 to this!
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
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?