1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
7
|
|
|
use kalanis\kw_forms\Interfaces\IWrapper; |
8
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
9
|
|
|
use kalanis\kw_rules\Interfaces; |
10
|
|
|
use kalanis\kw_rules\Rules; |
11
|
|
|
use kalanis\kw_rules\TRules; |
12
|
|
|
use kalanis\kw_templates\Interfaces\IHtmlElement; |
13
|
|
|
use kalanis\kw_templates\HtmlElement\THtmlElement; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AControl |
18
|
|
|
* @package kalanis\kw_forms\Controls |
19
|
|
|
* Abstraction of control entry - which will be rendered |
20
|
|
|
* Implementing IValidate because kw_rules are really independent |
21
|
|
|
*/ |
22
|
|
|
abstract class AControl implements Interfaces\IValidate, IHtmlElement, IWrapper |
23
|
|
|
{ |
24
|
|
|
use THtmlElement; |
25
|
|
|
use TKey; |
26
|
|
|
use TLabel; |
27
|
|
|
use TValue; |
28
|
|
|
use TRules; |
29
|
|
|
use TWrappers; |
30
|
|
|
|
31
|
|
|
/** @var string|int|float|bool|null */ |
32
|
|
|
protected $originalValue = null; |
33
|
|
|
// sprintf: 1 value, 2 attributes, 3 children |
34
|
|
|
protected string $templateInput = ''; |
35
|
|
|
protected static bool $escapeOutput = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string|int|bool|null $can |
39
|
|
|
*/ |
40
|
1 |
|
public static function escapeOutput($can = null): void |
41
|
|
|
{ |
42
|
1 |
|
static::$escapeOutput = !empty($can); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
21 |
|
protected function whichRulesFactory(): Interfaces\IRuleFactory |
46
|
|
|
{ |
47
|
21 |
|
return new Rules\Factory(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $key |
52
|
|
|
* @param string|int|float|bool|null $originalValue |
53
|
|
|
* @param string $label |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
78 |
|
public function setEntry(string $key, $originalValue = null, string $label = ''): self |
57
|
|
|
{ |
58
|
78 |
|
$this->setKey($key); |
59
|
78 |
|
$this->originalValue = $originalValue; |
60
|
78 |
|
$this->setLabel($label); |
61
|
78 |
|
$this->template = $this->fillTemplate(); |
62
|
78 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Because filling main template throws an error |
67
|
|
|
* 1 label, 2 input, 3 errors |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
72 |
|
protected function fillTemplate(): string |
71
|
|
|
{ |
72
|
72 |
|
return '%1$s %2$s %3$s'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @throws RenderException |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
14 |
|
public function render(): string |
80
|
|
|
{ |
81
|
14 |
|
return sprintf($this->template, $this->renderLabel(), $this->renderInput(), $this->renderErrors([])); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Render label on form control |
86
|
|
|
* @param string|string[] $attributes |
87
|
|
|
* @throws RenderException |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
15 |
|
public function renderLabel($attributes = []): string |
91
|
|
|
{ |
92
|
15 |
|
if ($this->label) { |
93
|
13 |
|
return $this->wrapIt(sprintf($this->templateLabel, $this->getAttribute('id'), $this->escaped(strval($this->getLabel())), $this->renderAttributes($attributes)), $this->wrappersLabel); |
94
|
|
|
} |
95
|
4 |
|
return ''; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return input entry in HTML |
100
|
|
|
* @param string|string[]|array|null $attributes |
101
|
|
|
* @throws RenderException |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
25 |
|
public function renderInput($attributes = null): string |
105
|
|
|
{ |
106
|
25 |
|
$this->addAttributes($attributes); |
107
|
25 |
|
if (!empty($this->value) && ($this->value != $this->originalValue)) { |
108
|
11 |
|
$value = $this->value; |
109
|
|
|
} else { |
110
|
25 |
|
$value = $this->originalValue; |
111
|
|
|
} |
112
|
25 |
|
$this->setAttribute('name', $this->getKey()); |
113
|
25 |
|
return $this->wrapIt(sprintf($this->templateInput, $this->escaped(strval($value)), $this->renderAttributes(), $this->renderChildren()), $this->wrappersInput); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return errors over entry which happened |
118
|
|
|
* @param array<RuleException> $errors |
119
|
|
|
* @throws RenderException |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
14 |
|
public function renderErrors(array $errors): string |
123
|
|
|
{ |
124
|
14 |
|
$return = ''; |
125
|
14 |
|
foreach ($errors as $error) { |
126
|
3 |
|
$return .= $this->wrapIt(sprintf($this->templateError, $this->escaped($error->getMessage())), $this->wrappersError); |
127
|
|
|
} |
128
|
14 |
|
return empty($return) ? '' : $this->wrapIt($return, $this->wrappersErrors); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
public function inherit(IHtmlElement $child): IHtmlElement |
132
|
|
|
{ |
133
|
1 |
|
$child->addAttributes($this->getAttributes()); |
134
|
1 |
|
$child->setChildren($this->getChildren()); |
135
|
1 |
|
if ($child instanceof IWrapper) { |
136
|
1 |
|
if (!empty($this->wrappersChild)) { |
137
|
1 |
|
$child->addWrapper($this->wrappersChild); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
if (!empty($this->wrappersErrors)) { |
141
|
1 |
|
$child->addWrapperErrors($this->wrappersErrors); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
if (!empty($this->wrappersError)) { |
145
|
1 |
|
$child->addWrapperError($this->wrappersError); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
if (!empty($this->wrappersInput)) { |
149
|
1 |
|
$child->addWrapperInput($this->wrappersInput); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
if (!empty($this->wrappersLabel)) { |
153
|
1 |
|
$child->addWrapperLabel($this->wrappersLabel); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
if (!empty($this->templateError)) { |
157
|
1 |
|
$child->setTemplateError($this->templateError); |
158
|
|
|
} |
159
|
|
|
} |
160
|
1 |
|
return $child; |
161
|
|
|
} |
162
|
|
|
|
163
|
32 |
|
protected function escaped(string $content): string |
164
|
|
|
{ |
165
|
32 |
|
return static::$escapeOutput ? htmlspecialchars($content, ENT_QUOTES | ENT_HTML5, 'UTF-8', false) : $content ; |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
public function count(): int |
169
|
|
|
{ |
170
|
1 |
|
return count($this->children); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|