Update website

This commit is contained in:
Guilhem Lavaux 2024-11-19 08:02:04 +01:00
parent 4413528994
commit 1d90fbf296
6865 changed files with 1091082 additions and 0 deletions

View file

@ -0,0 +1,353 @@
<?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 League\Uri\UriTemplate;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Exceptions\TemplateCanNotBeExpanded;
use function array_filter;
use function array_keys;
use function array_map;
use function array_unique;
use function explode;
use function implode;
use function preg_match;
use function rawurlencode;
use function str_replace;
use function strpos;
use function substr;
final class Expression
{
/**
* Expression regular expression pattern.
*
* @link https://tools.ietf.org/html/rfc6570#section-2.2
*/
private const REGEXP_EXPRESSION = '/^\{
(?:
(?<operator>[\.\/;\?&\=,\!@\|\+#])?
(?<variables>[^\}]*)
)
\}$/x';
/**
* Reserved Operator characters.
*
* @link https://tools.ietf.org/html/rfc6570#section-2.2
*/
private const RESERVED_OPERATOR = '=,!@|';
/**
* Processing behavior according to the expression type operator.
*
* @link https://tools.ietf.org/html/rfc6570#appendix-A
*/
private const OPERATOR_HASH_LOOKUP = [
'' => ['prefix' => '', 'joiner' => ',', 'query' => false],
'+' => ['prefix' => '', 'joiner' => ',', 'query' => false],
'#' => ['prefix' => '#', 'joiner' => ',', 'query' => false],
'.' => ['prefix' => '.', 'joiner' => '.', 'query' => false],
'/' => ['prefix' => '/', 'joiner' => '/', 'query' => false],
';' => ['prefix' => ';', 'joiner' => ';', 'query' => true],
'?' => ['prefix' => '?', 'joiner' => '&', 'query' => true],
'&' => ['prefix' => '&', 'joiner' => '&', 'query' => true],
];
/**
* @var string
*/
private $operator;
/**
* @var string
*/
private $joiner;
/**
* @var array<VarSpecifier>
*/
private $varSpecifiers;
/**
* @var array<string>
*/
private $variableNames;
/**
* @var string
*/
private $expressionString;
private function __construct(string $operator, VarSpecifier ...$varSpecifiers)
{
$this->operator = $operator;
$this->varSpecifiers = $varSpecifiers;
$this->joiner = self::OPERATOR_HASH_LOOKUP[$operator]['joiner'];
$this->variableNames = $this->setVariableNames();
$this->expressionString = $this->setExpressionString();
}
/**
* @return array<string>
*/
private function setVariableNames(): array
{
$mapper = static function (VarSpecifier $varSpecifier): string {
return $varSpecifier->name();
};
return array_unique(array_map($mapper, $this->varSpecifiers));
}
private function setExpressionString(): string
{
$mapper = static function (VarSpecifier $variable): string {
return $variable->toString();
};
$varSpecifierString = implode(',', array_map($mapper, $this->varSpecifiers));
return '{'.$this->operator.$varSpecifierString.'}';
}
/**
* {@inheritDoc}
*/
public static function __set_state(array $properties): self
{
return new self($properties['operator'], ...$properties['varSpecifiers']);
}
/**
* @throws SyntaxError if the expression is invalid
* @throws SyntaxError if the operator used in the expression is invalid
* @throws SyntaxError if the variable specifiers is invalid
*/
public static function createFromString(string $expression): self
{
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'] && false !== strpos(self::RESERVED_OPERATOR, $parts['operator'])) {
throw new SyntaxError('The operator used in the expression "'.$expression.'" is reserved.');
}
$mapper = static function (string $varSpec): VarSpecifier {
return VarSpecifier::createFromString($varSpec);
};
return new Expression($parts['operator'], ...array_map($mapper, explode(',', $parts['variables'])));
}
/**
* Returns the expression string representation.
*
*/
public function toString(): string
{
return $this->expressionString;
}
/**
* @return array<string>
*/
public function variableNames(): array
{
return $this->variableNames;
}
public function expand(VariableBag $variables): string
{
$parts = [];
foreach ($this->varSpecifiers as $varSpecifier) {
$parts[] = $this->replace($varSpecifier, $variables);
}
$nullFilter = static function ($value): bool {
return '' !== $value;
};
$expanded = implode($this->joiner, array_filter($parts, $nullFilter));
if ('' === $expanded) {
return $expanded;
}
$prefix = self::OPERATOR_HASH_LOOKUP[$this->operator]['prefix'];
if ('' === $prefix) {
return $expanded;
}
return $prefix.$expanded;
}
/**
* 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
*/
private function replace(VarSpecifier $varSpec, VariableBag $variables): string
{
$value = $variables->fetch($varSpec->name());
if (null === $value) {
return '';
}
$useQuery = self::OPERATOR_HASH_LOOKUP[$this->operator]['query'];
[$expanded, $actualQuery] = $this->inject($value, $varSpec, $useQuery);
if (!$actualQuery) {
return $expanded;
}
if ('&' !== $this->joiner && '' === $expanded) {
return $varSpec->name();
}
return $varSpec->name().'='.$expanded;
}
/**
* @param string|array<string> $value
*
* @return array{0:string, 1:bool}
*/
private function inject($value, VarSpecifier $varSpec, bool $useQuery): array
{
if (is_string($value)) {
return $this->replaceString($value, $varSpec, $useQuery);
}
return $this->replaceList($value, $varSpec, $useQuery);
}
/**
* Expands an expression using a string value.
*
* @return array{0:string, 1:bool}
*/
private function replaceString(string $value, VarSpecifier $varSpec, bool $useQuery): array
{
if (':' === $varSpec->modifier()) {
$value = substr($value, 0, $varSpec->position());
}
$expanded = rawurlencode($value);
if ('+' === $this->operator || '#' === $this->operator) {
return [$this->decodeReserved($expanded), $useQuery];
}
return [$expanded, $useQuery];
}
/**
* 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, bool $useQuery): array
{
if ([] === $value) {
return ['', false];
}
if (':' === $varSpec->modifier()) {
throw TemplateCanNotBeExpanded::dueToUnableToProcessValueListWithPrefix($varSpec->name());
}
$pairs = [];
$isAssoc = $this->isAssoc($value);
foreach ($value as $key => $var) {
if ($isAssoc) {
$key = rawurlencode((string) $key);
}
$var = rawurlencode($var);
if ('+' === $this->operator || '#' === $this->operator) {
$var = $this->decodeReserved($var);
}
if ('*' === $varSpec->modifier()) {
if ($isAssoc) {
$var = $key.'='.$var;
} elseif ($key > 0 && $useQuery) {
$var = $varSpec->name().'='.$var;
}
}
$pairs[$key] = $var;
}
if ('*' === $varSpec->modifier()) {
if ($isAssoc) {
// Don't prepend the value name when using the explode
// modifier with an associative array.
$useQuery = false;
}
return [implode($this->joiner, $pairs), $useQuery];
}
if ($isAssoc) {
// 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.
foreach ($pairs as $offset => &$data) {
$data = $offset.','.$data;
}
unset($data);
}
return [implode(',', $pairs), $useQuery];
}
/**
* Determines if an array is associative.
*
* This makes the assumption that input arrays are sequences or hashes.
* This assumption is a trade-off for accuracy in favor of speed, but it
* should work in almost every case where input is supplied for a URI
* template.
*/
private function isAssoc(array $array): bool
{
return [] !== $array && 0 !== array_keys($array)[0];
}
/**
* Removes percent encoding on reserved characters (used with + and # modifiers).
*/
private function decodeReserved(string $str): string
{
static $delimiters = [
':', '/', '?', '#', '[', ']', '@', '!', '$',
'&', '\'', '(', ')', '*', '+', ',', ';', '=',
];
static $delimitersEncoded = [
'%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40', '%21', '%24',
'%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D',
];
return str_replace($delimitersEncoded, $delimiters, $str);
}
}

