1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Monsieur Biz' Rich Editor plugin for Sylius. |
5
|
|
|
* |
6
|
|
|
* (c) Monsieur Biz <[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 MonsieurBiz\SyliusRichEditorPlugin\UiElement; |
15
|
|
|
|
16
|
|
|
final class Metadata implements MetadataInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private string $code; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private array $parameters; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Metadata constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $code |
32
|
|
|
* @param array $parameters |
33
|
|
|
*/ |
34
|
|
|
private function __construct(string $code, array $parameters) |
35
|
|
|
{ |
36
|
|
|
if (!isset($parameters['classes'], $parameters['templates'])) { |
37
|
|
|
throw new \InvalidArgumentException('Classes and Templates must be specified in parameters.'); |
38
|
|
|
} |
39
|
|
|
if (1 !== substr_count($code, '.')) { |
40
|
|
|
throw new \InvalidArgumentException('The Code should contain one dot (.).'); |
41
|
|
|
} |
42
|
|
|
$this->code = $code; |
43
|
|
|
$this->parameters = $parameters; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public static function fromCodeAndConfiguration(string $code, array $parameters): self |
50
|
|
|
{ |
51
|
|
|
return new self($code, $parameters); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getCode(): string |
58
|
|
|
{ |
59
|
|
|
return $this->code; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getAlias(): ?string |
66
|
|
|
{ |
67
|
|
|
return $this->parameters['alias'] ?? null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getCamelCasedCode(): string |
74
|
|
|
{ |
75
|
|
|
return (string) preg_replace_callback('/\.([a-z])/i', function($match) { |
76
|
|
|
return strtoupper($match[1]); |
77
|
|
|
}, $this->getCode()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function isEnabled(): bool |
84
|
|
|
{ |
85
|
|
|
return $this->parameters['enabled']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getParameter(string $name) |
92
|
|
|
{ |
93
|
|
|
if (!$this->hasParameter($name)) { |
94
|
|
|
throw new \InvalidArgumentException(sprintf('Parameter "%s" is not configured for resource "%s".', $name, $this->getCode())); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->parameters[$name]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function hasParameter(string $name): bool |
104
|
|
|
{ |
105
|
|
|
return \array_key_exists($name, $this->parameters); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function getParameters(): array |
112
|
|
|
{ |
113
|
|
|
return $this->parameters; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function getClass(string $name): string |
120
|
|
|
{ |
121
|
|
|
if (!$this->hasClass($name)) { |
122
|
|
|
throw new \InvalidArgumentException(sprintf('Class "%s" is not configured for resource "%s".', $name, $this->getCode())); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->parameters['classes'][$name]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function hasClass(string $name): bool |
132
|
|
|
{ |
133
|
|
|
return isset($this->parameters['classes'][$name]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function getTemplate(string $name): string |
140
|
|
|
{ |
141
|
|
|
if (!$this->hasTemplate($name)) { |
142
|
|
|
throw new \InvalidArgumentException(sprintf('Template "%s" is not configured for resource "%s".', $name, $this->getCode())); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->parameters['templates'][$name]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function hasTemplate(string $name): bool |
152
|
|
|
{ |
153
|
|
|
return isset($this->parameters['templates'][$name]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function getServiceId(string $serviceName): string |
160
|
|
|
{ |
161
|
|
|
$code = explode('.', $this->code); |
162
|
|
|
|
163
|
|
|
return sprintf('%s.%s.%s', $code[0], $serviceName, $code[1]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function getTags(): array |
170
|
|
|
{ |
171
|
|
|
return $this->parameters['tags'] ?? []; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|