Implements a map that may only contain collection-like values.
A collection-like value may be any of:
| package | Haldayne |
|---|---|
| inherited_from | \Haldayne\Boost\GuardedMapAbstract |
__construct(\Haldayne\Boost\Map|\Haldayne\Boost\Contract\Arrayable|\Haldayne\Boost\Contract\Jsonable|\Haldayne\Boost\Traversable|object|array $collection = null)
Initialize the map with the given collection, which can be any type that is "collection-like": array, object, Traversable, another Map, etc.
| api |
|---|
\Haldayne\Boost\Map\Haldayne\Boost\Contract\Arrayable\Haldayne\Boost\Contract\Jsonable\Haldayne\Boost\Traversableobjectarray
\InvalidArgumentException |
|---|
all(callable|string $expression) : \Haldayne\Boost\new
The expression decides whether an element is in or out. If the expression returns boolean false, the element is out. Otherwise, it's in.
$nums = new Map(range(0, 9));
$even = $nums->all(function ($val, $key) { return 0 == $val % 2; });
$odds = $nums->all('$_0 & 1');| api |
|---|
callablestring
\Haldayne\Boost\newstaticcount() : integer
| api |
|---|
integerdiff(\Haldayne\Boost\Map|\Haldayne\Boost\Contract\Arrayable|\Haldayne\Boost\Contract\Jsonable|\Haldayne\Boost\Traversable|object|array $collection, \Haldayne\Boost\enum $comparison = \Haldayne\Boost\Map::LOOSE) : \Haldayne\Boost\new
If comparison is loose, then only those elements whose values match will be removed. Otherwise, comparison is strict, and elements whose keys and values match will be removed.
| api |
|---|
\Haldayne\Boost\Map\Haldayne\Boost\Contract\Arrayable\Haldayne\Boost\Contract\Jsonable\Haldayne\Boost\Traversableobjectarray
\Haldayne\Boost\enum
\Haldayne\Boost\newstaticevery(callable|string $expression) : boolean
| api |
|---|
callablestring
booleanfilter(callable|string $expression) : \Haldayne\Boost\new
The filter expressions receives two arguments:
If the filter returns exactly boolean false, the element is not copied into the new map. Otherwise, it is. Keys from the original map carry into the new map.
| api |
|---|
callablestring
\Haldayne\Boost\newstaticfirst(callable|string $expression, integer $n = 1) : \Haldayne\Boost\new
Like find, but stop after finding N elements from the front. Defaults
to N = 1.
$nums = new Map(range(0, 9));
$odd3 = $nums->first('1 == ($_0 % 2)', 3); // first three odds| api |
|---|
callablestring
integer
\Haldayne\Boost\newstaticforget(mixed $key) : \Haldayne\Boost\MapOfCollections
This is the object method equivalent of the magic unset($map[$key]);
| api | |
|---|---|
| fluent | This method is part of a fluent interface and will return the same instance |
mixed
\Haldayne\Boost\MapOfCollectionsget(mixed $key, mixed $default = null) : mixed
If the key does not exist in the map, return the default.
This is the object method equivalent of the magic $map[$key].
| api |
|---|
mixed
mixed
mixedgetIterator() : \ArrayIterator
| api |
|---|
\ArrayIteratorhas(mixed $key) : boolean
This is the object method equivalent of the magic isset($map[$key]);
| api |
|---|
mixed
booleanintersect(\Haldayne\Boost\Map|\Haldayne\Boost\Contract\Arrayable|\Haldayne\Boost\Contract\Jsonable|\Haldayne\Boost\Traversable|object|array $collection, \Haldayne\Boost\enum $comparison = \Haldayne\Boost\Map::LOOSE) : \Haldayne\Boost\new
If comparison is loose, then only those elements whose value match will be included. Otherise, comparison is strict, and elements whose keys & values match will be included.
| api |
|---|
\Haldayne\Boost\Map\Haldayne\Boost\Contract\Arrayable\Haldayne\Boost\Contract\Jsonable\Haldayne\Boost\Traversableobjectarray
\Haldayne\Boost\enum
\Haldayne\Boost\newstaticinto(\Haldayne\Boost\Map $target) : \Haldayne\Boost\$target
$words = new MapOfStrings([ 'foo', 'bar' ]);
$words->map('strlen($_0)')->into(new MapOfInts)->sum(); // 6
Use when you've mapped your elements into a different type, and you want to fluently perform operations on the new type. In the example, the sum of the words' lengths was calculated.
| api |
|---|
\Haldayne\Boost\$targetisEmpty() : boolean
| api |
|---|
booleanlast(callable|string $expression, integer $n = 1) : \Haldayne\Boost\new
Like first, but stop after finding N elements from the end.
Defaults to N = 1.
$nums = new Map(range(0, 9));
$odds = $nums->last('1 == ($_0 % 2)', 2); // last two odd numbers| api |
|---|
callablestring
integer
\Haldayne\Boost\newstaticmap(callable|string $expression) : \Haldayne\Boost\Map
$nums = new Map(range(0, 9));
$doubled = $nums->map('$_0 * 2');
The expression receives two arguments:
$_0$_1The keys in the resulting map will be the same as the keys in the original map: only the values have (potentially) changed.
Recommended to use this method when you are mapping from one type to
the same type: int to int, string to string, etc. If you are changing
types, use the more powerful transform method.
| api |
|---|
callablestring
\Haldayne\Boost\Mapmerge(\Haldayne\Boost\Map|\Haldayne\Boost\Contract\Arrayable|\Haldayne\Boost\Contract\Jsonable|\Haldayne\Boost\Traversable|object|array $collection, callable $merger, mixed $default = null) : \Haldayne\Boost\MapOfCollections
The merger callable decides how to merge the current map's value with the given collection's value. The merger callable receives two arguments:
If the current map does not have a value for a key in the collection, then the default value is assumed.
| api | |
|---|---|
| fluent | This method is part of a fluent interface and will return the same instance |
\Haldayne\Boost\Map\Haldayne\Boost\Contract\Arrayable\Haldayne\Boost\Contract\Jsonable\Haldayne\Boost\Traversableobjectarray
callable
mixed
\Haldayne\Boost\MapOfCollectionsnone(callable|string $expression) : boolean
| api |
|---|
callablestring
booleanoffsetExists(mixed $key) : boolean
mixed
booleanoffsetGet(mixed $key) : mixed
mixed
mixedoffsetSet(mixed $key, mixed $value) : void
If key is null, the value is appended to the array using numeric indexes, just like native PHP. Unlike native-PHP, $key can be of any type: boolean, int, float, string, array, object, closure, resource.
mixed
mixed
offsetUnset(mixed $key) : void
mixed
partition(callable|string $expression) : \Haldayne\Boost\MapOfCollections
Calls the expression for each element in this map. The expression receives the value and key, respectively. The expression may return any value: this value is the grouping key and the element is put into that group.
$nums = new Map(range(0, 9));
$part = $nums->partition(function ($value, $key) {
return 0 == $value % 2 ? 'even' : 'odd';
});
var_dump(
$part['odd']->count(), // 5
array_sum($part['even']->toArray()) // 20
);| api |
|---|
callablestring
\Haldayne\Boost\MapOfCollectionspluck($key_for_value, $key_for_key = null) : \Haldayne\Boost\Map
Thinking of a Map of Collections as a two-dimensional array, this method slices a column out:
use Haldayne\Boost\MapOfCollections;
$map = new MapOfCollections;
$map->push([ 'id' => 5, 'name' => 'Ada', 'age' => 16 ]);
$map->push([ 'id' => 6, 'name' => 'Bee', 'age' => 12 ]);
$map->push([ 'id' => 7, 'name' => 'Cam', 'age' => 37 ]);
var_dump(
$map->pluck('name')->toArray(); // [ 'Ada', 'Bee', 'Cam' ]
$map->pluck('name', 'id')->toArray(); // [ 5=>'Ada', 6=>'Bee', 7=>'Cam' ]
);| api |
|---|
\Haldayne\Boost\Mappop() : mixed
| api |
|---|
mixedpush($element) : \Haldayne\Boost\MapOfCollections
| api | |
|---|---|
| fluent | This method is part of a fluent interface and will return the same instance |
\Haldayne\Boost\MapOfCollectionsreduce(callable|string $reducer, mixed $initial= null, callable|string|null $finisher= null) : mixed
The $reducer expression receives three arguments:
$_0)$_1)$_2)The initial value, if given or null if not, is passed as the current
reduction on the first invocation of $reducer. The return value from
$reducer then becomes the new, current reduced value.
$nums = new Map(range(0, 3));
$sum = $nums->reduce('$_0 + $_1');
// $sum == 6
If $finisher is a callable or string expression, then it will be
called last, after iterating over all elements. It will be passed
reduced value. The $finisher must return the new final value.
| api | |
|---|---|
| see |
callablestring
mixed
callablestringnull
mixedrekey(callable|string $expression) : \Haldayne\Boost\new
$keyed_by_bytecode = new Map(count_chars('war of the worlds', 1));
$keyed_by_letter = $keyed_by_bytecode->rekey('chr($_1)');| api |
|---|
callablestring
\Haldayne\Boost\newstaticset(mixed $key, mixed $value) : \Haldayne\Boost\MapOfCollections
This is the object method equivalent of the magic $map[$key] = 'foo'.
| api | |
|---|---|
| fluent | This method is part of a fluent interface and will return the same instance |
mixed
mixed
\Haldayne\Boost\MapOfCollectionssome(callable|string $expression) : boolean
| api |
|---|
callablestring
booleantoArray()
| api | |
|---|---|
| inherited_from | \Haldayne\Boost\Contract\Arrayable::toArray() |
toJson($options = 0)
| api | |
|---|---|
| inherited_from | \Haldayne\Boost\Contract\Jsonable::toJson() |
transform(callable $transformer, callable|null $creator= null, callable|null $finisher= null) : mixed
// transform a word list into a map of word to frequency in the list
use Haldayne\Boost\Map;
$words = new Map([ 'bear', 'bee', 'goose', 'bee' ]);
$lengths = $words->transform(
function (Map $new, $word) {
if ($new->has($word)) {
$new->set($word, $new->get($word)+1);
} else {
$new->set($word, 1);
}
}
);
Sometimes you need to create one map from another using a strategy that isn't one-to-one. You may need to change keys. You may need to add multiple elements. You may need to delete elements. You may need to change from a map to a number.
Whatever the case, the other simpler methods in Map don't quite fit the problem. What you need, and what this method provides, is a complete machine to transform this map into something else:
// convert a word list into a count of unique letters in those words
use Haldayne\Boost\Map;
$words = new Map([ 'bear', 'bee', 'goose', 'bee' ]);
$letters = $words->transform(
function ($frequencies, $word) {
foreach (count_chars($word, 1) as $byte => $frequency) {
$letter = chr($byte);
if ($frequencies->has($letter)) {
$new->set($letter, $frequencies->get($letter)+1);
} else {
$new->set($letter, 1);
}
}
},
function (Map $original) { return new MapOfIntegers(); },
function (MapOfIntegers $new) { return $new->sum(); }
);
This method accepts three callables
$creator, which is called first with the current map, performs any
initialization needed. The result of this callable will be passed to
all the other callables. If no creator is given, then use a default
one that returns an empty Map.
$transformer, which is called for every element in this map and
receives the initialized value, the current value, and the current key
in that order. The transformer should modify the initialized value
appropriately. Often this means adding to a new map zero or more
tranformed values.
$finisher, which is called last, receives the initialized value
that was modified by the transformer calls. The finisher may transform
that value once more as needed. If no finisher given, then no finishing
step is made.| api |
|---|
callablenull
callablenull
mixedallowed(mixed $value)
| inherited_from | \Haldayne\Boost\GuardedMapAbstract::allowed() |
|---|
allowed(mixed $value) : boolean
mixed
booleancollection_to_array($collection) : array | boolean
\InvalidArgumentException |
|---|
arrayboolean
grep(callable|string $expression, integer|null $limit = null) : \Haldayne\Boost\new
If limit is null, no limit on number of matches. If limit is positive, return that many from the front of the array. If limit is negative, return that many from the end of the array.
callablestring
integernull
\Haldayne\Boost\newstaticis_collection_like(mixed $value) : boolean
mixed
booleannormalize(mixed $value)
This default implementation does nothing.
| inherited_from | \Haldayne\Boost\GuardedMapAbstract::normalize() |
|---|
normalize(mixed $value) : mixed
This default implementation does nothing.
mixed
mixedpasses(mixed $result) : boolean
This method provides a definitive reference for what this and all derived classes consider passing:
mixed
booleanwalk(callable $code) : \Haldayne\Boost\MapOfCollections
The items are walked in the order they exist in the map. If the code returns boolean false, then the iteration halts. Values can be modified from within the callback, but not keys.
Example:
$map->each(function (&$value, $key) { $value++; return true; })->sum();| fluent | This method is part of a fluent interface and will return the same instance |
|---|
callable
\Haldayne\Boost\MapOfCollectionswalk_backward(callable $code) : \Haldayne\Boost\MapOfCollections
| fluent | This method is part of a fluent interface and will return the same instance |
|---|
callable
\Haldayne\Boost\MapOfCollectionscall(callable|string $expression)
callablestring
\InvalidArgumentException |
|---|
hash_to_key(string $hash) : mixed
string
mixedkey_to_hash(mixed $key) : string
mixed
\InvalidArgumentException |
|---|
string$array : array
array()$map_key_to_hash : array
array()