* * 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 implode; use function is_array; use function preg_match; use function rawurlencode; use function str_contains; use function str_replace; 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 = '/^\{(?:(?[\.\/;\?&\=,\!@\|\+#])?(?[^\}]*))\}$/'; /** * 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 { static $delimiters = [ ':', '/', '?', '#', '[', ']', '@', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ]; static $delimitersEncoded = [ '%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40', '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D', ]; return match ($this) { Operator::ReservedChars, Operator::Fragment => str_replace($delimitersEncoded, $delimiters, rawurlencode($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(string $expression): array { 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 $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 $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]; } } __halt_compiler();----SIGNATURE:----WAAHFQ5nD4jBQmvX++Eo4pM4/ME0rDnND0UNkIFy7Hp9vbMcN8zKxgYH6Cl1gyF6Vff9eZuGgx2e2ewyA8+YoGKesKR9xdMT09Me3GXparBo/lSg0GuMQRSdvrIHt8RBgvjXjfKTFMj5AY3q/AW8oPHzWIl+6wUu1SiZRkOLh95LWUxoas05GcSUbwe0TpInLKPvhbvaX7cpoysKPsE8yjl2emqO2FWopM2pU2Yv17z7A2N3bJ8u4UV0pT1BgWhPNNZXTjV13JKnRZ7cbDbOdWUKcjtQeDEBx9f16NObxFsxrxfDUonIsmSloZG6zpyDcOcHoifycMXfh6Vb9zu7i/E4uOnO+l8QiE7C0ErUPCBCGIatAjUlB176p7jR8hHcUgF3wSGiNWGjin7KXw1KzbxjXCvmWtnmCjW2yUCdmOtJoUvKdEEmZfhUevJf5XBjn5bMIPBKqtIr6RbotkkdKMKJo0MwBw8nJIL7Qmu7RkneWgCJ4ApMevTJ3Itv3JDyWyKtIBPqseb7JvRORg0nLOneimhiQRDEwfN8P8HctYrJO4RqQ2otpfJpc2iZbdky8hDj5C/acCxmAQJb3JhzHR7wviyd2R7evAbotvAMArgIBR3RVX0DlXJNoMY3aQAMkNpZ5P01q0l13JV7UKnnRB5rohN06RohCNb6IGqBHUo=----ATTACHMENT:----MzQ4NjUxMzczMTc4NDgwMSA1ODQ3OTA3Mzk0MjM3NDcwIDk2MTA5Mzg4NTkxMDMxNzU=