Upgrade to 3.1.0
This commit is contained in:
@@ -5,14 +5,16 @@ namespace Kirby\Toolkit;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* A set of handy methods to simplify array handling
|
||||
* and make it more consistent.
|
||||
* The `A` class provides a set of handy methods
|
||||
* to simplify array handling and make it more
|
||||
* consistent. The class contains methods for
|
||||
* fetching elements from arrays, merging and
|
||||
* sorting or shuffling arrays.
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class A
|
||||
{
|
||||
|
@@ -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
|
||||
*/
|
||||
|
@@ -3,9 +3,19 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Low level directory handling utilities
|
||||
* The `Dir` class provides methods
|
||||
* for dealing with directories on the
|
||||
* file system level, like creating,
|
||||
* listing, moving, copying or
|
||||
* evaluating directories etc.
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
*/
|
||||
class Dir
|
||||
{
|
||||
@@ -174,7 +184,11 @@ class Dir
|
||||
throw new Exception(sprintf('The directory "%s" does not exist and cannot be linked', $source));
|
||||
}
|
||||
|
||||
return symlink($source, $link);
|
||||
try {
|
||||
return symlink($source, $link) === true;
|
||||
} catch (Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -5,6 +5,13 @@ namespace Kirby\Toolkit;
|
||||
use Zend\Escaper\Escaper;
|
||||
|
||||
/**
|
||||
* The `Escape` class provides methods
|
||||
* for escaping common HTML attributes
|
||||
* data. This can be used to put
|
||||
* untrusted data into typical
|
||||
* attribute values like width, name,
|
||||
* value, etc.
|
||||
*
|
||||
* Wrapper for the Zend Escaper
|
||||
*
|
||||
* @link https://github.com/zendframework/zend-escaper
|
||||
|
@@ -3,12 +3,20 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
use Kirby\Http\Header;
|
||||
use Throwable;
|
||||
use ZipArchive;
|
||||
|
||||
/**
|
||||
* Low level file handling utilities
|
||||
* The `F` class provides methods for
|
||||
* dealing with files on the file system
|
||||
* level, like creating, reading,
|
||||
* deleting, copying or validatings files.
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
*/
|
||||
class F
|
||||
{
|
||||
@@ -324,7 +332,11 @@ class F
|
||||
throw new Exception(sprintf('The file "%s" does not exist and cannot be linked', $source));
|
||||
}
|
||||
|
||||
return $method($source, $link);
|
||||
try {
|
||||
return $method($source, $link) === true;
|
||||
} catch (Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -534,7 +546,7 @@ class F
|
||||
$parent = realpath($in);
|
||||
|
||||
if ($parent === false || is_dir($parent) === false) {
|
||||
throw new Exception(sprintf('The parent directory does not exist: "%s"', $parent));
|
||||
throw new Exception(sprintf('The parent directory does not exist: "%s"', $in));
|
||||
}
|
||||
|
||||
if (substr($realpath, 0, strlen($parent)) !== $parent) {
|
||||
|
@@ -28,7 +28,7 @@ class Iterator implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current key from the array
|
||||
* Returns the current key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -38,7 +38,7 @@ class Iterator implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all keys in the Iterator
|
||||
* Returns an array of all keys
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -48,7 +48,7 @@ class Iterator implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current element of the array
|
||||
* Returns the current element
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
@@ -58,7 +58,7 @@ class Iterator implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the cursor to the previous element in the array
|
||||
* Moves the cursor to the previous element
|
||||
* and returns it
|
||||
*
|
||||
* @return mixed
|
||||
@@ -69,7 +69,7 @@ class Iterator implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the cursor to the next element in the array
|
||||
* Moves the cursor to the next element
|
||||
* and returns it
|
||||
*
|
||||
* @return mixed
|
||||
@@ -80,7 +80,7 @@ class Iterator implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the cusor to the first element of the array
|
||||
* Moves the cusor to the first element
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
@@ -98,7 +98,7 @@ class Iterator implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts all elements in the array
|
||||
* Counts all elements
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@@ -130,7 +130,7 @@ class Iterator implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an element is in the collection by key.
|
||||
* Checks by key if an element is included
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return boolean
|
||||
|
@@ -5,7 +5,15 @@ namespace Kirby\Toolkit;
|
||||
use SimpleXMLElement;
|
||||
|
||||
/**
|
||||
* Mime type detection/guessing
|
||||
* The `Mime` class provides method
|
||||
* for mime type detection or guessing
|
||||
* from different criteria like
|
||||
* extensions etc.
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
*/
|
||||
class Mime
|
||||
{
|
||||
|
@@ -5,7 +5,9 @@ namespace Kirby\Toolkit;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* A set of handy string methods
|
||||
* The String class provides a set
|
||||
* of handy methods for string
|
||||
* handling and manipulation.
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
|
Reference in New Issue
Block a user