harp-orm/identity-map

Allow only identical objects

0.1.1 2015-09-09 20:23 UTC

This package is auto-updated.

Last update: 2024-10-21 21:05:58 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version

This package allows having "canonical objects".

Usage

Assuming "new Item(1)" will have id of "1":

$map = new IdentityMap(function ($item) {
    return $item->getId();
});

$item1 = new Item(1);
$item2 = new Item(1);
$item3 = new Item(2);

echo $map->get($item1); // Will return item1
echo $map->get($item2); // Will return item1
echo $map->get($item3); // Will return item3

That way you can make sure items with the same key are the same physical objects

The closure argument that use pass to the identity map, needs to return the "unique key" for each product.

License

Copyright (c) 2014-2015, Clippings Ltd. Developed by Ivan Kerin as part of Clippings.com

Under BSD-3-Clause license, read LICENSE file.