Upgrade to 3.2.5

This commit is contained in:
Bastian Allgeier
2019-09-24 11:00:59 +02:00
parent ff9b5b1861
commit 447a9dd266
234 changed files with 1990 additions and 1224 deletions

View File

@@ -19,7 +19,6 @@ use Exception;
*/
class Collection extends Iterator implements Countable
{
/**
* All registered collection filters
*
@@ -60,7 +59,7 @@ class Collection extends Iterator implements Countable
*
* @return array
*/
public function __debuginfo(): array
public function __debugInfo(): array
{
return $this->keys();
}
@@ -117,13 +116,13 @@ class Collection extends Iterator implements Countable
*
* @param mixed $key
* @param mixed $item
* @return Kirby\Toolkit\Collection
* @return \Kirby\Toolkit\Collection
*/
public function append(...$args)
{
if (count($args) === 1) {
$this->data[] = $args[0];
} elseif (count($args) === 2) {
} elseif (count($args) > 1) {
$this->set($args[0], $args[1]);
}
@@ -135,7 +134,7 @@ class Collection extends Iterator implements Countable
* The last chunk may be smaller
*
* @param int $size Number of elements per chunk
* @return Kirby\Toolkit\Collection A new collection with an element for each chunk and
* @return \Kirby\Toolkit\Collection A new collection with an element for each chunk and
* a sub collection in each chunk
*/
public function chunk(int $size)
@@ -197,7 +196,7 @@ class Collection extends Iterator implements Countable
/**
* Clone and remove all elements from the collection
*
* @return Kirby\Toolkit\Collection
* @return \Kirby\Toolkit\Collection
*/
public function empty()
{
@@ -210,6 +209,7 @@ class Collection extends Iterator implements Countable
/**
* Adds all elements to the collection
*
* @param mixed $items
* @return self
*/
public function extend($items)
@@ -250,6 +250,7 @@ class Collection extends Iterator implements Countable
* predefined filter methods.
*
* @param string $field
* @param array ...$args
* @return self
*/
public function filterBy(string $field, ...$args)
@@ -409,7 +410,7 @@ class Collection extends Iterator implements Countable
/**
* Returns the elements in reverse order
*
* @return Kirby\Toolkit\Collection
* @return \Kirby\Toolkit\Collection
*/
public function flip()
{
@@ -525,7 +526,7 @@ class Collection extends Iterator implements Countable
*
* @param string $field
* @param bool $i
* @return Kirby\Toolkit\Collection A new collection with an element for each group and a subcollection in each group
* @return \Kirby\Toolkit\Collection A new collection with an element for each group and a subcollection in each group
*/
public function groupBy($field, bool $i = true)
{
@@ -596,7 +597,7 @@ class Collection extends Iterator implements Countable
* Returns a new object with a limited number of elements
*
* @param int $limit The number of elements to return
* @return Kirby\Toolkit\Collection
* @return \Kirby\Toolkit\Collection
*/
public function limit(int $limit)
{
@@ -607,7 +608,7 @@ class Collection extends Iterator implements Countable
* Map a function to each element
*
* @param callable $callback
* @return Kirby\Toolkit\Collection
* @return \Kirby\Toolkit\Collection
*/
public function map(callable $callback)
{
@@ -629,8 +630,8 @@ class Collection extends Iterator implements Countable
/**
* Returns a Collection without the given element(s)
*
* @param args any number of keys, passed as individual arguments
* @return Kirby\Toolkit\Collection
* @param string ...$keys any number of keys, passed as individual arguments
* @return \Kirby\Toolkit\Collection
*/
public function not(...$keys)
{
@@ -645,7 +646,7 @@ class Collection extends Iterator implements Countable
* Returns a new object starting from the given offset
*
* @param int $offset The index to start from
* @return Kirby\Toolkit\Collection
* @return \Kirby\Toolkit\Collection
*/
public function offset(int $offset)
{
@@ -655,7 +656,8 @@ class Collection extends Iterator implements Countable
/**
* Add pagination
*
* @return Kirby\Toolkit\Collection a sliced set of data
* @param array ...$arguments
* @return \Kirby\Toolkit\Collection a sliced set of data
*/
public function paginate(...$arguments)
{
@@ -668,7 +670,7 @@ class Collection extends Iterator implements Countable
/**
* Get the previously added pagination object
*
* @return Kirby\Toolkit\Pagination|null
* @return \Kirby\Toolkit\Pagination|null
*/
public function pagination()
{
@@ -716,7 +718,7 @@ class Collection extends Iterator implements Countable
{
if (count($args) === 1) {
array_unshift($this->data, $args[0]);
} elseif (count($args) === 2) {
} elseif (count($args) > 1) {
$data = $this->data;
$this->data = [];
$this->set($args[0], $args[1]);
@@ -807,7 +809,7 @@ class Collection extends Iterator implements Countable
/**
* Shuffle all elements
*
* @return Kirby\Toolkit\Collection
* @return \Kirby\Toolkit\Collection
*/
public function shuffle()
{
@@ -830,7 +832,7 @@ class Collection extends Iterator implements Countable
*
* @param int $offset The optional index to start the slice from
* @param int $limit The optional number of elements to return
* @return Kirby\Toolkit\Collection
* @return \Kirby\Toolkit\Collection
*/
public function slice(int $offset = 0, int $limit = null)
{
@@ -942,7 +944,34 @@ class Collection extends Iterator implements Countable
$params[] = $field['flags'] ?? SORT_NATURAL | SORT_FLAG_CASE;
}
$params[] = &$array;
// check what kind of collection items we have; only check for the first
// item for better performance (we assume that all collection items are
// of the same type)
$firstItem = $collection->first();
if (is_object($firstItem) === true) {
// avoid the "Nesting level too deep - recursive dependency?" error
// when PHP tries to sort by the objects directly (in case all other
// fields are 100 % equal for some elements)
if (method_exists($firstItem, '__toString') === true) {
// PHP can easily convert the objects to strings, so it should
// compare them as strings instead of as objects to avoid the recursion
$params[] = &$array;
$params[] = SORT_STRING;
} else {
// we can't convert the objects to strings, so we need a fallback:
// custom fictional field that is guaranteed to have a unique value
// for each item; WARNING: may lead to slightly wrong sorting results
// and is therefore only used as a fallback if we don't have another way
$params[] = range(1, count($array));
$params[] = SORT_ASC;
$params[] = SORT_NUMERIC;
$params[] = &$array;
}
} else {
// collection items are scalar or array; no correction necessary
$params[] = &$array;
}
// array_multisort receives $params as separate params
array_multisort(...$params);
@@ -955,6 +984,7 @@ class Collection extends Iterator implements Countable
/**
* Converts the object into an array
*
* @param Closure $map
* @return array
*/
public function toArray(Closure $map = null): array
@@ -1000,8 +1030,8 @@ class Collection extends Iterator implements Countable
/**
* Alias for $this->not()
*
* @param args any number of keys, passed as individual arguments
* @return Kirby\Toolkit\Collection
* @param string ...$keys any number of keys, passed as individual arguments
* @return \Kirby\Toolkit\Collection
*/
public function without(...$keys)
{