Upgrade to 3.9.6

This commit is contained in:
Bastian Allgeier
2023-07-27 12:08:43 +02:00
parent f76fbaa53e
commit 7928c28702
58 changed files with 930 additions and 148 deletions

View File

@@ -70,6 +70,10 @@ class Argument
// numeric
if (is_numeric($argument) === true) {
if (strpos($argument, '.') === false) {
return new static((int)$argument);
}
return new static((float)$argument);
}

View File

@@ -117,7 +117,7 @@ Query::$entries['file'] = function (string $id): File|null {
};
Query::$entries['page'] = function (string $id): Page|null {
return App::instance()->site()->find($id);
return App::instance()->page($id);
};
Query::$entries['site'] = function (): Site {

View File

@@ -77,19 +77,17 @@ class Segment
/**
* Automatically resolves the segment depending on the
* segment position and the type of the base
*
* @param mixed $base Current value of the query chain
*/
public function resolve(mixed $base = null, array|object $data = []): mixed
{
// resolve arguments to array
$args = $this->arguments?->resolve($data) ?? [];
// 1st segment, start from $data array
// 1st segment, use $data as base
if ($this->position === 0) {
if (is_array($data) == true) {
return $this->resolveArray($data, $args);
}
return $this->resolveObject($data, $args);
$base = $data;
}
if (is_array($base) === true) {
@@ -109,26 +107,55 @@ class Segment
*/
protected function resolveArray(array $array, array $args): mixed
{
if (array_key_exists($this->method, $array) === false) {
static::error($array, $this->method, 'property');
// the directly provided array takes precedence
// to look up a matching entry
if (array_key_exists($this->method, $array) === true) {
$value = $array[$this->method];
// if this is a Closure we can directly use it, as
// Closures from the $array should always have priority
// over the Query::$entries Closures
if ($value instanceof Closure) {
return $value(...$args);
}
// if we have no arguments to pass, we also can directly
// use the value from the $array as it must not be different
// to the one from Query::$entries with the same name
if ($args === []) {
return $value;
}
}
$value = $array[$this->method];
if ($value instanceof Closure) {
return $value(...$args);
// fallback time: only if we are handling the first segment,
// we can also try to resolve the segment with an entry from the
// default Query::$entries
if ($this->position === 0) {
if (array_key_exists($this->method, Query::$entries) === true) {
return Query::$entries[$this->method](...$args);
}
}
if ($args !== []) {
// if we have not been able to return anything so far,
// we just need to differntiate between two different error messages
// this one is in case the original array contained the key,
// but was not a Closure while the segment had arguments
if (
array_key_exists($this->method, $array) &&
$args !== []
) {
throw new InvalidArgumentException('Cannot access array element "' . $this->method . '" with arguments');
}
return $value;
// last, the standard error for trying to access something
// that does not exist
static::error($array, $this->method, 'property');
}
/**
* Resolves segment by calling the method/accessing the property
* on the base object
* Resolves segment by calling the method/
* accessing the property on the base object
*/
protected function resolveObject(object $object, array $args): mixed
{
@@ -140,7 +167,8 @@ class Segment
}
if (
$args === [] && (
$args === [] &&
(
property_exists($object, $this->method) === true ||
method_exists($object, '__get') === true
)