View file

@ -0,0 +1,134 @@
<?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 League\Uri\UriTemplate;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Exceptions\TemplateCanNotBeExpanded;
use function array_merge;
use function array_unique;
use function gettype;
use function is_object;
use function is_string;
use function method_exists;
use function preg_match_all;
use function preg_replace;
use function sprintf;
use function strpos;
use const PREG_SET_ORDER;
final class Template
{
/**
* Expression regular expression pattern.
*/
private const REGEXP_EXPRESSION_DETECTOR = '/\{[^\}]*\}/x';
/**
* @var string
*/
private $template;
/**
* @var array<string, Expression>
*/
private $expressions = [];
/**
* @var array<string>
*/
private $variableNames;
private function __construct(string $template, Expression ...$expressions)
{
$this->template = $template;
$variableNames = [];
foreach ($expressions as $expression) {
$this->expressions[$expression->toString()] = $expression;
$variableNames[] = $expression->variableNames();
}
$this->variableNames = array_unique(array_merge([], ...$variableNames));
}
/**
* {@inheritDoc}
*/
public static function __set_state(array $properties): self
{
return new self($properties['template'], ...array_values($properties['expressions']));
}
/**
* @param object|string $template a string or an object with the __toString method
*
* @throws \TypeError if the template is not a string or an object with the __toString method
* @throws SyntaxError if the template contains invalid expressions
* @throws SyntaxError if the template contains invalid variable specification
*/
public static function createFromString($template): self
{
if (is_object($template) && method_exists($template, '__toString')) {
$template = (string) $template;
}
if (!is_string($template)) {
throw new \TypeError(sprintf('The template must be a string or a stringable object %s given.', gettype($template)));
}
/** @var string $remainder */
$remainder = preg_replace(self::REGEXP_EXPRESSION_DETECTOR, '', $template);
if (false !== strpos($remainder, '{') || false !== strpos($remainder, '}')) {
throw new SyntaxError('The template "'.$template.'" contains invalid expressions.');
}
$names = [];
preg_match_all(self::REGEXP_EXPRESSION_DETECTOR, $template, $findings, PREG_SET_ORDER);
$arguments = [];
foreach ($findings as $finding) {
if (!isset($names[$finding[0]])) {
$arguments[] = Expression::createFromString($finding[0]);
$names[$finding[0]] = 1;
}
}
return new self($template, ...$arguments);
}
public function toString(): string
{
return $this->template;
}
/**
* @return array<string>
*/
public function variableNames(): array
{
return $this->variableNames;
}
/**
* @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(VariableBag $variables): string
{
$uriString = $this->template;
/** @var Expression $expression */
foreach ($this->expressions as $pattern => $expression) {
$uriString = str_replace($pattern, $expression->expand($variables), $uriString);
}
return $uriString;
}
}

