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\Traversable
object
array
\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 |
---|
callable
string
\Haldayne\Boost\new
staticcount() : integer
api |
---|
integer
diff(\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\Traversable
object
array
\Haldayne\Boost\enum
\Haldayne\Boost\new
staticevery(callable|string $expression) : boolean
api |
---|
callable
string
boolean
filter(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 |
---|
callable
string
\Haldayne\Boost\new
staticfirst(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 |
---|
callable
string
integer
\Haldayne\Boost\new
staticforget(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\MapOfCollections
get(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
mixed
getIterator() : \ArrayIterator
api |
---|
\ArrayIterator
has(mixed $key) : boolean
This is the object method equivalent of the magic isset($map[$key]);
api |
---|
mixed
boolean
intersect(\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\Traversable
object
array
\Haldayne\Boost\enum
\Haldayne\Boost\new
staticinto(\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\$target
isEmpty() : boolean
api |
---|
boolean
last(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 |
---|
callable
string
integer
\Haldayne\Boost\new
staticmap(callable|string $expression) : \Haldayne\Boost\Map
$nums = new Map(range(0, 9));
$doubled = $nums->map('$_0 * 2');
The expression receives two arguments:
$_0
$_1
The 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 |
---|
callable
string
\Haldayne\Boost\Map
merge(\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\Traversable
object
array
callable
mixed
\Haldayne\Boost\MapOfCollections
none(callable|string $expression) : boolean
api |
---|
callable
string
boolean
offsetExists(mixed $key) : boolean
mixed
boolean
offsetGet(mixed $key) : mixed
mixed
mixed
offsetSet(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 |
---|
callable
string
\Haldayne\Boost\MapOfCollections
pluck($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\Map
pop() : mixed
api |
---|
mixed
push($element) : \Haldayne\Boost\MapOfCollections
api | |
---|---|
fluent | This method is part of a fluent interface and will return the same instance |
\Haldayne\Boost\MapOfCollections
reduce(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 |
callable
string
mixed
callable
string
null
mixed
rekey(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 |
---|
callable
string
\Haldayne\Boost\new
staticset(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\MapOfCollections
some(callable|string $expression) : boolean
api |
---|
callable
string
boolean
toArray()
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 |
---|
callable
null
callable
null
mixed
allowed(mixed $value)
inherited_from | \Haldayne\Boost\GuardedMapAbstract::allowed() |
---|
allowed(mixed $value) : boolean
mixed
boolean
collection_to_array($collection) : array | boolean
\InvalidArgumentException |
---|
array
boolean
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.
callable
string
integer
null
\Haldayne\Boost\new
staticis_collection_like(mixed $value) : boolean
mixed
boolean
normalize(mixed $value)
This default implementation does nothing.
inherited_from | \Haldayne\Boost\GuardedMapAbstract::normalize() |
---|
normalize(mixed $value) : mixed
This default implementation does nothing.
mixed
mixed
passes(mixed $result) : boolean
This method provides a definitive reference for what this and all derived classes consider passing:
mixed
boolean
walk(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\MapOfCollections
walk_backward(callable $code) : \Haldayne\Boost\MapOfCollections
fluent | This method is part of a fluent interface and will return the same instance |
---|
callable
\Haldayne\Boost\MapOfCollections
call(callable|string $expression)
callable
string
\InvalidArgumentException |
---|
hash_to_key(string $hash) : mixed
string
mixed
key_to_hash(mixed $key) : string
mixed
\InvalidArgumentException |
---|
string
$array : array
array()
$map_key_to_hash : array
array()