|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Form\Preset; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use WebHemi\Form\ElementInterface; |
|
18
|
|
|
use WebHemi\Form\PresetInterface; |
|
19
|
|
|
use WebHemi\Form\ServiceInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AbstractPreset |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractPreset implements PresetInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var ServiceInterface */ |
|
27
|
|
|
protected $formAdapter; |
|
28
|
|
|
/** @var ElementInterface[] */ |
|
29
|
|
|
private $elementPrototypes; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* LogSelector constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param ServiceInterface $formAdapter |
|
35
|
|
|
* @param ElementInterface[] ...$elementPrototypes |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(ServiceInterface $formAdapter, ElementInterface ...$elementPrototypes) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->formAdapter = $formAdapter; |
|
40
|
|
|
|
|
41
|
|
|
foreach ($elementPrototypes as $formElement) { |
|
42
|
|
|
$this->elementPrototypes[get_class($formElement)] = $formElement; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->init(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Initialize and add elements to the form. |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract protected function init() : void; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create a certain type of element. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $class |
|
59
|
|
|
* @param string $type |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* @param string $label |
|
62
|
|
|
* @return ElementInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function createElement(string $class, string $type, string $name, string $label) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!isset($this->elementPrototypes[$class])) { |
|
67
|
|
|
throw new InvalidArgumentException( |
|
68
|
|
|
sprintf('%s is not injected into the %s preset', $class, get_called_class()), |
|
69
|
|
|
1000 |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @var ElementInterface $element */ |
|
74
|
|
|
$element = clone $this->elementPrototypes[$class]; |
|
75
|
|
|
|
|
76
|
|
|
$element->setType($type) |
|
77
|
|
|
->setName($name) |
|
78
|
|
|
->setLabel($label); |
|
79
|
|
|
|
|
80
|
|
|
return $element; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the initialized form. |
|
85
|
|
|
* |
|
86
|
|
|
* @return ServiceInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getPreset() : ServiceInterface |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->formAdapter; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|