PHPackages                             stratadox/identity-map - 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. stratadox/identity-map

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

stratadox/identity-map
======================

v0.5.1(7y ago)02652MITPHPPHP &gt;=7.2

Since Apr 24Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Stratadox/IdentityMap)[ Packagist](https://packagist.org/packages/stratadox/identity-map)[ RSS](/packages/stratadox-identity-map/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (4)Versions (10)Used By (2)

Identity Map
============

[](#identity-map)

[![Build Status](https://camo.githubusercontent.com/b0fbde3dcd6fed0c172c1f90d71cd3cbc3f919cc71a953e533449644b880fdc9/68747470733a2f2f7472617669732d63692e6f72672f537472617461646f782f4964656e746974794d61702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Stratadox/IdentityMap)[![Coverage Status](https://camo.githubusercontent.com/75db1fd72f46f9b0aac90bfe26c3b99fa210bf46d85008d940fcd01b9e22c26b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f537472617461646f782f4964656e746974794d61702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Stratadox/IdentityMap?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5f781dde2b39defaac7962952d6da2120e3e243aa1187c7db030be619410db77/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f537472617461646f782f4964656e746974794d61702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Stratadox/IdentityMap/?branch=master)[![Infection Minimum](https://camo.githubusercontent.com/416cfd74a77aee9719fd9a0c0b5f9dd6970485b055c1e9a4cd01ce05e08a1754/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d73692d3130302d627269676874677265656e2e737667)](https://travis-ci.org/Stratadox/IdentityMap)[![PhpStan Level](https://camo.githubusercontent.com/7d521d242d198a9468ca6170cc4416765da9273ff094476e7125934666afc7da/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068707374616e2d372f372d627269676874677265656e2e737667)](https://travis-ci.org/Stratadox/IdentityMap)[![Maintainability](https://camo.githubusercontent.com/28fb6d0571b2919b2dc9949da1e1c5ffe9eb484581b598ee1f248f966cdc0b57/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38633237643632613032386539323936343864322f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/Stratadox/IdentityMap/maintainability)[![Latest Stable Version](https://camo.githubusercontent.com/193ec75e5713a9493f8e579279bc3e4548f61cd5a0bb8e0609a4f5e7e996c475/68747470733a2f2f706f7365722e707567782e6f72672f737472617461646f782f6964656e746974792d6d61702f762f737461626c65)](https://packagist.org/packages/stratadox/identity-map)[![License](https://camo.githubusercontent.com/4bb5a54678b63f4fe4a7b93c7989e4f83f356567ecaad7166493c3185612e65a/68747470733a2f2f706f7365722e707567782e6f72672f737472617461646f782f6964656e746974792d6d61702f6c6963656e7365)](https://packagist.org/packages/stratadox/identity-map)

Maps objects by identity.

About
-----

[](#about)

Contains the objects that have already been loaded.

Mainly used to prevent double loading of unique entities, and as registry of the loaded entities.

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

[](#installation)

Install with `composer require stratadox/identity-map`

What is this?
-------------

[](#what-is-this)

An [Identity Map](https://www.martinfowler.com/eaaCatalog/identityMap.html) is a registry of all the entities that have been loaded from the data source.

Client code can consult the identity map before performing expensive data retrieval operations (such as querying a database or requesting online resources)

How does it work?
-----------------

[](#how-does-it-work)

It's essentially just an immutable map of maps with objects.

The first layer maps classes to the map of loaded objects for that class. The second layer maps from the identity to the actual object.

Additionally, it contains a reverse map to quickly map an instance to its id.

How to use this?
----------------

[](#how-to-use-this)

### Making a map

[](#making-a-map)

Either create a map pre-filled with objects:

```
$map = IdentityMap::with([
    'id1' => $object1,
    'id2' => $object2,
]);
```

Or start with a blank map:

```
$map = IdentityMap::startEmpty();
```

...and later fill it up with objects:

```
$map = $map->add('id3', $object3);
```

Objects can be removed from the map by using:

```
$map = $map->remove(Foo::class, 'id3');
```

Or:

```
$map = $map->removeThe($object);
```

### Consulting the map

[](#consulting-the-map)

To check whether the entity with the requested id already exists in the map:

```
if ($map->has(Foo::class, '1')) { ...
```

To retrieve the corresponding object from the map:

```
$object = $map->get(Foo::class, '1');
```

To check whether the object instance was added:

```
if ($map->hasThe($object)) { ...
```

To retrieve the id of an object that is in the map:

```
$id = $map->idOf($object);
```

### Preventing unwanted classes

[](#preventing-unwanted-classes)

When loading a bunch of objects that may consist of both entities and value objects, one may want to ignore the value objects when provisioning the identity map.

This can be done by wrapping the identity map:

```
$map = Whitelist::forThe(IdentityMap::startEmpty(), MyEntity::class);
```

Adding objects that are not, in this case, of the `MyEntity` class, will be silently ignored.

Multiple entities can be whitelisted by specifying more classes:

```
$map = Whitelist::forThe(IdentityMap::startEmpty(), Foo::class, Bar::class);
```

Or using this shortcut:

```
$map = Whitelist::the(Foo::class, Bar::class);
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

9

Last Release

2909d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5333260?v=4)[Stratadox](/maintainers/Stratadox)[@Stratadox](https://github.com/Stratadox)

---

Top Contributors

[![Stratadox](https://avatars.githubusercontent.com/u/5333260?v=4)](https://github.com/Stratadox "Stratadox (34 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/stratadox-identity-map/health.svg)

```
[![Health](https://phpackages.com/badges/stratadox-identity-map/health.svg)](https://phpackages.com/packages/stratadox-identity-map)
```

###  Alternatives

[components/mathjs

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices.

15.0k3.8k](/packages/components-mathjs)[aura/payload-interface

An interface package for Domain Payload implementations.

13392.7k4](/packages/aura-payload-interface)[njxqlus/filament-lightbox

Lightbox entry for Filament

2054.6k](/packages/njxqlus-filament-lightbox)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
