|
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\UriTemplate; |
|
15
|
|
|
|
|
16
|
|
|
use League\Uri\Exceptions\TemplateCanNotBeExpanded; |
|
17
|
|
|
use function gettype; |
|
18
|
|
|
use function is_array; |
|
19
|
|
|
use function is_bool; |
|
20
|
|
|
use function is_scalar; |
|
21
|
|
|
use function method_exists; |
|
22
|
|
|
use function sprintf; |
|
23
|
|
|
|
|
24
|
|
|
final class VariableBag |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array<string,string|array<string>> |
|
28
|
|
|
*/ |
|
29
|
|
|
private $variables = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param iterable<string,mixed> $variables |
|
33
|
|
|
*/ |
|
34
|
18 |
|
public function __construct(iterable $variables = []) |
|
35
|
|
|
{ |
|
36
|
18 |
|
foreach ($variables as $name => $value) { |
|
37
|
12 |
|
$this->assign($name, $value); |
|
38
|
|
|
} |
|
39
|
14 |
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
public static function __set_state(array $properties): self |
|
42
|
|
|
{ |
|
43
|
2 |
|
return new self($properties['variables']); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return array<string,string|array<string>> |
|
48
|
|
|
*/ |
|
49
|
6 |
|
public function all(): array |
|
50
|
|
|
{ |
|
51
|
6 |
|
return $this->variables; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Fetches the variable value if none found returns null. |
|
56
|
|
|
* |
|
57
|
|
|
* @return null|string|array<string> |
|
58
|
|
|
*/ |
|
59
|
14 |
|
public function fetch(string $name) |
|
60
|
|
|
{ |
|
61
|
14 |
|
return $this->variables[$name] ?? null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string|array<string> $value |
|
66
|
|
|
*/ |
|
67
|
24 |
|
public function assign(string $name, $value): void |
|
68
|
|
|
{ |
|
69
|
24 |
|
$this->variables[$name] = $this->normalizeValue($value, $name, true); |
|
70
|
20 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param mixed $value the value to be expanded |
|
74
|
|
|
* |
|
75
|
|
|
* @throws TemplateCanNotBeExpanded if the value contains nested list |
|
76
|
|
|
* |
|
77
|
|
|
* @return string|array<string> |
|
78
|
|
|
*/ |
|
79
|
24 |
|
private function normalizeValue($value, string $name, bool $isNestedListAllowed) |
|
80
|
|
|
{ |
|
81
|
24 |
|
if (is_bool($value)) { |
|
82
|
4 |
|
return true === $value ? '1' : '0'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
22 |
|
if (null === $value || is_scalar($value) || method_exists($value, '__toString')) { |
|
86
|
18 |
|
return (string) $value; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
10 |
|
if (!is_array($value)) { |
|
90
|
2 |
|
throw new \TypeError(sprintf('The variable '.$name.' must be NULL, a scalar or a stringable object `%s` given', gettype($value))); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
8 |
|
if (!$isNestedListAllowed) { |
|
94
|
2 |
|
throw TemplateCanNotBeExpanded::dueToNestedListOfValue($name); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
8 |
|
foreach ($value as &$var) { |
|
98
|
8 |
|
$var = self::normalizeValue($var, $name, false); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
6 |
|
unset($var); |
|
101
|
|
|
|
|
102
|
6 |
|
return $value; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Replaces elements from passed variables into the current instance. |
|
107
|
|
|
*/ |
|
108
|
4 |
|
public function replace(VariableBag $variables): self |
|
109
|
|
|
{ |
|
110
|
4 |
|
$instance = clone $this; |
|
111
|
4 |
|
$instance->variables += $variables->variables; |
|
112
|
|
|
|
|
113
|
4 |
|
return $instance; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|