Initial Commit

This commit is contained in:
David Stone
2024-11-30 18:24:12 -07:00
commit e8f7955c1c
5432 changed files with 1397750 additions and 0 deletions

View File

@ -0,0 +1,67 @@
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare (strict_types=1);
namespace WP_Ultimo\Dependencies\League\Uri\UriTemplate;
use WP_Ultimo\Dependencies\League\Uri\Exceptions\SyntaxError;
use Stringable;
use function array_filter;
use function array_map;
use function array_unique;
use function explode;
use function implode;
/**
* @internal The class exposes the internal representation of an Expression and its usage
* @link https://www.rfc-editor.org/rfc/rfc6570#section-2.2
*/
final class Expression
{
/** @var array<VarSpecifier> */
private readonly array $varSpecifiers;
/** @var array<string> */
public readonly array $variableNames;
public readonly string $value;
private function __construct(public readonly Operator $operator, VarSpecifier ...$varSpecifiers)
{
$this->varSpecifiers = $varSpecifiers;
$this->variableNames = array_unique(array_map(static fn(VarSpecifier $varSpecifier): string => $varSpecifier->name, $varSpecifiers));
$this->value = '{' . $operator->value . implode(',', array_map(static fn(VarSpecifier $varSpecifier): string => $varSpecifier->toString(), $varSpecifiers)) . '}';
}
/**
* @throws SyntaxError if the expression is invalid
*/
public static function new(Stringable|string $expression) : self
{
$parts = Operator::parseExpression($expression);
return new Expression($parts['operator'], ...array_map(static fn(string $varSpec): VarSpecifier => VarSpecifier::new($varSpec), explode(',', $parts['variables'])));
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @throws SyntaxError if the expression is invalid
* @see Expression::new()
*
* @deprecated Since version 7.0.0
* @codeCoverageIgnore
*/
public static function createFromString(Stringable|string $expression) : self
{
return self::new($expression);
}
public function expand(VariableBag $variables) : string
{
$expanded = implode($this->operator->separator(), array_filter(array_map(fn(VarSpecifier $varSpecifier): string => $this->operator->expand($varSpecifier, $variables), $this->varSpecifiers), static fn($value): bool => '' !== $value));
return match ('') {
$expanded => '',
default => $this->operator->first() . $expanded,
};
}
}

View File

@ -0,0 +1,193 @@
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare (strict_types=1);
namespace WP_Ultimo\Dependencies\League\Uri\UriTemplate;
use WP_Ultimo\Dependencies\League\Uri\Encoder;
use WP_Ultimo\Dependencies\League\Uri\Exceptions\SyntaxError;
use Stringable;
use function implode;
use function is_array;
use function preg_match;
use function rawurlencode;
use function str_contains;
use function substr;
/**
* Processing behavior according to the expression type operator.
*
* @internal The class exposes the internal representation of an Operator and its usage
*
* @link https://www.rfc-editor.org/rfc/rfc6570#section-2.2
* @link https://tools.ietf.org/html/rfc6570#appendix-A
*/
enum Operator : string
{
/**
* Expression regular expression pattern.
*
* @link https://tools.ietf.org/html/rfc6570#section-2.2
*/
private const REGEXP_EXPRESSION = '/^\\{(?:(?<operator>[\\.\\/;\\?&\\=,\\!@\\|\\+#])?(?<variables>[^\\}]*))\\}$/';
/**
* Reserved Operator characters.
*
* @link https://tools.ietf.org/html/rfc6570#section-2.2
*/
private const RESERVED_OPERATOR = '=,!@|';
case None = '';
case ReservedChars = '+';
case Label = '.';
case Path = '/';
case PathParam = ';';
case Query = '?';
case QueryPair = '&';
case Fragment = '#';
public function first() : string
{
return match ($this) {
self::None, self::ReservedChars => '',
default => $this->value,
};
}
public function separator() : string
{
return match ($this) {
self::None, self::ReservedChars, self::Fragment => ',',
self::Query, self::QueryPair => '&',
default => $this->value,
};
}
public function isNamed() : bool
{
return match ($this) {
self::Query, self::PathParam, self::QueryPair => \true,
default => \false,
};
}
/**
* Removes percent encoding on reserved characters (used with + and # modifiers).
*/
public function decode(string $var) : string
{
return match ($this) {
Operator::ReservedChars, Operator::Fragment => (string) Encoder::encodeQueryOrFragment($var),
default => rawurlencode($var),
};
}
/**
* @throws SyntaxError if the expression is invalid
* @throws SyntaxError if the operator used in the expression is invalid
* @throws SyntaxError if the contained variable specifiers are invalid
*
* @return array{operator:Operator, variables:string}
*/
public static function parseExpression(Stringable|string $expression) : array
{
$expression = (string) $expression;
if (1 !== preg_match(self::REGEXP_EXPRESSION, $expression, $parts)) {
throw new SyntaxError('The expression "' . $expression . '" is invalid.');
}
/** @var array{operator:string, variables:string} $parts */
$parts = $parts + ['operator' => ''];
if ('' !== $parts['operator'] && str_contains(self::RESERVED_OPERATOR, $parts['operator'])) {
throw new SyntaxError('The operator used in the expression "' . $expression . '" is reserved.');
}
return ['operator' => self::from($parts['operator']), 'variables' => $parts['variables']];
}
/**
* Replaces an expression with the given variables.
*
* @throws TemplateCanNotBeExpanded if the variables is an array and a ":" modifier needs to be applied
* @throws TemplateCanNotBeExpanded if the variables contains nested array values
*/
public function expand(VarSpecifier $varSpecifier, VariableBag $variables) : string
{
$value = $variables->fetch($varSpecifier->name);
if (null === $value) {
return '';
}
[$expanded, $actualQuery] = $this->inject($value, $varSpecifier);
if (!$actualQuery) {
return $expanded;
}
if ('&' !== $this->separator() && '' === $expanded) {
return $varSpecifier->name;
}
return $varSpecifier->name . '=' . $expanded;
}
/**
* @param string|array<string> $value
*
* @return array{0:string, 1:bool}
*/
private function inject(array|string $value, VarSpecifier $varSpec) : array
{
if (is_array($value)) {
return $this->replaceList($value, $varSpec);
}
if (':' === $varSpec->modifier) {
$value = substr($value, 0, $varSpec->position);
}
return [$this->decode($value), $this->isNamed()];
}
/**
* Expands an expression using a list of values.
*
* @param array<string> $value
*
* @throws TemplateCanNotBeExpanded if the variables is an array and a ":" modifier needs to be applied
*
* @return array{0:string, 1:bool}
*/
private function replaceList(array $value, VarSpecifier $varSpec) : array
{
if (':' === $varSpec->modifier) {
throw TemplateCanNotBeExpanded::dueToUnableToProcessValueListWithPrefix($varSpec->name);
}
if ([] === $value) {
return ['', \false];
}
$pairs = [];
$isList = \array_is_list($value);
$useQuery = $this->isNamed();
foreach ($value as $key => $var) {
if (!$isList) {
$key = rawurlencode((string) $key);
}
$var = $this->decode($var);
if ('*' === $varSpec->modifier) {
if (!$isList) {
$var = $key . '=' . $var;
} elseif ($key > 0 && $useQuery) {
$var = $varSpec->name . '=' . $var;
}
}
$pairs[$key] = $var;
}
if ('*' === $varSpec->modifier) {
if (!$isList) {
// Don't prepend the value name when using the `explode` modifier with an associative array.
$useQuery = \false;
}
return [implode($this->separator(), $pairs), $useQuery];
}
if (!$isList) {
// When an associative array is encountered and the `explode` modifier is not set, then
// the result must be a comma separated list of keys followed by their respective values.
$retVal = [];
foreach ($pairs as $offset => $data) {
$retVal[$offset] = $offset . ',' . $data;
}
$pairs = $retVal;
}
return [implode(',', $pairs), $useQuery];
}
}

View File

@ -0,0 +1,111 @@
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare (strict_types=1);
namespace WP_Ultimo\Dependencies\League\Uri\UriTemplate;
use WP_Ultimo\Dependencies\League\Uri\Exceptions\SyntaxError;
use Stringable;
use function array_filter;
use function array_map;
use function array_reduce;
use function array_unique;
use function preg_match_all;
use function preg_replace;
use function str_contains;
use function str_replace;
use const PREG_SET_ORDER;
/**
* @internal The class exposes the internal representation of a Template and its usage
*/
final class Template implements Stringable
{
/**
* Expression regular expression pattern.
*/
private const REGEXP_EXPRESSION_DETECTOR = '/(?<expression>\\{[^}]*})/x';
/** @var array<Expression> */
private readonly array $expressions;
/** @var array<string> */
public readonly array $variableNames;
private function __construct(public readonly string $value, Expression ...$expressions)
{
$this->expressions = $expressions;
$this->variableNames = array_unique(\array_merge(...array_map(static fn(Expression $expression): array => $expression->variableNames, $expressions)));
}
/**
* @throws SyntaxError if the template contains invalid expressions
* @throws SyntaxError if the template contains invalid variable specification
*/
public static function new(Stringable|string $template) : self
{
$template = (string) $template;
/** @var string $remainder */
$remainder = preg_replace(self::REGEXP_EXPRESSION_DETECTOR, '', $template);
if (str_contains($remainder, '{') || str_contains($remainder, '}')) {
throw new SyntaxError('The template "' . $template . '" contains invalid expressions.');
}
preg_match_all(self::REGEXP_EXPRESSION_DETECTOR, $template, $founds, PREG_SET_ORDER);
return new self($template, ...\array_values(array_reduce($founds, function (array $carry, array $found) : array {
if (!isset($carry[$found['expression']])) {
$carry[$found['expression']] = Expression::new($found['expression']);
}
return $carry;
}, [])));
}
/**
* @throws TemplateCanNotBeExpanded if the variables are invalid
*/
public function expand(iterable $variables = []) : string
{
if (!$variables instanceof VariableBag) {
$variables = new VariableBag($variables);
}
return $this->expandAll($variables);
}
/**
* @throws TemplateCanNotBeExpanded if the variables are invalid or missing
*/
public function expandOrFail(iterable $variables = []) : string
{
if (!$variables instanceof VariableBag) {
$variables = new VariableBag($variables);
}
$missing = array_filter($this->variableNames, fn(string $name): bool => !isset($variables[$name]));
if ([] !== $missing) {
throw TemplateCanNotBeExpanded::dueToMissingVariables(...$missing);
}
return $this->expandAll($variables);
}
private function expandAll(VariableBag $variables) : string
{
return array_reduce($this->expressions, fn(string $uri, Expression $expr): string => str_replace($expr->value, $expr->expand($variables), $uri), $this->value);
}
public function __toString() : string
{
return $this->value;
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @throws SyntaxError if the template contains invalid expressions
* @throws SyntaxError if the template contains invalid variable specification
* @deprecated Since version 7.0.0
* @codeCoverageIgnore
* @see Template::new()
*
* Create a new instance from a string.
*
*/
public static function createFromString(Stringable|string $template) : self
{
return self::new($template);
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare (strict_types=1);
namespace WP_Ultimo\Dependencies\League\Uri\UriTemplate;
use InvalidArgumentException;
use WP_Ultimo\Dependencies\League\Uri\Contracts\UriException;
class TemplateCanNotBeExpanded extends InvalidArgumentException implements UriException
{
public readonly array $variablesNames;
public function __construct(string $message = '', string ...$variableNames)
{
parent::__construct($message, 0, null);
$this->variablesNames = $variableNames;
}
public static function dueToUnableToProcessValueListWithPrefix(string $variableName) : self
{
return new self('The ":" modifier cannot be applied on "' . $variableName . '" since it is a list of values.', $variableName);
}
public static function dueToNestedListOfValue(string $variableName) : self
{
return new self('The "' . $variableName . '" cannot be a nested list.', $variableName);
}
public static function dueToMissingVariables(string ...$variableNames) : self
{
return new self('The following required variables are missing: `' . \implode('`, `', $variableNames) . '`.', ...$variableNames);
}
}

View File

@ -0,0 +1,57 @@
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare (strict_types=1);
namespace WP_Ultimo\Dependencies\League\Uri\UriTemplate;
use WP_Ultimo\Dependencies\League\Uri\Exceptions\SyntaxError;
use function preg_match;
/**
* @internal The class exposes the internal representation of a Var Specifier
* @link https://www.rfc-editor.org/rfc/rfc6570#section-2.3
*/
final class VarSpecifier
{
/**
* Variables specification regular expression pattern.
*
* @link https://tools.ietf.org/html/rfc6570#section-2.3
*/
private const REGEXP_VARSPEC = '/^(?<name>(?:[A-z0-9_\\.]|%[0-9a-fA-F]{2})+)(?<modifier>\\:(?<position>\\d+)|\\*)?$/';
private const MODIFIER_POSITION_MAX_POSITION = 10000;
private function __construct(public readonly string $name, public readonly string $modifier, public readonly int $position)
{
}
public static function new(string $specification) : self
{
if (1 !== preg_match(self::REGEXP_VARSPEC, $specification, $parsed)) {
throw new SyntaxError('The variable specification "' . $specification . '" is invalid.');
}
$properties = ['name' => $parsed['name'], 'modifier' => $parsed['modifier'] ?? '', 'position' => $parsed['position'] ?? ''];
if ('' !== $properties['position']) {
$properties['position'] = (int) $properties['position'];
$properties['modifier'] = ':';
}
if ('' === $properties['position']) {
$properties['position'] = 0;
}
if (self::MODIFIER_POSITION_MAX_POSITION <= $properties['position']) {
throw new SyntaxError('The variable specification "' . $specification . '" is invalid the position modifier must be lower than 10000.');
}
return new self($properties['name'], $properties['modifier'], $properties['position']);
}
public function toString() : string
{
return $this->name . $this->modifier . match (\true) {
0 < $this->position => $this->position,
default => '',
};
}
}

View File

@ -0,0 +1,130 @@
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare (strict_types=1);
namespace WP_Ultimo\Dependencies\League\Uri\UriTemplate;
use ArrayAccess;
use Closure;
use Countable;
use IteratorAggregate;
use Stringable;
use Traversable;
use function array_filter;
use function is_bool;
use function is_scalar;
use const ARRAY_FILTER_USE_BOTH;
/**
* @internal The class exposes the internal representation of variable bags
*
* @phpstan-type InputValue string|bool|int|float|array<string|bool|int|float>
*
* @implements ArrayAccess<string, InputValue>
* @implements IteratorAggregate<string, InputValue>
*/
final class VariableBag implements ArrayAccess, Countable, IteratorAggregate
{
/**
* @var array<string,string|array<string>>
*/
private array $variables = [];
/**
* @param iterable<array-key, InputValue> $variables
*/
public function __construct(iterable $variables = [])
{
foreach ($variables as $name => $value) {
$this->assign((string) $name, $value);
}
}
public function count() : int
{
return \count($this->variables);
}
public function getIterator() : Traversable
{
yield from $this->variables;
}
public function offsetExists(mixed $offset) : bool
{
return \array_key_exists($offset, $this->variables);
}
public function offsetUnset(mixed $offset) : void
{
unset($this->variables[$offset]);
}
public function offsetSet(mixed $offset, mixed $value) : void
{
$this->assign($offset, $value);
/* @phpstan-ignore-line */
}
public function offsetGet(mixed $offset) : mixed
{
return $this->fetch($offset);
}
/**
* Tells whether the bag is empty or not.
*/
public function isEmpty() : bool
{
return [] === $this->variables;
}
/**
* Tells whether the bag is empty or not.
*/
public function isNotEmpty() : bool
{
return [] !== $this->variables;
}
/**
* Fetches the variable value if none found returns null.
*
* @return null|string|array<string>
*/
public function fetch(string $name) : null|string|array
{
return $this->variables[$name] ?? null;
}
/**
* @param Stringable|InputValue $value
*/
public function assign(string $name, Stringable|string|bool|int|float|array|null $value) : void
{
$this->variables[$name] = $this->normalizeValue($value, $name, \true);
}
/**
* @param Stringable|InputValue $value
*
* @throws TemplateCanNotBeExpanded if the value contains nested list
*/
private function normalizeValue(Stringable|string|float|int|bool|array|null $value, string $name, bool $isNestedListAllowed) : array|string
{
return match (\true) {
is_bool($value) => \true === $value ? '1' : '0',
null === $value || is_scalar($value) || $value instanceof Stringable => (string) $value,
!$isNestedListAllowed => throw TemplateCanNotBeExpanded::dueToNestedListOfValue($name),
default => \array_map(fn($var): array|string => self::normalizeValue($var, $name, \false), $value),
};
}
/**
* Replaces elements from passed variables into the current instance.
*/
public function replace(VariableBag $variables) : self
{
return new self($this->variables + $variables->variables);
}
/**
* Filters elements using the closure.
*/
public function filter(Closure $fn) : self
{
return new self(array_filter($this->variables, $fn, ARRAY_FILTER_USE_BOTH));
}
}