Bag
class Bag implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable (View source)
An OO implementation of PHP's array functionality and more (minus mutability).
All methods that allow mutation are deprecated, use MutableBag for those use cases instead.
Properties
protected array | $items |
Methods
Constructor.
Takes the items and recursively converts them to Bags.
Creates a bag by using one collection for keys and another for its values.
Returns the array of items.
Returns the items recursively converting them to arrays.
Creates a new instance from the specified items.
Returns whether an item exists for the given key.
Returns whether a key exists using path syntax to check nested data.
Returns whether the item is in the bag.
Returns an item by its key.
Returns an item using path syntax to retrieve nested data.
Returns the number of items in this bag.
Checks whether the bag is empty.
Returns the first item in the list or null if empty.
Returns the last item in the list or null if empty.
Joins the list to a string.
Returns the sum of the values in this list.
Returns the product of the values in this list.
Returns whether the items in this bag are key/value pairs.
Returns whether the items in this bag are zero indexed and sequential.
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.
Returns a random value.
Returns a random key.
Returns a mutable bag with the items from this bag.
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.
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
recursively added to the items in this bag
if they do not already exist by comparing keys. The opposite of 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 values mapped to the number of times they are in the bag.
Returns a bag with a random number of key/value pairs.
Returns a list with a random number of keys (as values).
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 based on the $iteratee
function.
Returns a bag with the keys sorted with the $comparator
.
Removes and returns the item at the specified $key
from the bag.
Details
at line 36
__construct(array $items = [])
Constructor.
at line 60
static Bag
from(iterable|stdClass|null $collection)
Create a bag from a variety of collections.
at line 72
static Bag
fromRecursive(iterable|stdClass|null $collection)
Takes the items and recursively converts them to Bags.
at line 94
static Bag
combine(iterable $keys, iterable $values)
Creates a bag by using one collection for keys and another for its values.
at line 115
array
toArray()
Returns the array of items.
at line 125
array
toArrayRecursive()
Returns the items recursively converting them to arrays.
at line 140
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.
at line 156
bool
has(string $key)
Returns whether an item exists for the given key.
at line 175
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 /
.
at line 189
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.
at line 202
mixed
get(string $key, mixed $default = null)
Returns an item by its key.
at line 224
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 /
.
at line 234
int
count()
Returns the number of items in this bag.
at line 244
bool
isEmpty()
Checks whether the bag is empty.
at line 254
mixed|null
first()
Returns the first item in the list or null if empty.
at line 264
mixed|null
last()
Returns the last item in the list or null if empty.
at line 276
string
join(string $separator)
Joins the list to a string.
at line 286
number
sum()
Returns the sum of the values in this list.
at line 296
number
product()
Returns the product of the values in this list.
at line 308
bool
isAssociative()
Returns whether the items in this bag are key/value pairs.
Note: Empty bags are not.
at line 320
bool
isIndexed()
Returns whether the items in this bag are zero indexed and sequential.
Note: Empty bags are.
at line 337
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.
at line 360
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.
at line 381
mixed|null
find(callable $predicate, int $fromIndex)
Returns the first item that matches the $predicate
or null.
at line 398
mixed|null
findLast(callable $predicate, int $fromIndex = null)
Returns the last item that matches the $predicate
or null.
at line 415
mixed|null
findKey(callable $predicate, int $fromIndex)
Returns the first key that matches the $predicate
or null.
at line 436
mixed|null
findLastKey(callable $predicate, int $fromIndex = null)
Returns the last key that matches the $predicate
or null.
at line 516
mixed
randomValue()
Returns a random value.
at line 528
mixed
randomKey()
Returns a random key.
at line 570
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', '', '']
at line 584
MutableBag
mutable()
Returns a mutable bag with the items from this bag.
at line 616
Bag
values()
Returns a bag with all the values of the items.
Useful for reindexing a list.
at line 631
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
.
at line 650
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.
at line 672
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
.
at line 694
Bag
reject(callable $predicate)
Returns a bag with the items that do not satisfy the $predicate
. The opposite of filter.
Keys are preserved, so lists could need to be re-indexed.
at line 724
Bag
replace(iterable $collection)
Replaces items in this bag from the $collection
by comparing keys and returns the result.
at line 743
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).
at line 762
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 replace.
Example:
Bag::from(['foo' => 'bar'])
->defaults(['foo' => 'other', 'hello' => 'world']);
// => Bag of ['foo' => 'bar', 'hello' => 'world']
at line 775
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 replaceRecursive.
at line 790
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 replace instead.
at line 807
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.
at line 826
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
});
at line 866
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']
at line 880
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.
at line 901
mixed
reduce(callable $callback, mixed $initial = null)
Iteratively reduce the items to a single value using the $callback
function.
at line 941
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]]
at line 974
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]
at line 990
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]
at line 1014
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]
at line 1077
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']
at line 1104
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']
at line 1128
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.
at line 1165
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.
at line 1192
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']
at line 1224
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
at line 1245
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.
at line 1283
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.
at line 1308
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']
at line 1340
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
at line 1418
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 |
at line 1466
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']
// ]
at line 1479
Bag
sortWith(callable $comparator, bool $preserveKeys = false)
Returns a bag with the values sorted with the $comparator
.
at line 1507
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 |
at line 1544
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']
at line 1556
Bag
sortKeysWith(callable $comparator)
Returns a bag with the keys sorted with the $comparator
.
at line 1625
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.
at line 1639
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.
at line 1654
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.
at line 1697
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.
at line 1709
clear()
deprecated
deprecated since 1.1 and will be removed in 2.0. Use MutableBag instead.
Remove all items from bag.
at line 1726
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.
at line 1747
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.
at line 1765
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.
at line 1779
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.