diff options
author | Andrew Dolgov <[email protected]> | 2022-07-24 14:03:04 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2022-07-24 14:03:04 +0300 |
commit | 5006c754c42a09f5b88b258c2da4b9eff7508357 (patch) | |
tree | 841425d4e163d746642d70b8ac0df381343bbf2b /plugins/af_readability/vendor/league/uri/src/UriTemplate | |
parent | f7b3c50828d4604ac5999daf3c3405e65c877e2f (diff) |
readability: add missing dependencies
Diffstat (limited to 'plugins/af_readability/vendor/league/uri/src/UriTemplate')
4 files changed, 665 insertions, 0 deletions
diff --git a/plugins/af_readability/vendor/league/uri/src/UriTemplate/Expression.php b/plugins/af_readability/vendor/league/uri/src/UriTemplate/Expression.php new file mode 100644 index 000000000..99ecac98b --- /dev/null +++ b/plugins/af_readability/vendor/league/uri/src/UriTemplate/Expression.php @@ -0,0 +1,329 @@ +<?php + +/** + * League.Uri (https://uri.thephpleague.com) + * + * (c) Ignace Nyamagana Butera <[email protected]> + * + * 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], + ]; + + private string $operator; + /** @var array<VarSpecifier> */ + private array $varSpecifiers; + private string $joiner; + /** @var array<string> */ + private array $variableNames; + private string $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 + { + return array_unique(array_map( + static fn (VarSpecifier $varSpecifier): string => $varSpecifier->name(), + $this->varSpecifiers + )); + } + + private function setExpressionString(): string + { + $varSpecifierString = implode(',', array_map( + static fn (VarSpecifier $variable): string => $variable->toString(), + $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.'); + } + + return new Expression($parts['operator'], ...array_map( + static fn (string $varSpec): VarSpecifier => VarSpecifier::createFromString($varSpec), + 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); + } + + $expanded = implode($this->joiner, array_filter($parts, static fn ($value): bool => '' !== $value)); + 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); + } +} diff --git a/plugins/af_readability/vendor/league/uri/src/UriTemplate/Template.php b/plugins/af_readability/vendor/league/uri/src/UriTemplate/Template.php new file mode 100644 index 000000000..ecd130fe1 --- /dev/null +++ b/plugins/af_readability/vendor/league/uri/src/UriTemplate/Template.php @@ -0,0 +1,126 @@ +<?php + +/** + * League.Uri (https://uri.thephpleague.com) + * + * (c) Ignace Nyamagana Butera <[email protected]> + * + * 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 TypeError; +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'; + + private string $template; + /** @var array<string, Expression> */ + private array $expressions = []; + /** @var array<string> */ + private array $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; + } +} diff --git a/plugins/af_readability/vendor/league/uri/src/UriTemplate/VarSpecifier.php b/plugins/af_readability/vendor/league/uri/src/UriTemplate/VarSpecifier.php new file mode 100644 index 000000000..ac49efb53 --- /dev/null +++ b/plugins/af_readability/vendor/league/uri/src/UriTemplate/VarSpecifier.php @@ -0,0 +1,96 @@ +<?php + +/** + * League.Uri (https://uri.thephpleague.com) + * + * (c) Ignace Nyamagana Butera <[email protected]> + * + * 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'; + + private string $name; + private string $modifier; + private int $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; + } +} diff --git a/plugins/af_readability/vendor/league/uri/src/UriTemplate/VariableBag.php b/plugins/af_readability/vendor/league/uri/src/UriTemplate/VariableBag.php new file mode 100644 index 000000000..cf60de91e --- /dev/null +++ b/plugins/af_readability/vendor/league/uri/src/UriTemplate/VariableBag.php @@ -0,0 +1,114 @@ +<?php + +/** + * League.Uri (https://uri.thephpleague.com) + * + * (c) Ignace Nyamagana Butera <[email protected]> + * + * 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 TypeError; +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 array $variables = []; + + /** + * @param iterable<string,string|bool|int|float|array<string|bool|int|float>> $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|bool|int|float|array<string|bool|int|float> $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 + { + return new self($this->variables + $variables->variables); + } +} |