Skip to content

T17000 collection revamp#17009

Merged
niden merged 18 commits into
5.0.xfrom
T17000-collection-revamp
May 14, 2026
Merged

T17000 collection revamp#17009
niden merged 18 commits into
5.0.xfrom
T17000-collection-revamp

Conversation

@niden
Copy link
Copy Markdown
Member

@niden niden commented May 13, 2026

Hello!

In raising this pull request, I confirm the following:

  • I have read and understood the Contributing Guidelines
  • I have checked that another pull request for this purpose does not exist
  • I wrote some tests for this PR
  • I have updated the relevant CHANGELOG
  • I have created a PR for the documentation about this change

Notable changes:

  • Changed Phalcon\Support\Collection::__construct() to accept two additional opt-in parameters — bool $strictNull = false and ?string $type = null. When strictNull is true, get() returns stored null values verbatim instead of substituting the default (previous behavior was unconditional default-substitution); when type is set, every set()/init() runs through the new validateType() guard, which maps the scalar tokens int/string/bool/float/array/object to their is_* checks and treats anything else as a class/interface name compared with instanceof, throwing InvalidArgumentException on mismatch
  • Changed Phalcon\Support\Collection::__serialize() to return a structured array ["data", "insensitive", "strictNull", "type"] so a serialize/unserialize round-trip restores the full configuration (previously only data survived). __unserialize() accepts both the new structured format and the legacy flat-array format, so existing serialized blobs keep deserializing without intervention
  • Changed Phalcon\Support\Collection::getKeys() and Phalcon\Support\Collection::getValues() to delegate to the new keys() / values() methods and marked the get*-prefixed pair @deprecated; behavior is unchanged but new code should call keys() / values() instead
  • Changed Phalcon\Contracts\Support\Collection to declare the expanded method surface (column, each, filter, first, getType, isEmpty, keys, last, map, reduce, replace, sort, values, where) so the contract matches the implementation
  • Added Phalcon\Support\Collection:column(string $propertyOrMethod): array (lift a single property/method off every item, keyed by the original collection key)
  • Added Phalcon\Support\Collection:each(callable $callback) (run the callback for side effects and return $this for chaining)
  • Added Phalcon\Support\Collection:filter(callable $callback) (new collection of items where the callback returns truthy)
  • Added Phalcon\Support\Collection:first() / last() (head/tail value or null on empty)
  • Added Phalcon\Support\Collection:isEmpty(): bool, getType(): string|null (the type guard configured at construction)
  • Added Phalcon\Support\Collection:keys(bool $insensitive = true) and values() (the non-get-prefixed names, with getKeys/getValues retained as deprecated wrappers)
  • Added Phalcon\Support\Collection:map(callable $callback) (new collection of transformed values, keys preserved)
  • Added Phalcon\Support\Collection:reduce(callable $callback, mixed $initial = null): mixed (callback signature ($accumulator, $value, $key))
  • Added Phalcon\Support\Collection:replace(array $data): void (clear then init in one call)
  • Added Phalcon\Support\Collection:sort(?callable $callback = null, int $order = SORT_ASC) (uasort when a callback is supplied, otherwise asort/arsort keyed by $order)
  • Added Phalcon\Support\Collection:where(string $propertyOrMethod, mixed $value) (strict-equality filter via extractValue)

Thanks

@niden niden merged commit d540269 into 5.0.x May 14, 2026
192 checks passed
@niden niden deleted the T17000-collection-revamp branch May 14, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant