1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Curlyspoon\Core\Elements; |
4
|
|
|
|
5
|
|
|
use Curlyspoon\Core\Contracts\Element as ElementContract; |
6
|
|
|
use Exception; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
|
9
|
|
|
abstract class Element implements ElementContract |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
* |
19
|
|
|
* @see OptionsResolver::setDefault() |
20
|
|
|
*/ |
21
|
|
|
protected $defaults = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
* |
26
|
|
|
* @see OptionsResolver::setRequired() |
27
|
|
|
*/ |
28
|
|
|
protected $required = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
* |
33
|
|
|
* @see OptionsResolver::setAllowedTypes() |
34
|
|
|
*/ |
35
|
|
|
protected $types = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
* |
40
|
|
|
* @see OptionsResolver::setAllowedValues() |
41
|
|
|
*/ |
42
|
|
|
protected $values = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $options = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var OptionsResolver |
51
|
|
|
*/ |
52
|
|
|
protected $resolver; |
53
|
|
|
|
54
|
|
|
public function __construct(array $options = []) |
55
|
|
|
{ |
56
|
|
|
$this->setOptions($options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function make(array $options = []): ElementContract |
60
|
|
|
{ |
61
|
|
|
return new static($options); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setOptions(array $options): ElementContract |
65
|
|
|
{ |
66
|
|
|
$this->options = $options; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getOptions(): array |
72
|
|
|
{ |
73
|
|
|
return $this->options; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function resolve(): ElementContract |
77
|
|
|
{ |
78
|
|
|
$this->setOptions($this->optionsResolver()->resolve($this->getOptions())); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function isValid(): bool |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$this->optionsResolver()->resolve($this->getOptions()); |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} catch (Exception $exception) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
abstract public function render(): string; |
95
|
|
|
|
96
|
|
|
public function toString(): string |
97
|
|
|
{ |
98
|
|
|
return $this->render(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function __toString(): string |
102
|
|
|
{ |
103
|
|
|
return $this->toString(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function optionsResolver(): OptionsResolver |
107
|
|
|
{ |
108
|
|
|
if (is_null($this->resolver)) { |
109
|
|
|
$this->resolver = new OptionsResolver(); |
110
|
|
|
$this->resolver->setDefaults($this->defaults); |
111
|
|
|
|
112
|
|
|
$this->resolver->setRequired($this->required); |
113
|
|
|
|
114
|
|
|
foreach ($this->types as $option => $types) { |
115
|
|
|
$this->resolver->setAllowedTypes($option, $types); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($this->values as $option => $values) { |
119
|
|
|
$this->resolver->setAllowedValues($option, $values); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (method_exists($this, 'configureOptions')) { |
123
|
|
|
$this->configureOptions($this->resolver); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->resolver; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|