|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MonsieurBiz\SyliusRichEditorPlugin\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use MonsieurBiz\SyliusRichEditorPlugin\Exception\DuplicatedUiElementTypeException; |
|
8
|
|
|
use MonsieurBiz\SyliusRichEditorPlugin\Exception\UndefinedUiElementTypeException; |
|
9
|
|
|
use MonsieurBiz\SyliusRichEditorPlugin\UiElement\UiElementInterface; |
|
10
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
11
|
|
|
use Webmozart\Assert\Assert; |
|
12
|
|
|
|
|
13
|
|
|
final class UiElementFactory implements UiElementFactoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private $uiElements = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* UiElementFactory constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param TranslatorInterface $translator |
|
21
|
|
|
* @param string[] $uiElementClasses |
|
22
|
|
|
* |
|
23
|
|
|
* @throws DuplicatedUiElementTypeException |
|
24
|
|
|
* @throws \ReflectionException |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(TranslatorInterface $translator, array $uiElementClasses) |
|
27
|
|
|
{ |
|
28
|
|
|
foreach ($uiElementClasses as $uiElementClass) { |
|
29
|
|
|
/** @var UiElementInterface $uiElement */ |
|
30
|
|
|
$uiElement = new $uiElementClass($translator); |
|
31
|
|
|
Assert::isInstanceOf($uiElement, UiElementInterface::class); |
|
32
|
|
|
if (isset($this->uiElements[$uiElement->getType()])) { |
|
33
|
|
|
$reflection = new \ReflectionClass($this->uiElements[$uiElement->getType()]); |
|
34
|
|
|
throw new DuplicatedUiElementTypeException(sprintf( |
|
35
|
|
|
'The UI Element with type "%s" already exists in class "%s', |
|
36
|
|
|
$uiElement->getType(), |
|
37
|
|
|
$reflection->getName() |
|
38
|
|
|
)); |
|
39
|
|
|
} |
|
40
|
|
|
$this->uiElements[$uiElement->getType()] = $uiElement; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return UiElementInterface[] |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getUiElements(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->uiElements; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $type |
|
54
|
|
|
* @return UiElementInterface |
|
55
|
|
|
* @throws UndefinedUiElementTypeException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getUiElementByType(string $type): UiElementInterface |
|
58
|
|
|
{ |
|
59
|
|
|
if (!isset($this->uiElements[$type])) { |
|
60
|
|
|
throw new UndefinedUiElementTypeException(sprintf('The UI Element type "%s" cannot be found', $type)); |
|
61
|
|
|
} |
|
62
|
|
|
return $this->uiElements[$type]; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|