mfn/util-simpleorderedmap

Ordered map accepting arbitrary keys

v0.0.2 2014-09-27 07:17 UTC

This package is auto-updated.

Last update: 2024-10-16 16:56:08 UTC


README

Homepage: https://github.com/mfn/php-util-simpleorderedmap

Basic usage:

$map = new \Mfn\Util\Map\SimpleOrderedMap;
$map->add(new \stdClass, "value");
foreach ($map->keys() as $key) {
  echo $map->get($key), PHP_EOL;
}

Outputs: value

Common usage

Add and set keys/values:

use \Mfn\Util\Map\SimpleOrderedMap;
$map = SimpleOrderedMap;
$map->add($key, $val); # throws exception if key already exists
$map->set($key, $val); # replaces value if key already exists

Retrieve:

$val = $map->get($key); # throws exception if key does not exist
if ($map->exists($key)) {
  # ...
}

Remove:

$map->del($key); # throws exception if key does not exist

The keys are always kept in their order when removing, this position index are available:

$index = $map->getKeyPosition($key);
$key = $map->getKayAt($index);
$val = $map->getValueAt($index);

Create from existing hashes or arrays:

use \Mfn\Util\Map\SimpleOrderedMap;
$map = SimpleOrderedMap::fromHash(['a'=>true,10=>NULL]);
$map->keys(); # ['a',10]
$map->values(): # [true,NULL]

# The same with separate arrays. Note: arrays length must match
$map = SimpleOrderedMap::fromArrays(
  ['a',10],
  [true,NULL]
);
$map->keys(); # ['a',10]
$map->values(): # [true,NULL]

Many more methods, please see the source of SimpleOrderedMap

Using with key/value validation

If you feel like you need some kind of validation, you can use SimpleOrderedValidatingMap which provides callbacks for checking new added key and values:

use Mfn\Util\Map\SimpleOrderedValidatingMap as Map;

$map = new Map(
              function($key) {
                if ($key instanceof \stdClass) {
                  return;
                }
                throw new \Mfn\Util\Map\SimpleOrderedValidatingMapException(
                  'Only keys of type stdClass are allowed'
                );
              },
              function($value) {
                if ($value instanceof \stdClass) {
                  return;
                }
                throw new \Mfn\Util\Map\SimpleOrderedValidatingMapException(
                  'Only values of type stdClass are allowed'
                );
              }
           );
# Throws an exception
$map->add(1,2);

Install

Using composer: composer.phar require mfn/util-simpleorderedmap 0.0.2

Contribute

Fork it, hack on a feature branch, create a pull request

© Markus Fischer markus@fischer.name