1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setherator\Variables; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Aurimas Niekis <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Variables |
14
|
|
|
{ |
15
|
|
|
protected static ?Variables $instance; |
16
|
|
|
protected array $variables; |
17
|
|
|
protected array $context; |
18
|
|
|
|
19
|
|
|
/** @var ValueParserInterface[] */ |
20
|
|
|
protected array $valueParsers; |
21
|
|
|
|
22
|
17 |
|
public function __construct() |
23
|
|
|
{ |
24
|
17 |
|
static::$instance = $this; |
25
|
|
|
|
26
|
17 |
|
$this->variables = []; |
27
|
17 |
|
$this->context = []; |
28
|
17 |
|
$this->valueParsers = []; |
29
|
17 |
|
} |
30
|
|
|
|
31
|
1 |
|
public function add(...$variables): self |
32
|
|
|
{ |
33
|
1 |
|
$this->variables = array_replace($this->variables, ...$variables); |
34
|
|
|
|
35
|
1 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function addValueParser(ValueParserInterface $valueParser): self |
39
|
|
|
{ |
40
|
1 |
|
$this->valueParsers[] = $valueParser; |
41
|
|
|
|
42
|
1 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function getContext(): array |
46
|
|
|
{ |
47
|
2 |
|
return $this->context; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function setContext(array $context): self |
51
|
|
|
{ |
52
|
2 |
|
$this->context = $context; |
53
|
|
|
|
54
|
2 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
13 |
|
public static function getInstance(): self |
58
|
|
|
{ |
59
|
13 |
|
if (isset(static::$instance)) { |
60
|
12 |
|
return static::$instance; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
throw new RuntimeException(static::class . ' was not initialised yet'); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function all(bool $compute = true): array |
67
|
|
|
{ |
68
|
1 |
|
if (false === $compute) { |
69
|
1 |
|
return $this->variables; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$result = []; |
73
|
1 |
|
foreach ($this->variables as $name => $value) { |
74
|
1 |
|
$result[$name] = $this->get($name); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return $result; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function names(): array |
81
|
|
|
{ |
82
|
1 |
|
return array_keys($this->variables); |
83
|
|
|
} |
84
|
|
|
|
85
|
11 |
|
public function set(string $key, $value): self |
86
|
|
|
{ |
87
|
11 |
|
$this->variables[$key] = $value; |
88
|
|
|
|
89
|
11 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function has(string $key): bool |
93
|
|
|
{ |
94
|
1 |
|
return array_key_exists($key, $this->variables); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function remove(string $key): self |
98
|
|
|
{ |
99
|
1 |
|
unset($this->variables[$key]); |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
public function getRaw(string $name, $default = null) |
105
|
|
|
{ |
106
|
2 |
|
if (false === isset($this->variables[$name])) { |
107
|
1 |
|
return $default; |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
return $this->variables[$name]; |
111
|
|
|
} |
112
|
|
|
|
113
|
12 |
|
public function get(string $name, $default = null) |
114
|
|
|
{ |
115
|
12 |
|
if (false === isset($this->variables[$name])) { |
116
|
2 |
|
return $this->parseValue($default); |
117
|
|
|
} |
118
|
|
|
|
119
|
12 |
|
$value = $this->variables[$name]; |
120
|
|
|
|
121
|
12 |
|
if ($value instanceof NonCacheableClosure) { |
122
|
5 |
|
return $this->parseValue($value); |
123
|
|
|
} |
124
|
|
|
|
125
|
11 |
|
$this->variables[$name] = $this->parseValue($value); |
126
|
|
|
|
127
|
11 |
|
return $this->variables[$name]; |
128
|
|
|
} |
129
|
|
|
|
130
|
16 |
|
public function parseValue($value) |
131
|
|
|
{ |
132
|
16 |
|
if ($value instanceof Closure || $value instanceof NonCacheableClosure) { |
133
|
14 |
|
return $this->parseValue($value()); |
134
|
|
|
} |
135
|
|
|
|
136
|
16 |
|
foreach ($this->valueParsers as $valueParser) { |
137
|
1 |
|
if (true === $valueParser->supports($value)) { |
138
|
1 |
|
return $this->parseValue( |
139
|
1 |
|
$valueParser->parseValue($value, $this) |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
16 |
|
return $value; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|