2017 © Pedro Peláez
 

library identity-map

image

stratadox/identity-map

  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

Identity Map

Build Status Coverage Status Scrutinizer Code Quality Infection Minimum PhpStan Level Maintainability Latest Stable Version License, (*1)

Maps objects by identity., (*2)

About

Contains the objects that have already been loaded., (*3)

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

Installation

Install with composer require stratadox/identity-map, (*5)

What is this?

An Identity Map is a registry of all the entities that have been loaded from the data source., (*6)

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

How does it work?

It's essentially just an immutable map of maps with objects., (*8)

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., (*9)

Additionally, it contains a reverse map to quickly map an instance to its id., (*10)

How to use this?

Making a map

Either create a map pre-filled with objects:, (*11)

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

Or start with a blank map:, (*12)

$map = IdentityMap::startEmpty();

...and later fill it up with objects:, (*13)

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

Objects can be removed from the map by using:, (*14)

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

Or:, (*15)

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

Consulting the map

To check whether the entity with the requested id already exists in the map:, (*16)

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

To retrieve the corresponding object from the map:, (*17)

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

To check whether the object instance was added:, (*18)

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

To retrieve the id of an object that is in the map:, (*19)

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

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., (*20)

This can be done by wrapping the identity map:, (*21)

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

Adding objects that are not, in this case, of the MyEntity class, will be silently ignored., (*22)

Multiple entities can be whitelisted by specifying more classes:, (*23)

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

Or using this shortcut:, (*24)

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

The Versions

30/05 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

23/05 2018
21/05 2018
21/05 2018
21/05 2018
20/05 2018
17/05 2018
16/05 2018
25/04 2018

v0.2

0.2.0.0

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

24/04 2018

v0.1

0.1.0.0

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires