|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Cycle\Schema\Relation; |
|
11
|
|
|
|
|
12
|
|
|
use Cycle\Schema\Exception\OptionException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Calculate missing option values using template and relation context. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class OptionSchema |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
private $aliases = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
private $options = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
private $template = []; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array */ |
|
29
|
|
|
private $context = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $aliases |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(array $aliases) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->aliases = $aliases; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create new option set with user provided options. |
|
41
|
|
|
* |
|
42
|
|
|
* @param iterable $options |
|
43
|
|
|
* @return OptionSchema |
|
44
|
|
|
*/ |
|
45
|
|
|
public function withOptions(iterable $options): self |
|
46
|
|
|
{ |
|
47
|
|
|
$r = clone $this; |
|
48
|
|
|
foreach ($options as $name => $value) { |
|
49
|
|
|
if (isset($r->aliases[$name]) && isset($r->template[$name])) { |
|
50
|
|
|
throw new OptionException("Undefined relation option `{$name}`"); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$r->options[$name] = $value; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $r; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create new option set with option rendering template. Template expect to allocate |
|
61
|
|
|
* relation options only in a integer constants. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $template |
|
64
|
|
|
* @return OptionSchema |
|
65
|
|
|
*/ |
|
66
|
|
|
public function withTemplate(array $template): self |
|
67
|
|
|
{ |
|
68
|
|
|
$r = clone $this; |
|
69
|
|
|
$r->template = $template; |
|
70
|
|
|
|
|
71
|
|
|
return $r; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create new option set with relation context values (i.e. relation name, target name and etc). |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $context |
|
78
|
|
|
* @return OptionSchema |
|
79
|
|
|
*/ |
|
80
|
|
|
public function withContext(array $context): self |
|
81
|
|
|
{ |
|
82
|
|
|
$r = clone $this; |
|
83
|
|
|
$r->context += $context; |
|
84
|
|
|
|
|
85
|
|
|
return $r; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Check if option has been defined. |
|
90
|
|
|
* |
|
91
|
|
|
* @param int $option |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function has(int $option): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return isset($this->template[$option]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get calculated option value. |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $option |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public function get(int $option) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->has($option)) { |
|
108
|
|
|
throw new OptionException("Undefined relation option `{$option}`"); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// user defined value |
|
112
|
|
|
foreach ($this->aliases as $alias => $targetOption) { |
|
113
|
|
|
if ($targetOption === $option && isset($this->options[$alias])) { |
|
114
|
|
|
return $this->options[$alias]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// non template value |
|
119
|
|
|
$value = $this->template[$option]; |
|
120
|
|
|
if (!is_string($value)) { |
|
121
|
|
|
return $value; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->calculate($option, $value); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function __debugInfo() |
|
131
|
|
|
{ |
|
132
|
|
|
$result = []; |
|
133
|
|
|
|
|
134
|
|
|
foreach ($this->template as $option => $value) { |
|
135
|
|
|
$value = $this->get($option); |
|
136
|
|
|
|
|
137
|
|
|
$alias = array_search($option, $this->aliases, true); |
|
138
|
|
|
$result[$alias] = $value; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $result; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Calculate option value using templating. |
|
146
|
|
|
* |
|
147
|
|
|
* @param int $option |
|
148
|
|
|
* @param string $value |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
private function calculate(int $option, string $value): string |
|
152
|
|
|
{ |
|
153
|
|
|
foreach ($this->context as $name => $ctxValue) { |
|
154
|
|
|
$value = $this->injectValue($name, $ctxValue, $value); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
foreach ($this->aliases as $name => $targetOption) { |
|
158
|
|
|
if ($option !== $targetOption) { |
|
159
|
|
|
$value = $this->injectOption($name, $targetOption, $value); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $value; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $name |
|
168
|
|
|
* @param int $option |
|
169
|
|
|
* @param string $target |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
private function injectOption(string $name, int $option, string $target): string |
|
173
|
|
|
{ |
|
174
|
|
|
if (strpos($target, "{{$name}}") === false) { |
|
175
|
|
|
return $target; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return str_replace("{{$name}}", $this->get($option), $target); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $name |
|
183
|
|
|
* @param string $value |
|
184
|
|
|
* @param string $target |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
private function injectValue(string $name, string $value, string $target): string |
|
188
|
|
|
{ |
|
189
|
|
|
return str_replace("{{$name}}", $value, $target); |
|
190
|
|
|
} |
|
191
|
|
|
} |