Upgrade to 3.2.0
This commit is contained in:
@@ -13,8 +13,9 @@ use Exception;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class A
|
||||
{
|
||||
|
@@ -10,6 +10,12 @@ use Exception;
|
||||
* The collection class provides a nicer
|
||||
* interface around arrays of arrays or objects,
|
||||
* with advanced filters, sorting, navigation and more.
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Collection extends Iterator implements Countable
|
||||
{
|
||||
@@ -111,7 +117,7 @@ class Collection extends Iterator implements Countable
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param mixed $item
|
||||
* @return Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function append(...$args)
|
||||
{
|
||||
@@ -129,7 +135,7 @@ class Collection extends Iterator implements Countable
|
||||
* The last chunk may be smaller
|
||||
*
|
||||
* @param int $size Number of elements per chunk
|
||||
* @return 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)
|
||||
@@ -162,7 +168,7 @@ class Collection extends Iterator implements Countable
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function clone(): self
|
||||
public function clone()
|
||||
{
|
||||
return clone $this;
|
||||
}
|
||||
@@ -191,7 +197,7 @@ class Collection extends Iterator implements Countable
|
||||
/**
|
||||
* Clone and remove all elements from the collection
|
||||
*
|
||||
* @return Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function empty()
|
||||
{
|
||||
@@ -204,9 +210,9 @@ class Collection extends Iterator implements Countable
|
||||
/**
|
||||
* Adds all elements to the collection
|
||||
*
|
||||
* @return Collection
|
||||
* @return self
|
||||
*/
|
||||
public function extend($items): self
|
||||
public function extend($items)
|
||||
{
|
||||
$collection = clone $this;
|
||||
return $collection->set($items);
|
||||
@@ -366,11 +372,11 @@ class Collection extends Iterator implements Countable
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function findBy(string $attribute, $value)
|
||||
{
|
||||
foreach ($this->data as $key => $item) {
|
||||
foreach ($this->data as $item) {
|
||||
if ($this->getAttribute($item, $attribute) == $value) {
|
||||
return $item;
|
||||
}
|
||||
@@ -384,7 +390,7 @@ class Collection extends Iterator implements Countable
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function findByKey($key)
|
||||
public function findByKey(string $key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
@@ -403,7 +409,7 @@ class Collection extends Iterator implements Countable
|
||||
/**
|
||||
* Returns the elements in reverse order
|
||||
*
|
||||
* @return Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function flip()
|
||||
{
|
||||
@@ -475,9 +481,9 @@ class Collection extends Iterator implements Countable
|
||||
* Groups the elements by a given callback
|
||||
*
|
||||
* @param Closure $callback
|
||||
* @return Collection A new collection with an element for each group and a subcollection in each group
|
||||
* @return self A new collection with an element for each group and a subcollection in each group
|
||||
*/
|
||||
public function group(Closure $callback): Collection
|
||||
public function group(Closure $callback)
|
||||
{
|
||||
$groups = [];
|
||||
|
||||
@@ -519,7 +525,7 @@ class Collection extends Iterator implements Countable
|
||||
*
|
||||
* @param string $field
|
||||
* @param bool $i
|
||||
* @return 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)
|
||||
{
|
||||
@@ -590,7 +596,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 Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function limit(int $limit)
|
||||
{
|
||||
@@ -601,7 +607,7 @@ class Collection extends Iterator implements Countable
|
||||
* Map a function to each element
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function map(callable $callback)
|
||||
{
|
||||
@@ -624,7 +630,7 @@ 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 Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function not(...$keys)
|
||||
{
|
||||
@@ -639,7 +645,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 Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function offset(int $offset)
|
||||
{
|
||||
@@ -649,7 +655,7 @@ class Collection extends Iterator implements Countable
|
||||
/**
|
||||
* Add pagination
|
||||
*
|
||||
* @return Collection a sliced set of data
|
||||
* @return Kirby\Toolkit\Collection a sliced set of data
|
||||
*/
|
||||
public function paginate(...$arguments)
|
||||
{
|
||||
@@ -662,7 +668,7 @@ class Collection extends Iterator implements Countable
|
||||
/**
|
||||
* Get the previously added pagination object
|
||||
*
|
||||
* @return Pagination|null
|
||||
* @return Kirby\Toolkit\Pagination|null
|
||||
*/
|
||||
public function pagination()
|
||||
{
|
||||
@@ -704,9 +710,9 @@ class Collection extends Iterator implements Countable
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param mixed $item
|
||||
* @return Collection
|
||||
* @return self
|
||||
*/
|
||||
public function prepend(...$args): self
|
||||
public function prepend(...$args)
|
||||
{
|
||||
if (count($args) === 1) {
|
||||
array_unshift($this->data, $args[0]);
|
||||
@@ -786,7 +792,7 @@ class Collection extends Iterator implements Countable
|
||||
* @param mixed $value
|
||||
* @return self
|
||||
*/
|
||||
public function set($key, $value = null): self
|
||||
public function set($key, $value = null)
|
||||
{
|
||||
if (is_array($key)) {
|
||||
foreach ($key as $k => $v) {
|
||||
@@ -801,7 +807,7 @@ class Collection extends Iterator implements Countable
|
||||
/**
|
||||
* Shuffle all elements
|
||||
*
|
||||
* @return Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function shuffle()
|
||||
{
|
||||
@@ -824,7 +830,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 Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function slice(int $offset = 0, int $limit = null)
|
||||
{
|
||||
@@ -975,7 +981,7 @@ class Collection extends Iterator implements Countable
|
||||
* Alias for $this->not()
|
||||
*
|
||||
* @param args any number of keys, passed as individual arguments
|
||||
* @return Collection
|
||||
* @return Kirby\Toolkit\Collection
|
||||
*/
|
||||
public function without(...$keys)
|
||||
{
|
||||
|
@@ -4,11 +4,16 @@ namespace Kirby\Toolkit;
|
||||
|
||||
use ArgumentCountError;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\A;
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Vue-like components
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Component
|
||||
{
|
||||
@@ -129,7 +134,7 @@ class Component
|
||||
}
|
||||
|
||||
/**
|
||||
* Improved var_dump output
|
||||
* Improved `var_dump` output
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
@@ -8,9 +8,9 @@ namespace Kirby\Toolkit;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Config extends Silo
|
||||
{
|
||||
|
@@ -3,13 +3,18 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use ReflectionFunction;
|
||||
|
||||
/**
|
||||
* A smart extension of Closures with
|
||||
* magic dependency injection based on the
|
||||
* defined variable names.
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Controller
|
||||
{
|
||||
|
@@ -14,8 +14,9 @@ use Throwable;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Dir
|
||||
{
|
||||
@@ -42,9 +43,11 @@ class Dir
|
||||
*
|
||||
* @param string $dir
|
||||
* @param string $target
|
||||
* @param bool $recursive
|
||||
* @param array $ignore
|
||||
* @return bool
|
||||
*/
|
||||
public static function copy(string $dir, string $target): bool
|
||||
public static function copy(string $dir, string $target, bool $recursive = true, array $ignore = []): bool
|
||||
{
|
||||
if (is_dir($dir) === false) {
|
||||
throw new Exception('The directory "' . $dir . '" does not exist');
|
||||
@@ -61,8 +64,14 @@ class Dir
|
||||
foreach (static::read($dir) as $name) {
|
||||
$root = $dir . '/' . $name;
|
||||
|
||||
if (in_array($root, $ignore) === true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_dir($root) === true) {
|
||||
static::copy($root, $target . '/' . $name);
|
||||
if ($recursive === true) {
|
||||
static::copy($root, $target . '/' . $name);
|
||||
}
|
||||
} else {
|
||||
F::copy($root, $target . '/' . $name);
|
||||
}
|
||||
|
@@ -13,8 +13,13 @@ use Zend\Escaper\Escaper;
|
||||
* value, etc.
|
||||
*
|
||||
* Wrapper for the Zend Escaper
|
||||
*
|
||||
* @link https://github.com/zendframework/zend-escaper
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Escape
|
||||
{
|
||||
|
@@ -3,7 +3,6 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Http\Header;
|
||||
use Throwable;
|
||||
use ZipArchive;
|
||||
|
||||
@@ -15,8 +14,9 @@ use ZipArchive;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class F
|
||||
{
|
||||
@@ -77,6 +77,7 @@ class F
|
||||
'jpeg',
|
||||
'jpg',
|
||||
'jpe',
|
||||
'jp2',
|
||||
'png',
|
||||
'ps',
|
||||
'psd',
|
||||
@@ -455,7 +456,7 @@ class F
|
||||
}
|
||||
|
||||
// delete the existing file
|
||||
unlink($newRoot);
|
||||
static::remove($newRoot);
|
||||
}
|
||||
|
||||
// actually move the file if it exists
|
||||
|
@@ -5,6 +5,12 @@ namespace Kirby\Toolkit;
|
||||
/**
|
||||
* Laravel-style static facades
|
||||
* for class instances
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
abstract class Facade
|
||||
{
|
||||
|
@@ -10,9 +10,9 @@ use Exception;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license http://getkirby.com/license
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class File
|
||||
{
|
||||
@@ -35,7 +35,7 @@ class File
|
||||
}
|
||||
|
||||
/**
|
||||
* Improved var_dump() output
|
||||
* Improved `var_dump` output
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -61,7 +61,7 @@ class File
|
||||
* @param boolean $force
|
||||
* @return self
|
||||
*/
|
||||
public function copy(string $target, bool $force = false): self
|
||||
public function copy(string $target, bool $force = false)
|
||||
{
|
||||
if (F::copy($this->root, $target, $force) !== true) {
|
||||
throw new Exception('The file "' . $this->root . '" could not be copied');
|
||||
@@ -194,7 +194,7 @@ class File
|
||||
* @param bool $overwrite Force overwriting any existing files
|
||||
* @return self
|
||||
*/
|
||||
public function move(string $newRoot, bool $overwrite = false): self
|
||||
public function move(string $newRoot, bool $overwrite = false)
|
||||
{
|
||||
if (F::move($this->root, $newRoot, $overwrite) !== true) {
|
||||
throw new Exception('The file: "' . $this->root . '" could not be moved to: "' . $newRoot . '"');
|
||||
@@ -253,7 +253,7 @@ class File
|
||||
* @param bool $overwrite Force overwrite existing files
|
||||
* @return self
|
||||
*/
|
||||
public function rename(string $newName, bool $overwrite = false): self
|
||||
public function rename(string $newName, bool $overwrite = false)
|
||||
{
|
||||
$newRoot = F::rename($this->root, $newName, $overwrite);
|
||||
|
||||
|
@@ -10,9 +10,9 @@ use Kirby\Http\Url;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Html
|
||||
{
|
||||
@@ -52,7 +52,7 @@ class Html
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an a tag
|
||||
* Generates an `a` tag
|
||||
*
|
||||
* @param string $href The url for the `a` tag
|
||||
* @param mixed $text The optional text. If `null`, the url will be used as text
|
||||
@@ -61,20 +61,15 @@ class Html
|
||||
*/
|
||||
public static function a(string $href = null, $text = null, array $attr = []): string
|
||||
{
|
||||
$attr = array_merge(['href' => $href], $attr);
|
||||
|
||||
if (empty($text) === true) {
|
||||
$text = $attr['href'];
|
||||
if (Str::startsWith($href, 'mailto:')) {
|
||||
return static::email($href, $text, $attr);
|
||||
}
|
||||
|
||||
if (is_string($text) === true && Str::isUrl($text) === true) {
|
||||
$text = Url::short($text);
|
||||
if (Str::startsWith($href, 'tel:')) {
|
||||
return static::tel($href, $text, $attr);
|
||||
}
|
||||
|
||||
// add rel=noopener to target blank links to improve security
|
||||
$attr['rel'] = static::rel($attr['rel'] ?? null, $attr['target'] ?? null);
|
||||
|
||||
return static::tag('a', $text, $attr);
|
||||
return static::link($href, $text, $attr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,7 +162,7 @@ class Html
|
||||
* @param array $attr Additional attributes for the tag
|
||||
* @return string the generated html
|
||||
*/
|
||||
public static function email(string $email, string $text = null, array $attr = []): string
|
||||
public static function email(string $email, $text = null, array $attr = []): string
|
||||
{
|
||||
if (empty($email) === true) {
|
||||
return '';
|
||||
@@ -327,6 +322,32 @@ class Html
|
||||
return in_array(strtolower($tag), $void);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an `a` link tag
|
||||
*
|
||||
* @param string $href The url for the `a` tag
|
||||
* @param mixed $text The optional text. If `null`, the url will be used as text
|
||||
* @param array $attr Additional attributes for the tag
|
||||
* @return string the generated html
|
||||
*/
|
||||
public static function link(string $href = null, $text = null, array $attr = []): string
|
||||
{
|
||||
$attr = array_merge(['href' => $href], $attr);
|
||||
|
||||
if (empty($text) === true) {
|
||||
$text = $attr['href'];
|
||||
}
|
||||
|
||||
if (is_string($text) === true && Str::isUrl($text) === true) {
|
||||
$text = Url::short($text);
|
||||
}
|
||||
|
||||
// add rel=noopener to target blank links to improve security
|
||||
$attr['rel'] = static::rel($attr['rel'] ?? null, $attr['target'] ?? null);
|
||||
|
||||
return static::tag('a', $text, $attr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add noopeener noreferrer to rels when target is `_blank`
|
||||
*
|
||||
@@ -398,7 +419,7 @@ class Html
|
||||
$text = $tel;
|
||||
}
|
||||
|
||||
return static::a('tel:' . $number, $text, $attr);
|
||||
return static::link('tel:' . $number, $text, $attr);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -7,6 +7,12 @@ use Exception;
|
||||
|
||||
/**
|
||||
* Localization class, roughly inspired by VueI18n
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class I18n
|
||||
{
|
||||
|
@@ -6,6 +6,12 @@ namespace Kirby\Toolkit;
|
||||
* Extended version of PHP's iterator
|
||||
* class that builds the foundation of our
|
||||
* Collection class.
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Iterator implements \Iterator
|
||||
{
|
||||
|
@@ -12,8 +12,9 @@ use SimpleXMLElement;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Mime
|
||||
{
|
||||
@@ -254,6 +255,31 @@ class Mime
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all available extensions for a given mime type
|
||||
*
|
||||
* @param string|null $mime
|
||||
* @return array
|
||||
*/
|
||||
public static function toExtensions(string $mime = null): array
|
||||
{
|
||||
$extensions = [];
|
||||
|
||||
foreach (static::$types as $key => $value) {
|
||||
if (is_array($value) === true && in_array($mime, $value) === true) {
|
||||
$extensions[] = $key;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($value === $mime) {
|
||||
$extensions[] = $key;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $extensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mime type of a file
|
||||
*
|
||||
|
@@ -7,6 +7,12 @@ use stdClass;
|
||||
/**
|
||||
* Super simple stdClass extension with
|
||||
* magic getter methods for all properties
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Obj extends stdClass
|
||||
{
|
||||
@@ -36,7 +42,7 @@ class Obj extends stdClass
|
||||
}
|
||||
|
||||
/**
|
||||
* Improved var_dump() output
|
||||
* Improved `var_dump` output
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
@@ -6,6 +6,12 @@ use Exception;
|
||||
|
||||
/**
|
||||
* Basic pagination handling
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Pagination
|
||||
{
|
||||
@@ -48,7 +54,7 @@ class Pagination
|
||||
* Creates a pagination instance for the given
|
||||
* collection with a flexible argument api
|
||||
*
|
||||
* @param Collection $collection
|
||||
* @param Kirby\CmToolkits\Collection $collection
|
||||
* @param ...mixed $arguments
|
||||
* @return self
|
||||
*/
|
||||
|
@@ -3,9 +3,17 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
* Properties
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
trait Properties
|
||||
{
|
||||
protected $propertyData = [];
|
||||
|
@@ -9,11 +9,22 @@ namespace Kirby\Toolkit;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Query
|
||||
{
|
||||
const PARTS = '!([a-zA-Z_]*(\(.*?\))?)\.|' . self::SKIP . '!';
|
||||
const METHOD = '!\((.*)\)!';
|
||||
const PARAMETERS = '!,|' . self::SKIP . '!';
|
||||
|
||||
const NO_PNTH = '\([^\(]+\)(*SKIP)(*FAIL)';
|
||||
const NO_SQBR = '\[[^]]+\](*SKIP)(*FAIL)';
|
||||
const NO_DLQU = '\"[^"]+\"(*SKIP)(*FAIL)';
|
||||
const NO_SLQU = '\'[^\']+\'(*SKIP)(*FAIL)';
|
||||
const SKIP = self::NO_PNTH . '|' . self::NO_SQBR . '|' .
|
||||
self::NO_DLQU . '|' . self::NO_SLQU;
|
||||
|
||||
/**
|
||||
* The query string
|
||||
@@ -33,7 +44,7 @@ class Query
|
||||
* Creates a new Query object
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $data
|
||||
* @param array|object $data
|
||||
*/
|
||||
public function __construct(string $query = null, $data = [])
|
||||
{
|
||||
@@ -53,13 +64,25 @@ class Query
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
$parts = $this->parts($this->query);
|
||||
return $this->resolve($this->query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the query if anything
|
||||
* can be found. Otherwise returns null.
|
||||
*
|
||||
* @param string $query
|
||||
* @return mixed
|
||||
*/
|
||||
protected function resolve(string $query)
|
||||
{
|
||||
$parts = $this->parts($query);
|
||||
$data = $this->data;
|
||||
$value = null;
|
||||
|
||||
while (count($parts)) {
|
||||
$part = array_shift($parts);
|
||||
$info = $this->info($part);
|
||||
$info = $this->part($part);
|
||||
$method = $info['method'];
|
||||
$value = null;
|
||||
|
||||
@@ -86,64 +109,90 @@ class Query
|
||||
/**
|
||||
* Breaks the query string down into its components
|
||||
*
|
||||
* @param string $token
|
||||
* @param string $query
|
||||
* @return array
|
||||
*/
|
||||
protected function parts(string $token): array
|
||||
protected function parts(string $query): array
|
||||
{
|
||||
$token = trim($token);
|
||||
$token = preg_replace_callback('!\((.*?)\)!', function ($match) {
|
||||
return '(' . str_replace('.', '@@@', $match[1]) . ')';
|
||||
}, $token);
|
||||
$query = trim($query);
|
||||
|
||||
$parts = explode('.', $token);
|
||||
// match all parts but the last
|
||||
preg_match_all(self::PARTS, $query, $match);
|
||||
|
||||
return $parts;
|
||||
// remove all matched parts from the query to retrieve last part
|
||||
foreach ($match[0] as $part) {
|
||||
$query = Str::after($query, $part);
|
||||
}
|
||||
|
||||
array_push($match[1], $query);
|
||||
return $match[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyzes each part of the query string and
|
||||
* extracts methods and method arguments.
|
||||
*
|
||||
* @param string $token
|
||||
* @param string $part
|
||||
* @return array
|
||||
*/
|
||||
protected function info(string $token): array
|
||||
protected function part(string $part): array
|
||||
{
|
||||
$args = [];
|
||||
$method = preg_replace_callback('!\((.*?)\)!', function ($match) use (&$args) {
|
||||
$args = array_map(function ($arg) {
|
||||
$arg = trim($arg);
|
||||
$arg = str_replace('@@@', '.', $arg);
|
||||
|
||||
if (substr($arg, 0, 1) === '"') {
|
||||
return trim($arg, '"');
|
||||
}
|
||||
|
||||
if (substr($arg, 0, 1) === '\'') {
|
||||
return trim($arg, '\'');
|
||||
}
|
||||
|
||||
switch ($arg) {
|
||||
case 'null':
|
||||
return null;
|
||||
case 'false':
|
||||
return false;
|
||||
case 'true':
|
||||
return true;
|
||||
}
|
||||
|
||||
if (is_numeric($arg) === true) {
|
||||
return (float)$arg;
|
||||
}
|
||||
|
||||
return $arg;
|
||||
}, str_getcsv($match[1], ','));
|
||||
}, $token);
|
||||
$method = preg_replace_callback(self::METHOD, function ($match) use (&$args) {
|
||||
$args = preg_split(self::PARAMETERS, $match[1]);
|
||||
$args = array_map('self::parameter', $args);
|
||||
}, $part);
|
||||
|
||||
return [
|
||||
'method' => $method,
|
||||
'args' => $args
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a parameter of query to
|
||||
* proper type.
|
||||
*
|
||||
* @param mixed $arg
|
||||
* @return mixed
|
||||
*/
|
||||
protected function parameter($arg)
|
||||
{
|
||||
$arg = trim($arg);
|
||||
|
||||
// string with double quotes
|
||||
if (substr($arg, 0, 1) === '"') {
|
||||
return trim($arg, '"');
|
||||
}
|
||||
|
||||
// string with single quotes
|
||||
if (substr($arg, 0, 1) === '\'') {
|
||||
return trim($arg, '\'');
|
||||
}
|
||||
|
||||
// boolean or null
|
||||
switch ($arg) {
|
||||
case 'null':
|
||||
return null;
|
||||
case 'false':
|
||||
return false;
|
||||
case 'true':
|
||||
return true;
|
||||
}
|
||||
|
||||
// numeric
|
||||
if (is_numeric($arg) === true) {
|
||||
return (float)$arg;
|
||||
}
|
||||
|
||||
// array: split and recursive sanitizing
|
||||
if (substr($arg, 0, 1) === '[' && substr($arg, -1) === ']') {
|
||||
$arg = substr($arg, 1, -1);
|
||||
$arg = preg_split(self::PARAMETERS, $arg);
|
||||
return array_map('self::parameter', $arg);
|
||||
}
|
||||
|
||||
// resolve parameter for objects and methods itself
|
||||
return $this->resolve($arg);
|
||||
}
|
||||
}
|
||||
|
@@ -9,9 +9,9 @@ namespace Kirby\Toolkit;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Silo
|
||||
{
|
||||
|
@@ -11,19 +11,36 @@ use Exception;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Str
|
||||
{
|
||||
|
||||
/**
|
||||
* Language translation table
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $language = [];
|
||||
|
||||
/**
|
||||
* Ascii translation table
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $ascii = [
|
||||
public static $ascii = [
|
||||
'/°|₀/' => '0',
|
||||
'/¹|₁/' => '1',
|
||||
'/²|₂/' => '2',
|
||||
'/³|₃/' => '3',
|
||||
'/⁴|₄/' => '4',
|
||||
'/⁵|₅/' => '5',
|
||||
'/⁶|₆/' => '6',
|
||||
'/⁷|₇/' => '7',
|
||||
'/⁸|₈/' => '8',
|
||||
'/⁹|₉/' => '9',
|
||||
'/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ|Ä|A/' => 'A',
|
||||
'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|æ|ǽ|ä|a/' => 'a',
|
||||
'/Б/' => 'B',
|
||||
@@ -75,7 +92,7 @@ class Str
|
||||
'/Ź|Ż|Ž|З/' => 'Z',
|
||||
'/ź|ż|ž|з/' => 'z',
|
||||
'/Æ|Ǽ/' => 'AE',
|
||||
'/ß/'=> 'ss',
|
||||
'/ß/' => 'ss',
|
||||
'/IJ/' => 'IJ',
|
||||
'/ij/' => 'ij',
|
||||
'/Œ/' => 'OE',
|
||||
@@ -177,8 +194,18 @@ class Str
|
||||
*/
|
||||
public static function ascii(string $string): string
|
||||
{
|
||||
$foreign = static::$ascii;
|
||||
$string = preg_replace(array_keys($foreign), array_values($foreign), $string);
|
||||
$string = str_replace(
|
||||
array_keys(static::$language),
|
||||
array_values(static::$language),
|
||||
$string
|
||||
);
|
||||
|
||||
$string = preg_replace(
|
||||
array_keys(static::$ascii),
|
||||
array_values(static::$ascii),
|
||||
$string
|
||||
);
|
||||
|
||||
return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $string);
|
||||
}
|
||||
|
||||
@@ -477,7 +504,7 @@ class Str
|
||||
* @param bool $caseInsensitive
|
||||
* @return int|bool
|
||||
*/
|
||||
public static function position(string $string, string $needle, bool $caseInsensitive = false)
|
||||
public static function position(string $string = null, string $needle, bool $caseInsensitive = false)
|
||||
{
|
||||
if ($caseInsensitive === true) {
|
||||
$string = static::lower($string);
|
||||
@@ -821,7 +848,7 @@ class Str
|
||||
* @param bool $caseInsensitive
|
||||
* @return bool
|
||||
*/
|
||||
public static function startsWith(string $string, string $needle, bool $caseInsensitive = false): bool
|
||||
public static function startsWith(string $string = null, string $needle, bool $caseInsensitive = false): bool
|
||||
{
|
||||
if ($needle === '') {
|
||||
return true;
|
||||
|
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
@@ -10,9 +9,9 @@ use Throwable;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Tpl
|
||||
{
|
||||
|
@@ -3,19 +3,19 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Image\Image;
|
||||
use Kirby\Toolkit\Str;
|
||||
use ReflectionFunction;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* A set of validator methods
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
* A set of validator methods
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class V
|
||||
{
|
||||
|
||||
@@ -66,17 +66,20 @@ class V
|
||||
$reflection = new ReflectionFunction($validator);
|
||||
$arguments = [];
|
||||
|
||||
|
||||
foreach ($reflection->getParameters() as $index => $parameter) {
|
||||
$value = $params[$index] ?? null;
|
||||
|
||||
if (is_array($value) === true) {
|
||||
foreach ($value as $index => $item) {
|
||||
if (is_array($item) === true) {
|
||||
$value[$index] = implode('|', $item);
|
||||
try {
|
||||
foreach ($value as $index => $item) {
|
||||
if (is_array($item) === true) {
|
||||
$value[$index] = implode('|', $item);
|
||||
}
|
||||
}
|
||||
$value = implode(', ', $value);
|
||||
} catch (Throwable $e) {
|
||||
$value = '-';
|
||||
}
|
||||
$value = implode(', ', $value);
|
||||
}
|
||||
|
||||
$arguments[$parameter->getName()] = $value;
|
||||
|
@@ -10,9 +10,9 @@ use Throwable;
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class View
|
||||
{
|
||||
|
@@ -3,14 +3,14 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
/**
|
||||
* XML parser and creator Class
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
* XML parser and creator Class
|
||||
*
|
||||
* @package Kirby Toolkit
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Xml
|
||||
{
|
||||
|
||||
|
Reference in New Issue
Block a user