View file

@ -0,0 +1,107 @@
<?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 League\Uri\UriTemplate;
use League\Uri\Exceptions\SyntaxError;
use function preg_match;
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+)|\*)?
$/x';
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $modifier;
/**
* @var int
*/
private $position;
private function __construct(string $name, string $modifier, int $position)
{
$this->name = $name;
$this->modifier = $modifier;
$this->position = $position;
}
/**
* {@inheritDoc}
*/
public static function __set_state(array $properties): self
{
return new self($properties['name'], $properties['modifier'], $properties['position']);
}
public static function createFromString(string $specification): self
{
if (1 !== preg_match(self::REGEXP_VARSPEC, $specification, $parsed)) {
throw new SyntaxError('The variable specification "'.$specification.'" is invalid.');
}
$parsed += ['modifier' => '', 'position' => ''];
if ('' !== $parsed['position']) {
$parsed['position'] = (int) $parsed['position'];
$parsed['modifier'] = ':';
}
if ('' === $parsed['position']) {
$parsed['position'] = 0;
}
if (10000 <= $parsed['position']) {
throw new SyntaxError('The variable specification "'.$specification.'" is invalid the position modifier must be lower than 10000.');
}
return new self($parsed['name'], $parsed['modifier'], $parsed['position']);
}
public function toString(): string
{
if (0 < $this->position) {
return $this->name.$this->modifier.$this->position;
}
return $this->name.$this->modifier;
}
public function name(): string
{
return $this->name;
}
public function modifier(): string
{
return $this->modifier;
}
public function position(): int
{
return $this->position;
}
}

View file

@ -0,0 +1,116 @@
<?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 League\Uri\UriTemplate;
use League\Uri\Exceptions\TemplateCanNotBeExpanded;
use function gettype;
use function is_array;
use function is_bool;
use function is_object;
use function is_scalar;
use function method_exists;
use function sprintf;
final class VariableBag
{
/**
* @var array<string,string|array<string>>
*/
private $variables = [];
/**
* @param iterable<string,mixed> $variables
*/
public function __construct(iterable $variables = [])
{
foreach ($variables as $name => $value) {
$this->assign($name, $value);
}
}
public static function __set_state(array $properties): self
{
return new self($properties['variables']);
}
/**
* @return array<string,string|array<string>>
*/
public function all(): array
{
return $this->variables;
}
/**
* Fetches the variable value if none found returns null.
*
* @return null|string|array<string>
*/
public function fetch(string $name)
{
return $this->variables[$name] ?? null;
}
/**
* @param string|array<string> $value
*/
public function assign(string $name, $value): void
{
$this->variables[$name] = $this->normalizeValue($value, $name, true);
}
/**
* @param mixed $value the value to be expanded
*
* @throws TemplateCanNotBeExpanded if the value contains nested list
*
* @return string|array<string>
*/
private function normalizeValue($value, string $name, bool $isNestedListAllowed)
{
if (is_bool($value)) {
return true === $value ? '1' : '0';
}
if (null === $value || is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
return (string) $value;
}
if (!is_array($value)) {
throw new \TypeError(sprintf('The variable '.$name.' must be NULL, a scalar or a stringable object `%s` given', gettype($value)));
}
if (!$isNestedListAllowed) {
throw TemplateCanNotBeExpanded::dueToNestedListOfValue($name);
}
foreach ($value as &$var) {
$var = self::normalizeValue($var, $name, false);
}
unset($var);
return $value;
}
/**
* Replaces elements from passed variables into the current instance.
*/
public function replace(VariableBag $variables): self
{
$instance = clone $this;
$instance->variables += $variables->variables;
return $instance;
}
}