PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.1
Defender Security – Malware Scanner, Login Security & Firewall v5.4.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / league / uri / src / UriTemplate.php

UriTemplate.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.1, at vendor/league/uri/src/UriTemplate.php

135 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 League\Uri;
15
16 use League\Uri\Contracts\UriException;
17 use League\Uri\Contracts\UriInterface;
18 use League\Uri\Exceptions\SyntaxError;
19 use League\Uri\Exceptions\TemplateCanNotBeExpanded;
20 use League\Uri\UriTemplate\Template;
21 use 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 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