PHPackages                             jojo1981/typed-set - 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. jojo1981/typed-set

ActiveLibrary

jojo1981/typed-set
==================

Typed set

6.1.0(4mo ago)117.1k↓21.9%1MITPHPPHP ^8.0CI passing

Since Jan 9Pushed 4mo ago1 watchersCompare

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

READMEChangelogDependencies (4)Versions (25)Used By (1)

A typed set data structure
==========================

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

[![PHP Status](https://github.com/jojo1981/typed-set/actions/workflows/build.yml/badge.svg)](https://github.com/jojo1981/typed-set/actions/workflows/build.yml)[![Coverage Status](https://camo.githubusercontent.com/8e83d7ac90325b66e3f557956ce5e1a3c6fd1e37c6e2b74479e866bfe0756ac6/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6a6f6a6f313938312f74797065642d7365742f62616467652e737667)](https://coveralls.io/github/jojo1981/typed-set)[![Latest Stable Version](https://camo.githubusercontent.com/681df96fe8af8cd061a737de21b77f56f95eea0c051a71f9c01cf97ed87002f3/68747470733a2f2f706f7365722e707567782e6f72672f6a6f6a6f313938312f74797065642d7365742f762f737461626c65)](https://packagist.org/packages/jojo1981/typed-set)[![Total Downloads](https://camo.githubusercontent.com/c4dea7456d3c6c75d8152fc131080aad4dec08232af88a2905e4997b987362be/68747470733a2f2f706f7365722e707567782e6f72672f6a6f6a6f313938312f74797065642d7365742f646f776e6c6f616473)](https://packagist.org/packages/jojo1981/typed-set)[![License](https://camo.githubusercontent.com/dceac868dd09694df5209c98f156c6facb0bfa9eb424534a4452fd63556bb24b/68747470733a2f2f706f7365722e707567782e6f72672f6a6f6a6f313938312f74797065642d7365742f6c6963656e7365)](https://packagist.org/packages/jojo1981/typed-set)

Author: Joost Nijhuis &lt;&gt;

The typed set is an ordered mutable set.

When a new set is created the `type` for the set *MUST* be given.
Also a set can be created based on elements by using: `\Jojo1981\TypedSet\Set::createFromElements`.
The set is of a certain `type` and will guarantee all elements in the set are of the same `type`.
The *type* can be a **primitive** `type` or a **class**/**interface** `type` set for the collection.

Elements are considered as equal when the hash is the same.
The hash can be retrieved when the element is an instance of: `\Jojo1981\TypedSet\HashableInterface` or
when there is a handler which support the type. Handlers are classes which are implementing
the interface: `\Jojo1981\TypedSet\HandlerInterface` and are registered to the `GlobalHandler`. The `HashableInterface` has a higher precedence than the handlers.

The `GlobalHandler` is a singleton which can be configured during the bootstrap of your application.
Default handlers and custom handlers can be enabled.

When the element is an object which does not implement: `HashableInterface` and no handler support the
element the fallback will be that a hash will be generated based on the object hash.
This Set will be a set which has unique object instances.

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\TypedSet\Set` class is countable and traversable (iterable).
The set has the following convenient instance methods:

- add($element): void
- addAll(array $elements = \[\]): void
- contains($element): bool
- remove($element): void
- clear(): void
- isEmpty(): bool
- isNonEmpty(): bool
- toArray(): array
- getType(): string
- isEqualType(TypeInterface $type): bool
- isEqual(Set $other): bool
- compare(Set $other): DifferenceResult
- map(callable $mapper, ?string $type = null): Set
- filter(callable $predicate): Set
- find(callable $predicate): mixed
- all(callable $predicate): bool
- some(callable $predicate): bool
- none(callable $predicate): bool
- count(): int

The `\Jojo1981\TypedSet\Set` has a static method `createFromElements`.

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

[](#installation)

### Library

[](#library)

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

### Composer

[](#composer)

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

```
composer require jojo1981/typed-set
```

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

[](#basic-usage)

```
