Skip to content
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

Add compatibility with Doctrine Collection interface #51

Open
strider2038 opened this issue Mar 29, 2019 · 8 comments
Open

Add compatibility with Doctrine Collection interface #51

strider2038 opened this issue Mar 29, 2019 · 8 comments

Comments

@strider2038
Copy link

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 strider2038 changed the title Add compatibility with doctrine Collection interface Add compatibility with Doctrine Collection interface Mar 29, 2019
@shadowhand
Copy link

shadowhand commented Mar 29, 2019

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?

@strider2038
Copy link
Author

@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.

@shadowhand
Copy link

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

@ramsey
Copy link
Owner

ramsey commented Mar 29, 2019

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

@laxity7
Copy link

laxity7 commented Apr 19, 2019

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

@tyteen4a03
Copy link

+1 to this!

@tyteen4a03
Copy link

I noticed some incompatibilities between this library and doctrine/collections and have raised an issue on Doctrine's side: doctrine/collections#378

@tyteen4a03
Copy link

It seems like Doctrine has reasons to only accept Closures: doctrine/collections#321 (comment)

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

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

No branches or pull requests

5 participants