Download the PHP package apantle/hashmapper without Composer
On this page you can find all versions of the php package apantle/hashmapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download apantle/hashmapper
More information about apantle/hashmapper
Files in apantle/hashmapper
Package hashmapper
Short Description Simple Ordered Hashmap Object Transformer (array_map for associative arrays)
License MIT
Informations about the package hashmapper
Objective
This is a minimalistic library aimed to reuse logic on HashTables mapping, that i use over and over i.e. consuming API results to pass to Twig views.
Makes easy to do filtering/renaming of unwanted keys through simple dictionary of keys of source to target. Through callbacks supports any transformation, and passing it another callable or another instance of HashMapper, pretty complex transformation of associative arrays, a.k.a as Hashmaps for friends.
Installation
You can install the package via composer:
Simple key mapping
Change one key in input, only output that in target.
Input | Mapper | Output |
---|---|---|
Callback key mapping
For somewhat complex transforms, you can use a function that will receive as arguments:
- the value of the source hashmap specified at key:
- the whole hashmap if you need other values of it
Use another HashMapper
If you have a complex subkey that is not easily mapped with a simple function, you could use another HashMapper with the spec for that subkey, as the mapper for that key.
Spread Operator Mapping with Callable
If you want to take a key with subkeys of the source and spread it (copy
the dictionary of key and values it contains) on the target hashmap, you
can pass a tuple with the string '...'
as the target key, and your chosen callable.
Implicit spread (not specifying '...' key)
If you need all the dictionaries inside top level keys be spread into the
target, rather than writing your mapping spec as a tuple, you can give it
only a callable, specifying the option implicitSpread => true
in the
constructor to the Mapper functor.
Call a HashMapper as Functor object
For better reuse, now offers through the __invoke
magic, a simpler way to use
it to map a collection of associative arrays, as array_map
, array_reduce
or
Collection::map
(from Illuminate\Support
).
Reuse a HashMapper to transform an array of associative arrays
Instead of using the HashMapper as the function for array_map
or Collection::map
,
you can use our own helper, that applies the same set of transformations
to every array passed.
See issue:1 for more complete examples in test sources.