ImmutableBag deprecated
class ImmutableBag extends Bag (View source)
deprecated since 1.1 and will be removed in 2.0. Use Bag instead.
Properties
protected array | $items | from Bag |
Methods
Takes the items and recursively converts them to Bags.
Creates a bag by using one collection for keys and another for its values.
Returns whether a key exists using path syntax to check nested data.
Returns an item using path syntax to retrieve nested data.
Gets the first index/key of a given item.
Gets the last index/key of a given item.
Returns the first item that matches the $predicate
or null.
Returns the last item that matches the $predicate
or null.
Returns the first key that matches the $predicate
or null.
Returns the last key that matches the $predicate
or null.
Applies the $callable
to each value in the bag and returns
a new bag with the items returned by the function.
Applies the given function to each key in the bag and returns a new bag with the keys returned by the function and their values.
Returns a bag with the items that do not satisfy the $predicate
. The opposite of Bag::filter.
Replaces items in this bag from the $collection
by comparing keys and returns the result.
Returns a bag with the items replaced recursively from the $collection
.
Returns a bag with the items from the $collection
added to the items in this bag
if they do not already exist by comparing keys. The opposite of Bag::replace.
Returns a bag with the items from the $collection
recursively added to the items in this bag
if they do not already exist by comparing keys. The opposite of Bag::replaceRecursive.
Returns a bag with a slice of $length
items starting at position $offset
extracted from this bag.
Partitions the items into two bags according to the $predicate
.
Returns a bag with the values from a single column, identified by the $columnKey
.
Iteratively reduce the items to a single value using the $callback
function.
Returns a bag with the items split into chunks.
Returns a bag with the items padded to the $size
with the $value
.
Returns a bag with values mapped to the number of times they are in the bag.
Returns a bag without the values that are also in $collection
.
Returns a bag without the values that are also in $collection
based on the $iteratee
function.
Returns a bag without the keys that are also in $collection
.
Returns a bag without the keys that are also in $collection
based on the $iteratee
function.
Returns a bag with only the values that are also in $collection
.
Returns a bag with only the values that are also in $collection
based on the $iteratee
function.
Returns a bag with only the keys that are also in $collection
.
Returns a bag with only the keys that are also in $collection
based on the $iteratee
function.
Returns a bag with the values sorted.
Returns a bag with the values sorted based on the $iteratee
function.
Returns a bag with the values sorted with the $comparator
.
Returns a bag with the keys sorted.
Returns a bag with the keys sorted based on the $iteratee
function.
Returns a bag with the keys sorted with the $comparator
.
Details
static Bag
fromRecursive(iterable|stdClass|null $collection)
Takes the items and recursively converts them to Bags.
static Bag
combine(iterable $keys, iterable $values)
Creates a bag by using one collection for keys and another for its values.
array
toArrayRecursive()
Returns the items recursively converting them to arrays.
protected Bag
createFrom(array $items)
Creates a new instance from the specified items.
This method is provided for derived classes to specify how a new instance should be created when constructor semantics have changed.
bool
hasPath(string $path)
Returns whether a key exists using path syntax to check nested data.
Example:
$bag->hasPath('foo/bar/baz')
// => true
This method does not allow for keys that contain /
.
bool
hasItem(mixed $item)
Returns whether the item is in the bag.
This uses a strict check so types must much and objects must be the same instance to match.
mixed
getPath(string $path, mixed $default = null)
Returns an item using path syntax to retrieve nested data.
Example:
// Get the bar key of a set of nested arrays.
// This is equivalent to $data['foo']['baz']['bar'] but won't
// throw warnings for missing keys.
$bag->getPath('foo/bar/baz');
This method does not allow for keys that contain /
.
bool
isAssociative()
Returns whether the items in this bag are key/value pairs.
Note: Empty bags are not.
bool
isIndexed()
Returns whether the items in this bag are zero indexed and sequential.
Note: Empty bags are.
int|string|null
indexOf(mixed $item, int $fromIndex)
Gets the first index/key of a given item.
This uses a strict check so types must much and objects must be the same instance to match.
int|string|null
lastIndexOf(mixed $item, int $fromIndex = null)
Gets the last index/key of a given item.
This uses a strict check so types must much and objects must be the same instance to match.
mixed|null
find(callable $predicate, int $fromIndex)
Returns the first item that matches the $predicate
or null.
mixed|null
findLast(callable $predicate, int $fromIndex = null)
Returns the last item that matches the $predicate
or null.
mixed|null
findKey(callable $predicate, int $fromIndex)
Returns the first key that matches the $predicate
or null.
mixed|null
findLastKey(callable $predicate, int $fromIndex = null)
Returns the last key that matches the $predicate
or null.
Bag
call(callable $callable, array $args = null)
Calls the $callable
to modify the items.
This allows for chain-ability with custom functionality.
The $callable
is given the bag's items (array) as the first parameter and should return an iterable which is
then converted to a bag. Any extra parameters passed in to this method are passed to the $callable
after
the items parameter.
Example with closure:
Bag::from(['red', 'blue'])
->call(function (array $colors) {
$colors[] = 'green';
return $colors;
})
->join(', ');
// => "red, blue, green"
Example with function name and args:
Bag::from(['red', 'blue'])
->call('array_pad', 4, ''); // Assuming bag doesn't have a pad method ;)
// => Bag of ['red', 'blue', '', '']
MutableBag
mutable()
Returns a mutable bag with the items from this bag.
Bag
values()
Returns a bag with all the values of the items.
Useful for reindexing a list.
Bag
map(callable $callback)
Applies the $callable
to each value in the bag and returns
a new bag with the items returned by the function.
Note: This differs from array_map in that the callback is passed $key
first, then $value
.
Bag
mapKeys(callable $callback)
Applies the given function to each key in the bag and returns a new bag with the keys returned by the function and their values.
Bag
filter(callable $predicate)
Returns a bag with the items that satisfy the $predicate
.
Keys are preserved, so lists could need to be re-indexed.
Note: This differs from array_filter in that the $predicate
is passed $key
first, then $value
.
Bag
reject(callable $predicate)
Returns a bag with the items that do not satisfy the $predicate
. The opposite of Bag::filter.
Keys are preserved, so lists could need to be re-indexed.
Bag
replace(iterable $collection)
Replaces items in this bag from the $collection
by comparing keys and returns the result.
Bag
replaceRecursive(iterable $collection)
Returns a bag with the items replaced recursively from the $collection
.
This differs from array_replace_recursive in a couple ways:
Lists (zero indexed and sequential items) from given collection completely replace lists in this Bag.
Null values from given collection do not replace lists or associative arrays in this Bag (they do still replace scalar values).
Bag
defaults(iterable $collection)
Returns a bag with the items from the $collection
added to the items in this bag
if they do not already exist by comparing keys. The opposite of Bag::replace.
Example:
Bag::from(['foo' => 'bar'])
->defaults(['foo' => 'other', 'hello' => 'world']);
// => Bag of ['foo' => 'bar', 'hello' => 'world']
Bag
defaultsRecursive(iterable $collection)
Returns a bag with the items from the $collection
recursively added to the items in this bag
if they do not already exist by comparing keys. The opposite of Bag::replaceRecursive.
Bag
merge(iterable $list)
Returns a bag with the items merged with the given list.
Note: This should only be used for lists (zero indexed and sequential items). For associative arrays, use Bag::replace instead.
Bag
slice(int $offset, int|null $length = null, bool $preserveKeys = false)
Returns a bag with a slice of $length
items starting at position $offset
extracted from this bag.
Bag[]
partition(callable $predicate)
Partitions the items into two bags according to the $predicate
.
Keys are preserved in the resulting bags.
Example:
[$trueItems, $falseItems] = $bag->partition(function ($key, $item) {
return true; // whatever logic
});
Bag
column(string|int|null $columnKey, string|int|null $indexKey = null)
Returns a bag with the values from a single column, identified by the $columnKey
.
Optionally, an $indexKey
may be provided to index the values in the
returned Bag by the values from the $indexKey
column.
Example:
$bag = Bag::from([
['id' => 10, 'name' => 'Alice'],
['id' => 20, 'name' => 'Bob'],
['id' => 30, 'name' => 'Carson'],
]);
$bag->column('name');
// => Bag of ['Alice', 'Bob', 'Carson']
$bag->column('name', 'id');
// => Bag of [10 => 'Alice', 20 => 'Bob', 30 => 'Carson']
Bag
flip()
Returns a bag with all keys exchanged with their associated values.
If a value has several occurrences, the latest key will be used as its value, and all others will be lost.
mixed
reduce(callable $callback, mixed $initial = null)
Iteratively reduce the items to a single value using the $callback
function.
Bag|Bag[]
chunk(int $size, bool $preserveKeys = false)
Returns a bag with the items split into chunks.
The last chunk may contain less items.
Example:
Bag::from([1, 2, 3, 4, 5])
->chunk(2);
// => Bag of [Bag of [1, 2], Bag of [3, 4], Bag of [5]]
Bag
pad(int $size, mixed $value)
Returns a bag with the items padded to the $size
with the $value
.
If size is positive then the array is padded on the right. If it's negative then on the left.
Examples:
$bag = Bag::from([1, 2]);
$bag->pad(4, null);
// => Bag of [1, 2, null, null]
$bag->pad(-4, null);
// => Bag of [null, null, 1, 2]
$bag->pad(2, null);
// => Bag of [1, 2]
Bag
countValues()
Returns a bag with values mapped to the number of times they are in the bag.
Example:
Bag::from(['hello', 'world', 'world'])
->countValues();
// => Bag of ['hello' => 1, 'world' => 2]
Bag
flatten(int $depth = 1)
Returns a bag with the items flattened.
Example:
$bag = Bag::from([[1, 2], [[3]], 4])
// Flatten one level
$bag->flatten()
// => Bag of [1, 2, [3], 4]
// Flatten all levels
$bag->flatten(INF)
// => Bag of [1, 2, 3, 4]
Bag
randomValues(int $size)
Returns a bag with a random number of key/value pairs.
Bag
randomKeys(int $size)
Returns a list with a random number of keys (as values).
Bag
pick(iterable|string|string[]|int|int[] $keys)
Returns a bag with only $keys
.
$keys
should be passed in as multiple parameters if possible.
But if you have a list of keys they can be passed in as the first parameter.
Note that if you can use PHP 5.6+ syntax, you can do pick(...$keys)
which is preferred.
Example:
Bag::from([
'a' => 'red',
'b' => 'blue',
'c' => 'green',
])
->pick('a', 'c', 'd');
// => Bag of ['a' => 'red', 'c' => 'green']
Bag
omit(iterable|string|string[]|int|int[] $keys)
Returns a bag without $keys
.
$keys
should be passed in as multiple parameters if possible.
But if you have a list of keys they can be passed in as the first parameter.
Note that if you can use PHP 5.6+ syntax, you can do omit(...$keys)
which is preferred.
Example:
Bag::from([
'a' => 'red',
'b' => 'blue',
'c' => 'green',
])
->omit('a', 'c', 'd');
// => Bag of ['b' => 'blue']
Bag
diff(iterable $collection, callable $comparator = null)
Returns a bag without the values that are also in $collection
.
The order is determined by this bag.
Example:
Bag::from(['red', 'blue', 'green'])
->diff(['blue', 'black']);
// => Bag of ['red', 'green']
Keys are preserved, so lists could need to be re-indexed.
Bag
diffBy(iterable $collection, callable $iteratee)
Returns a bag without the values that are also in $collection
based on the $iteratee
function.
Example:
$bag = Bag::from([
['name' => 'Alice'],
['name' => 'Bob'],
['name' => 'Carson'],
]);
$itemsToRemove = [
['name' => 'Bob'],
['name' => 'Carson'],
['name' => 'David'],
];
// Compare each value by its 'name' property
$bag->diffBy($itemsToRemove, function ($item) {
return $item['name'];
});
// => Bag of [
// ['name' => 'Alice']
// ]
// Both items with name 'Bob' and 'Carson' are removed since they are also in $itemsToRemove
Keys are preserved, so lists could need to be re-indexed.
Bag
diffKeys(iterable $collection, callable $comparator = null)
Returns a bag without the keys that are also in $collection
.
The order is determined by this bag.
Example:
Bag::from([
'a' => 'red',
'b' => 'blue',
'c' => 'green',
])->diffKeys([
'b' => 'value does not matter',
'd' => 'something',
]);
// => Bag of ['a' => 'red', 'c' => 'green']
Bag
diffKeysBy(iterable $collection, callable $iteratee)
Returns a bag without the keys that are also in $collection
based on the $iteratee
function.
Example:
$bag = Bag::from([
'a' => 'red',
'B' => 'blue',
'c' => 'green',
]);
$itemsToRemove = [
'b' => null,
'C' => null,
'D' => null,
];
// Compare each key case-insensitively
$bag->diffKeysBy($itemsToRemove, 'strtolower');
// => Bag of ['a' => 'red']
// Keys 'B' and 'c' are removed since all keys are compared after
// being lower-cased and 'b' and 'C' are also in $itemsToRemove
Bag
intersect(iterable $collection, callable $comparator = null)
Returns a bag with only the values that are also in $collection
.
Example:
Bag::from(['red', 'blue', 'green'])
->intersect(['blue', 'black']);
// => Bag of ['blue']
Keys are preserved, so lists could need to be re-indexed.
Bag
intersectBy(iterable $collection, callable $iteratee)
Returns a bag with only the values that are also in $collection
based on the $iteratee
function.
Example:
$bag = Bag::from([
['name' => 'Alice'],
['name' => 'Bob'],
['name' => 'Carson'],
]);
$itemsToKeep = [
['name' => 'Bob'],
['name' => 'Carson'],
['name' => 'David'],
];
// Compare each value by its 'name' property
$bag->intersectBy($itemsToKeep, function ($item) {
return $item['name'];
});
// => Bag of [
// ['name' => 'Bob']
// ['name' => 'Carson']
// ]
// Both items with name 'Bob' and 'Carson' are kept since they are also in $itemsToKeep
Keys are preserved, so lists could need to be re-indexed.
Bag
intersectKeys(iterable $collection, callable $comparator = null)
Returns a bag with only the keys that are also in $collection
.
Example:
Bag::from([
'a' => 'red',
'b' => 'blue',
'c' => 'green',
])->intersectKeys([
'b' => 'value does not matter',
'd' => 'something',
]);
// => Bag of ['b' => 'blue']
Bag
intersectKeysBy(iterable $collection, callable $iteratee)
Returns a bag with only the keys that are also in $collection
based on the $iteratee
function.
Example:
$bag = Bag::from([
'a' => 'red',
'B' => 'blue',
'c' => 'green',
]);
$itemsToKeep = [
'b' => null,
'C' => null,
'D' => null,
];
// Compare each key case-insensitively
$bag->intersectKeysBy($itemsToKeep, 'strtolower');
// => Bag of ['B' => 'blue', 'c' => 'green']
// Keys 'B' and 'c' are kept since all keys are compared after
// being lower-cased and 'b' and 'C' are also in $itemsToKeep
Bag
sort(int $order = SORT_ASC, int $flags = SORT_REGULAR, bool $preserveKeys = false)
Returns a bag with the values sorted.
Sorting flags:
Constant | Description |
---|---|
SORT_REGULAR |
compare values without changing types |
SORT_NUMERIC |
compare values numerically |
SORT_STRING |
compare values as strings |
SORT_STRING | SORT_FLAG_CASE |
compare values as strings ignoring case |
SORT_LOCALE_STRING |
compare values as strings based on the current locale |
SORT_NATURAL |
compare values as strings using "natural ordering" |
SORT_NATURAL | SORT_FLAG_CASE |
compare values as strings using "natural ordering" ignoring case |
Bag
sortBy(callable $iteratee, int $order = SORT_ASC, bool $preserveKeys = false)
Returns a bag with the values sorted based on the $iteratee
function.
Example:
$bag = Bag::from([
['name' => 'Bob'],
['name' => 'Alice'],
]);
// Sort values by "name" property
$bag->sortBy(function ($item) {
return $item['name'];
});
// => Bag of [
// ['name' => 'Alice']
// ['name' => 'Bob']
// ]
Bag
sortWith(callable $comparator, bool $preserveKeys = false)
Returns a bag with the values sorted with the $comparator
.
Bag
sortKeys(int $order = SORT_ASC, int $flags = SORT_REGULAR)
Returns a bag with the keys sorted.
Sorting flags:
Constant | Description |
---|---|
SORT_REGULAR |
compare values without changing types |
SORT_NUMERIC |
compare values numerically |
SORT_STRING |
compare values as strings |
SORT_STRING | SORT_FLAG_CASE |
compare values as strings ignoring case |
SORT_LOCALE_STRING |
compare values as strings based on the current locale |
SORT_NATURAL |
compare values as strings using "natural ordering" |
SORT_NATURAL | SORT_FLAG_CASE |
compare values as strings using "natural ordering" ignoring case |
Bag
sortKeysBy(callable $iteratee, int $order = SORT_ASC)
Returns a bag with the keys sorted based on the $iteratee
function.
Example:
$bag = Bag::from([
'blue' => 'a',
'red' => 'b',
'black' => 'c',
]);
// Sort keys by first letter
$bag->sortKeysBy(function ($key) {
return $key[0];
});
// Bag of ['blue' => 'a', 'black' => 'c', 'red' => 'b']
Bag
sortKeysWith(callable $comparator)
Returns a bag with the keys sorted with the $comparator
.
add(mixed $item)
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Adds an item to the end of this bag.
prepend(mixed $item)
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Adds an item to the beginning of this bag.
set(string $key, mixed $value)
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Sets a item by key.
setPath(string $path, mixed $value)
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Sets a value using path syntax to set nested data.
Inner arrays will be created as needed to set the value.
Example:
// Set an item at a nested structure
$bag->setPath('foo/bar', 'color');
// Append to a list in a nested structure
$bag->get($data, 'foo/baz');
// => null
$bag->setPath('foo/baz/[]', 'a');
$bag->setPath('foo/baz/[]', 'b');
$bag->getPath('foo/baz');
// => ['a', 'b']
This function does not support keys that contain /
or []
characters
because these are special tokens used when traversing the data structure.
A value may be appended to an existing array by using []
as the final
key of a path.
Note: To set values in arrays that are in ArrayAccess
objects their
offsetGet()
method needs to be able to return arrays by reference.
See MutableBag for an example of this.
clear()
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Remove all items from bag.
mixed
remove(string|int $key, mixed|null $default = null)
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Removes and returns the item at the specified $key
from the bag.
removeItem(mixed $item)
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Removes the given item from the bag if it is found.
mixed|null
removeFirst()
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Removes and returns the first item in the list.
mixed|null
removeLast()
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Removes and returns the last item in the list.