PHPackages                             jojo1981/typed-collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jojo1981/typed-collection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jojo1981/typed-collection
=========================

Typed collection

5.2.0(4mo ago)218.4k↓21.9%3MITPHPPHP ^8.0CI passing

Since Oct 23Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/jojo1981/typed-collection)[ Packagist](https://packagist.org/packages/jojo1981/typed-collection)[ RSS](/packages/jojo1981-typed-collection/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (14)Used By (3)

A typed collection data structure
=================================

[](#a-typed-collection-data-structure)

[![Build Status](https://github.com/jojo1981/typed-collection/actions/workflows/build.yml/badge.svg)](https://github.com/jojo1981/typed-collection/actions/workflows/build.yml)[![Coverage Status](https://camo.githubusercontent.com/c97da6a20e5fe3915039178b19775efc04fd68787d6e5feb46732bf3bb1db984/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6a6f6a6f313938312f74797065642d636f6c6c656374696f6e2f62616467652e737667)](https://coveralls.io/github/jojo1981/typed-collection)[![Latest Stable Version](https://camo.githubusercontent.com/d16c7afec8c7323c3dbefa1d54611c521e26ed3f8a60c8de7e197e6ed6f9e138/68747470733a2f2f706f7365722e707567782e6f72672f6a6f6a6f313938312f74797065642d636f6c6c656374696f6e2f762f737461626c65)](https://packagist.org/packages/jojo1981/typed-collection)[![Total Downloads](https://camo.githubusercontent.com/5da715dab09dc92558b8a37d381011a3bfd232cad0c3d4805c865512c6582036/68747470733a2f2f706f7365722e707567782e6f72672f6a6f6a6f313938312f74797065642d636f6c6c656374696f6e2f646f776e6c6f616473)](https://packagist.org/packages/jojo1981/typed-collection)[![License](https://camo.githubusercontent.com/dfec067e6109577d6c6e35bec14277ce4e1be807dcefba3164cb4463cd69041d/68747470733a2f2f706f7365722e707567782e6f72672f6a6f6a6f313938312f74797065642d636f6c6c656374696f6e2f6c6963656e7365)](https://packagist.org/packages/jojo1981/typed-collection)

Author: Joost Nijhuis &lt;&gt;

The typed collection is an ordered mutable sequence.

It is a data structure which is in fact a wrapper around an indexed array.
When a new collection is created the `type` for the collection *MUST* be given. The collection is of a certain `type` and will guarantee all elements in the collection are of the same `type`.
The *type* can be a **primitive** `type` or a **class**/**interface** `type` set for the collection.

Available types are:

- int (alias integer),
- float (aliases real, double or number)
- string (alias text)
- array
- object
- callable (alias callback)
- iterable
- class (class or interface name)

The `\Jojo1981\TypedCollection\Collection` class is countable and is traversable (iterable). The collection has the following convenient instance methods:

- getType(): string
- isEqualType(Collection $otherCollection): bool
- isEmpty(): bool
- isNonEmpty(): bool
- unshiftElement($element): void
- pushElement($element): void
- shiftElement()
- popElement()
- hasElement($element): bool
- pushElements(array $elements): void
- unshiftElements(array $elements): void
- setElements(array $elements): void
- removeElement($element): void
- indexOfElement($element): ?int
- getElementByIndex(int $index)
- getFirstElement()
- getLastElement()
- getFirstElementAsCollection(): Collection
- toArray(): array
- sortBy(callable $comparator): Collection
- map(string $type, callable $mapper): Collection
- flatMap(string $type, callable $mapper): Collection
- merge(Collection $otherCollection, Collection ...$otherCollections): void
- all(callable $predicate): bool
- some(callable $predicate): bool
- none(callable $predicate): bool
- forEach(callable $callback): void
- foldLeft(callable $callback, $initial = null)
- foldRight(callable $callback, $initial = null)
- filter(callable $predicate): Collection
- find(callable $predicate)
- group(callable $predicate, callable ...$predicates): Collection
- partition(callable $predicate): Collection
- slice(int $offset, ?int $length = null): Collection
- clear(): void
- count(): int
- getIterator(): CollectionIterator
- isEqualCollection(Collection $otherCollection, ?callable $predicate = null, bool $strict = false): bool

The `\Jojo1981\TypedCollection\Collection` has a static method `createFromCollections`.
Multiple collection of the same `type` can be merged together into one collection.

Installation
------------

[](#installation)

### Library

[](#library)

```
git clone https://github.com/jojo1981/typed-collection.git
```

### Composer

[](#composer)

[Install PHP Composer](https://getcomposer.org/doc/00-intro.md)

```
composer require jojo1981/typed-collection
```

Basic usage
-----------

[](#basic-usage)

```
