PHPackages                             micoli/multitude - 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. micoli/multitude

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

micoli/multitude
================

v0.7.6(3y ago)1107↓100%MITPHPPHP &gt;=8.1

Since Apr 15Pushed 3y ago2 watchersCompare

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

READMEChangelogDependencies (10)Versions (10)Used By (0)

Micoli\\Multitude
=================

[](#micolimultitude)

A collection library for PHP.

[![Build Status](https://github.com/micoli/Multitude/workflows/Tests/badge.svg)](https://github.com/micoli/Multitude/actions)[![Coverage Status](https://camo.githubusercontent.com/4920c90577b62b5863347a27b7b600031a2c96b29af9776a1f74f9d43f42eaf0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d69636f6c692f4d756c7469747564652f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/micoli/Multitude?branch=main)[![Latest Stable Version](https://camo.githubusercontent.com/534387bb37ff18f28467d24e0b609f3e7a6898e0547f46a33ce554a9468838a8/687474703a2f2f706f7365722e707567782e6f72672f6d69636f6c692f6d756c7469747564652f76)](https://packagist.org/packages/micoli/multitude)[![Total Downloads](https://camo.githubusercontent.com/9819033b6aaa37deb4bbd28a9c059d459de2bf84f238e58fcd33ab67d31d4963/687474703a2f2f706f7365722e707567782e6f72672f6d69636f6c692f6d756c7469747564652f646f776e6c6f616473)](https://packagist.org/packages/micoli/multitude)[![Latest Unstable Version](https://camo.githubusercontent.com/c83d667889808633873d110a690f33700fe4f599c7a1d88416fa78ff92e0f69d/687474703a2f2f706f7365722e707567782e6f72672f6d69636f6c692f6d756c7469747564652f762f756e737461626c65)](https://packagist.org/packages/micoli/multitude) [![License](https://camo.githubusercontent.com/efdd3c95f5dc74f649db448cc5c0bc727d9697646e1079a0b4acce08d64ebc4f/687474703a2f2f706f7365722e707567782e6f72672f6d69636f6c692f6d756c7469747564652f6c6963656e7365)](https://packagist.org/packages/micoli/multitude)[![PHP Version Require](https://camo.githubusercontent.com/faa51e8455781f2e87aac7a3a9f54efc779b093ee855df0bc2520252d24dca52/687474703a2f2f706f7365722e707567782e6f72672f6d69636f6c692f6d756c7469747564652f726571756972652f706870)](https://packagist.org/packages/micoli/multitude)

Two types of collections are available:

- Sets (`MutableSet` and `ImmutableSet`), are sequences of unique values. Values can be of any types.
- Map (`MutableMap` and `ImmutableMap`), is a sequential collection of key-value pairs. Keys can be any type, but must be unique. Values can be of any types.

In both `ImmutableSet` and `ImmutableMap`, if a method alter the content af the inner values, a new instance is returned of the same type. Oppositely, in `MutableSet` and `MutableMap`, same methods are altering the inner values.

Methods are the most possible fluent.

Thanks to  for the main ideas used in that library, this is a complete rewrite of it's initial version.

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

[](#installation)

This library is installable via [Composer](https://getcomposer.org/):

```
composer require micoli/multitude
```

Requirements
------------

[](#requirements)

This library requires PHP 8.0 or later.

Project status
--------------

[](#project-status)

While this library is still under development, it is still in early development status. It follows semver version tagging.

Quick start
-----------

[](#quick-start)

Constructor are only statics:

- `new MutableSet(['a','b','c'])`
- `new MutableMap([2=>'a',3=>'b',4=>'c'])`
- `new MutableMap([[2,'a'],['2','aa'],[3,'b'],['3','bb'],[4,'c'])`

You can use fromTuples constructor if you need a strong typing for keys of your map, e.g. `'2'` key is different of `2`.

Methods that accept a `bool $throw` parameter will trigger an exception if `$throw == true` or fails silently if `$throw == false`.

Example with an associative array
---------------------------------

[](#example-with-an-associative-array)

```
    public function testItShouldFullyWorkWithAssociativeArray(): void
    {
        /** @var ImmutableMap $map */
        $map = new ImmutableMap([
            ['library', ['value' => 10, 'tags' => ['tag1']]],
            ['projects', ['value' => 5, 'tags' => ['tag2']]],
            ['gist', ['value' => 7, 'tags' => ['tag1', 'tag2']]],
            ['repository', ['value' => 7, 'tags' => ['tag3']]],
        ]);
        $totalSum = $map
            ->filter(fn (array $project, mixed $category): bool => array_search('tag1', $project['tags']) !== false)
            ->reduce(fn (int $sum, mixed $project, mixed $category): int => $sum + $project['value'], 0);
        self::assertSame($totalSum, 17);
        self::assertCount(4, $map);
    }
```

Example with an immutable map fully typed
-----------------------------------------

[](#example-with-an-immutable-map-fully-typed)

File: `Project.php`

```
