Upgrade to 3.1.0
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Closure;
|
||||
use Countable;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
@@ -10,7 +11,7 @@ use Exception;
|
||||
* interface around arrays of arrays or objects,
|
||||
* with advanced filters, sorting, navigation and more.
|
||||
*/
|
||||
class Collection extends Iterator
|
||||
class Collection extends Iterator implements Countable
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -59,7 +60,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Low-level getter for items
|
||||
* Low-level getter for elements
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return mixed
|
||||
@@ -74,7 +75,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Low-level setter for collection items
|
||||
* Low-level setter for elements
|
||||
*
|
||||
* @param string $key string or array
|
||||
* @param mixed $value
|
||||
@@ -96,7 +97,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Low-level item remover
|
||||
* Low-level element remover
|
||||
*
|
||||
* @param mixed $key the name of the key
|
||||
*/
|
||||
@@ -106,7 +107,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an element to the data array
|
||||
* Appends an element
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param mixed $item
|
||||
@@ -124,17 +125,17 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates chunks of the same size
|
||||
* Creates chunks of the same size.
|
||||
* The last chunk may be smaller
|
||||
*
|
||||
* @param int $size Number of items per chunk
|
||||
* @return Collection A new collection with an item for each chunk and
|
||||
* @param int $size Number of elements per chunk
|
||||
* @return Collection A new collection with an element for each chunk and
|
||||
* a sub collection in each chunk
|
||||
*/
|
||||
public function chunk(int $size): self
|
||||
{
|
||||
// create a multidimensional array that is chunked with the given
|
||||
// chunk size keep keys of the items
|
||||
// chunk size keep keys of the elements
|
||||
$chunks = array_chunk($this->data, $size, true);
|
||||
|
||||
// convert each chunk to a subcollection
|
||||
@@ -167,7 +168,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter and setter for the collection data
|
||||
* Getter and setter for the data
|
||||
*
|
||||
* @param array $data
|
||||
* @return array|Collection
|
||||
@@ -188,7 +189,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone and remove all items from the collection
|
||||
* Clone and remove all elements from the collection
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -201,7 +202,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds all items to the collection
|
||||
* Adds all elements to the collection
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -212,7 +213,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the collection by a custom
|
||||
* Filters elements by a custom
|
||||
* filter function or an array of filters
|
||||
*
|
||||
* @param Closure $filter
|
||||
@@ -239,8 +240,8 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the collection by one of the predefined
|
||||
* filter methods.
|
||||
* Filters elements by one of the
|
||||
* predefined filter methods.
|
||||
*
|
||||
* @param string $field
|
||||
* @return self
|
||||
@@ -329,7 +330,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Find one or multiple collection items by id
|
||||
* Find one or multiple elements by id
|
||||
*
|
||||
* @param string ...$keys
|
||||
* @return mixed
|
||||
@@ -358,7 +359,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a single item by an attribute and its value
|
||||
* Find a single element by an attribute and its value
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
@@ -375,7 +376,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a single item by key (id)
|
||||
* Find a single element by key (id)
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
@@ -386,7 +387,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element from the array
|
||||
* Returns the first element
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
@@ -397,7 +398,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the array in reverse order
|
||||
* Returns the elements in reverse order
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -421,8 +422,8 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts an attribute value from the given item
|
||||
* in the collection. This is useful if items in the collection
|
||||
* Extracts an attribute value from the given element
|
||||
* in the collection. This is useful if elements in the collection
|
||||
* might be objects, arrays or anything else and you need to
|
||||
* get the value independently from that. We use it for filterBy.
|
||||
*
|
||||
@@ -463,10 +464,10 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Groups the collection by a given callback
|
||||
* Groups the elements by a given callback
|
||||
*
|
||||
* @param Closure $callback
|
||||
* @return Collection A new collection with an item for each group and a subcollection in each group
|
||||
* @return Collection A new collection with an element for each group and a subcollection in each group
|
||||
*/
|
||||
public function group(Closure $callback): Collection
|
||||
{
|
||||
@@ -497,7 +498,7 @@ class Collection extends Iterator
|
||||
// create a new entry for the group if it does not exist yet
|
||||
$groups[$value] = new static([$key => $item]);
|
||||
} else {
|
||||
// add the item to an existing group
|
||||
// add the element to an existing group
|
||||
$groups[$value]->set($key, $item);
|
||||
}
|
||||
}
|
||||
@@ -506,11 +507,11 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Groups the collection by a given field
|
||||
* Groups the elements by a given field
|
||||
*
|
||||
* @param string $field
|
||||
* @param bool $i
|
||||
* @return Collection A new collection with an item for each group and a subcollection in each group
|
||||
* @return Collection A new collection with an element for each group and a subcollection in each group
|
||||
*/
|
||||
public function groupBy(string $field, bool $i = true)
|
||||
{
|
||||
@@ -527,7 +528,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the collection has no items
|
||||
* Checks if the number of elements is zero
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@@ -537,7 +538,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the number of items in the collection is even
|
||||
* Checks if the number of elements is even
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@@ -547,7 +548,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the collection has no items
|
||||
* Checks if the number of elements is more than zero
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@@ -557,7 +558,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the number of items in the collection is odd
|
||||
* Checks if the number of elements is odd
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@@ -567,7 +568,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last element from the collection
|
||||
* Returns the last element
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
@@ -589,7 +590,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a function to each item in the collection
|
||||
* Map a function to each element
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return Collection
|
||||
@@ -771,7 +772,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new item to the collection
|
||||
* Adds a new element to the collection
|
||||
*
|
||||
* @param mixed $key string or array
|
||||
* @param mixed $value
|
||||
@@ -790,7 +791,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuffle all elements in the array
|
||||
* Shuffle all elements
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -829,7 +830,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the object by any number of fields
|
||||
* Sorts the elements by any number of fields
|
||||
*
|
||||
* @param $field string|callable Field name or value callback to sort by
|
||||
* @param $direction string asc or desc
|
||||
@@ -918,7 +919,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the current object into an array
|
||||
* Converts the object into an array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -932,7 +933,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the current object into a json string
|
||||
* Converts the object into a JSON string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -942,7 +943,7 @@ class Collection extends Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Convertes the collection to a string
|
||||
* Convertes the object to a string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
Reference in New Issue
Block a user