Skip to content

Make Collection Extensible #17965

@othercorey

Description

@othercorey

Description

Currently, users are not able to extend Collection. They can create a Collection instance by passing a custom iterator that wraps the previous collection, but they cannot chain calls. They have to re-wrap manually each time.

Even if users could extend Collection without breaking, the current modifiers return CollectionInterface that's implemented by various other classes and does not return static.

Making collections extensible requires these changes:

  • Combine CollectionInterface and CollectionTrait into Collection
  • Change built-in iterators to be standard Interface implementations and not extend Collection
  • Change Collection to return new static() which wraps one of the iterators
  • Add helper method to use a custom iterator without extending Collection

Users would have to make these changes:

  • Change CollectionInterface type declarations to Collection
  • Get the inner iterator before calling printer() on the TreeIterator

This allows future changes:

  • Allow the user to specify the result set class that extends Collection
  • Allow adding methods to Collection without modifying an interface

Collection cannot be extended because it has confusing logic trying to determine if it's a standard Collection or an iterator extending Collection. Any user class that extends Collection breaks this logic and fails to unwrap properly.

Unknown:

  • Do we need to perform the full unwrapping of inner iterators

CakePHP Version

6.0

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions