| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* League.Uri (https://uri.thephpleague.com) |
| 5 |
* |
| 6 |
* (c) Ignace Nyamagana Butera <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace WP_DEFENDER_VENDOR\League\Uri; |
| 15 |
|
| 16 |
use WP_DEFENDER_VENDOR\League\Uri\Contracts\UriException; |
| 17 |
use WP_DEFENDER_VENDOR\League\Uri\Contracts\UriInterface; |
| 18 |
use WP_DEFENDER_VENDOR\League\Uri\Exceptions\SyntaxError; |
| 19 |
use WP_DEFENDER_VENDOR\League\Uri\Exceptions\TemplateCanNotBeExpanded; |
| 20 |
use WP_DEFENDER_VENDOR\League\Uri\UriTemplate\Template; |
| 21 |
use WP_DEFENDER_VENDOR\League\Uri\UriTemplate\VariableBag; |
| 22 |
use TypeError; |
| 23 |
|
| 24 |
/** |
| 25 |
* Defines the URI Template syntax and the process for expanding a URI Template into a URI reference. |
| 26 |
* |
| 27 |
* @link https://tools.ietf.org/html/rfc6570 |
| 28 |
* @package WP_DEFENDER_VENDOR\League\Uri |
| 29 |
* @author Ignace Nyamagana Butera <[email protected]> |
| 30 |
* @since 6.1.0 |
| 31 |
* |
| 32 |
* Based on GuzzleHttp\UriTemplate class in Guzzle v6.5. |
| 33 |
* @link https://github.com/guzzle/guzzle/blob/6.5/src/UriTemplate.php |
| 34 |
*/ |
| 35 |
final class UriTemplate |
| 36 |
{ |
| 37 |
private Template $template; |
| 38 |
private VariableBag $defaultVariables; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param object|string $template a string or an object with the __toString method |
| 42 |
* |
| 43 |
* @throws TypeError if the template is not a string or an object with the __toString method |
| 44 |
* @throws SyntaxError if the template syntax is invalid |
| 45 |
* @throws TemplateCanNotBeExpanded if the template variables are invalid |
| 46 |
*/ |
| 47 |
public function __construct($template, array $defaultVariables = []) |
| 48 |
{ |
| 49 |
$this->template = Template::createFromString($template); |
| 50 |
$this->defaultVariables = $this->filterVariables($defaultVariables); |
| 51 |
} |
| 52 |
|
| 53 |
public static function __set_state(array $properties): self |
| 54 |
{ |
| 55 |
return new self($properties['template']->toString(), $properties['defaultVariables']->all()); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Filters out variables for the given template. |
| 60 |
* |
| 61 |
* @param array<string,string|array<string>> $variables |
| 62 |
*/ |
| 63 |
private function filterVariables(array $variables): VariableBag |
| 64 |
{ |
| 65 |
$output = new VariableBag(); |
| 66 |
foreach ($this->template->variableNames() as $name) { |
| 67 |
if (isset($variables[$name])) { |
| 68 |
$output->assign($name, $variables[$name]); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $output; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* The template string. |
| 77 |
*/ |
| 78 |
public function getTemplate(): string |
| 79 |
{ |
| 80 |
return $this->template->toString(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the names of the variables in the template, in order. |
| 85 |
* |
| 86 |
* @return string[] |
| 87 |
*/ |
| 88 |
public function getVariableNames(): array |
| 89 |
{ |
| 90 |
return $this->template->variableNames(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the default values used to expand the template. |
| 95 |
* |
| 96 |
* The returned list only contains variables whose name is part of the current template. |
| 97 |
* |
| 98 |
* @return array<string,string|array> |
| 99 |
*/ |
| 100 |
public function getDefaultVariables(): array |
| 101 |
{ |
| 102 |
return $this->defaultVariables->all(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns a new instance with the updated default variables. |
| 107 |
* |
| 108 |
* This method MUST retain the state of the current instance, and return |
| 109 |
* an instance that contains the modified default variables. |
| 110 |
* |
| 111 |
* If present, variables whose name is not part of the current template |
| 112 |
* possible variable names are removed. |
| 113 |
*/ |
| 114 |
public function withDefaultVariables(array $defaultDefaultVariables): self |
| 115 |
{ |
| 116 |
return new self( |
| 117 |
$this->template->toString(), |
| 118 |
$this->filterVariables($defaultDefaultVariables)->all() |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @throws TemplateCanNotBeExpanded if the variable contains nested array values |
| 124 |
* @throws UriException if the resulting expansion can not be converted to a UriInterface instance |
| 125 |
*/ |
| 126 |
public function expand(array $variables = []): UriInterface |
| 127 |
{ |
| 128 |
return Uri::createFromString( |
| 129 |
$this->template->expand( |
| 130 |
$this->filterVariables($variables)->replace($this->defaultVariables) |
| 131 |
) |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
|