PluginProbe
User Access Manager / 2.3.13
User Access Manager v2.3.13
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Form/FormHelper.php +82 -96 trunk2.3.13 View file →
@@ -4,19 +4,14 @@
4 4
5 5 namespace UserAccessManager\Form;
6 6
7 7 use Exception;
8 -use UserAccessManager\Config\Parameter\BooleanConfigParameter;
8 +use UserAccessManager\Config\BooleanConfigParameter;
9 9 use UserAccessManager\Config\Config;
10 -use UserAccessManager\Config\Parameter\ConfigParameter;
10 +use UserAccessManager\Config\ConfigParameter;
11 11 use UserAccessManager\Config\MainConfig;
12 -use UserAccessManager\Config\Parameter\SelectionConfigParameter;
13 -use UserAccessManager\Config\Parameter\StringConfigParameter;
14 -use UserAccessManager\Form\Element\FormElement;
15 -use UserAccessManager\Form\Element\Input;
16 -use UserAccessManager\Form\Element\MultipleFormElementValue;
17 -use UserAccessManager\Form\Element\Radio;
18 -use UserAccessManager\Form\Element\Select;
12 +use UserAccessManager\Config\SelectionConfigParameter;
13 +use UserAccessManager\Config\StringConfigParameter;
19 14 use UserAccessManager\Wrapper\Php;
20 15 use UserAccessManager\Wrapper\Wordpress;
21 16
22 17 class FormHelper
@@ -28,43 +23,40 @@
28 23 private FormFactory $formFactory
29 24 ) {
30 25 }
31 26
32 - private function resolveTextConstant(string $ident): string
27 + private function getObjectText(string $ident, bool $description = false, $objectKey = null): string
33 28 {
34 - return defined($ident) ? constant($ident) : $ident;
35 - }
29 + $ident .= ($description === true) ? '_DESC' : '';
36 30
37 - private function getPublicObject(string $objectKey): ?object
38 - {
39 - $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
40 - + $this->wordpress->getTaxonomies(['public' => true], 'objects');
31 + if ($objectKey !== null) {
32 + $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
33 + + $this->wordpress->getTaxonomies(['public' => true], 'objects');
41 34
42 - return $objects[$objectKey] ?? null;
43 - }
35 + if (isset($objects[$objectKey]) === true) {
36 + $ident = str_replace(strtoupper($objectKey), 'OBJECT', $ident);
37 + $text = (defined($ident) === true) ? constant($ident) : $ident;
38 + $count = substr_count($text, '%s');
44 39
45 - private function getObjectText(string $ident, bool $description, ?string $objectKey = null): string
46 - {
47 - $ident .= ($description === true) ? '_DESC' : '';
48 - $object = ($objectKey !== null) ? $this->getPublicObject($objectKey) : null;
40 + if ($count > 0) {
41 + $arguments = $this->php->arrayFill(0, $count, $objects[$objectKey]->labels->name);
42 + $text = vsprintf($text, $arguments);
43 + }
49 44
50 - if ($object === null) {
51 - return $this->resolveTextConstant($ident);
45 + return $text;
46 + }
52 47 }
53 48
54 - $text = $this->resolveTextConstant(str_replace(strtoupper($objectKey), 'OBJECT', $ident));
55 - $placeholderCount = substr_count($text, '%s');
56 -
57 - if ($placeholderCount === 0) {
58 - return $text;
59 - }
60 -
61 - return vsprintf($text, $this->php->arrayFill(0, $placeholderCount, $object->labels->name));
49 + return (defined($ident) === true) ? constant($ident) : $ident;
62 50 }
63 51
64 52 public function getText(string $key, bool $description = false): string
65 53 {
66 - return $this->getObjectText('TXT_UAM_' . strtoupper($key) . '_SETTING', $description, $key);
54 + return $this->getObjectText(
55 + 'TXT_UAM_' . strtoupper($key) . '_SETTING',
56 + $description,
57 + $key
58 + );
67 59 }
68 60
69 61 public function getParameterText(
70 62 ConfigParameter $configParameter,
@@ -70,27 +62,36 @@
70 62 ConfigParameter $configParameter,
71 63 bool $description = false,
72 64 ?string $objectKey = null
73 65 ): string {
74 - return $this->getObjectText('TXT_UAM_' . strtoupper($configParameter->getId()), $description, $objectKey);
66 + $ident = 'TXT_UAM_' . strtoupper($configParameter->getId());
67 +
68 + return $this->getObjectText(
69 + $ident,
70 + $description,
71 + $objectKey
72 + );
75 73 }
76 74
77 75 /**
78 76 * @throws Exception
79 77 */
80 - public function createMultipleFormElementValue(
78 + public function createMultipleFromElement(
81 79 string $value,
82 80 string $label,
83 81 ?ConfigParameter $parameter = null
84 82 ): MultipleFormElementValue {
85 - $elementValue = $this->formFactory->createMultipleFormElementValue($value, $label);
86 - $subElement = ($parameter !== null) ? $this->convertConfigParameter($parameter) : null;
83 + $value = $this->formFactory->createMultipleFormElementValue($value, $label);
87 84
88 - if ($subElement !== null) {
89 - $elementValue->setSubElement($subElement);
85 + if ($parameter !== null) {
86 + $convertedParameter = $this->convertConfigParameter($parameter);
87 +
88 + if ($convertedParameter !== null) {
89 + $value->setSubElement($convertedParameter);
90 + }
90 91 }
91 92
92 - return $elementValue;
93 + return $value;
93 94 }
94 95
95 96 /**
96 97 * @throws Exception
@@ -98,41 +99,31 @@
98 99 private function convertSelectionParameter(
99 100 SelectionConfigParameter $configParameter,
100 101 ?string $objectKey = null,
101 102 array $overwrittenValues = []
102 - ): Select|Radio {
103 + ): mixed {
103 104 $values = [];
104 105
105 106 foreach ($configParameter->getSelections() as $selection) {
106 - $optionLabel = $this->resolveTextConstant(
107 - 'TXT_UAM_' . strtoupper($configParameter->getId() . '_' . $selection)
108 - );
107 + $optionNameKey = 'TXT_UAM_' . strtoupper($configParameter->getId() . '_' . $selection);
108 + $label = (defined($optionNameKey) === true) ? constant($optionNameKey) : $optionNameKey;
109 109
110 - $overwrittenValue = $overwrittenValues[$selection] ?? null;
111 - $values[] = ($overwrittenValues === [])
112 - ? $this->formFactory->createValueSetFormElementValue($selection, $optionLabel)
113 - : $this->createMultipleFormElementValue($selection, $optionLabel, $overwrittenValue);
110 + if ($overwrittenValues === []) {
111 + $values[] = $this->formFactory->createValueSetFromElementValue($selection, $label);
112 + } else {
113 + $parameter = (isset($overwrittenValues[$selection]) === true) ? $overwrittenValues[$selection] : null;
114 + $values[] = $this->createMultipleFromElement($selection, $label, $parameter);
115 + }
114 116 }
115 117
116 - $label = $this->getParameterText($configParameter, false, $objectKey);
117 - $description = $this->getParameterText($configParameter, true, $objectKey);
118 + $objectMethod = $overwrittenValues === [] ? 'createSelect' : 'createRadio';
118 119
119 - if ($overwrittenValues === []) {
120 - return $this->formFactory->createSelect(
121 - $configParameter->getId(),
122 - $values,
123 - $configParameter->getValue(),
124 - $label,
125 - $description
126 - );
127 - }
128 -
129 - return $this->formFactory->createRadio(
120 + return $this->formFactory->{$objectMethod}(
130 121 $configParameter->getId(),
131 122 $values,
132 123 $configParameter->getValue(),
133 - $label,
134 - $description
124 + $this->getParameterText($configParameter, false, $objectKey),
125 + $this->getParameterText($configParameter, true, $objectKey)
135 126 );
136 127 }
137 128
138 129 /**
@@ -142,9 +133,9 @@
142 133 ConfigParameter $configParameter,
143 134 ?string $objectKey = null,
144 135 array $overwrittenValues = []
145 136 ): Input|Radio|Select|null {
146 - if ($configParameter instanceof StringConfigParameter) {
137 + if (($configParameter instanceof StringConfigParameter) === true) {
147 138 return $this->formFactory->createInput(
148 139 $configParameter->getId(),
149 140 $configParameter->getValue(),
150 141 $this->getParameterText($configParameter, false, $objectKey),
@@ -149,24 +140,20 @@
149 140 $configParameter->getValue(),
150 141 $this->getParameterText($configParameter, false, $objectKey),
151 142 $this->getParameterText($configParameter, true, $objectKey)
152 143 );
153 - }
144 + } elseif (($configParameter instanceof BooleanConfigParameter) === true) {
145 + $yes = $this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES);
146 + $no = $this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO);
154 147
155 - if ($configParameter instanceof BooleanConfigParameter) {
156 148 return $this->formFactory->createRadio(
157 149 $configParameter->getId(),
158 - [
159 - $this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES),
160 - $this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO)
161 - ],
150 + [$yes, $no],
162 151 $configParameter->getValue(),
163 152 $this->getParameterText($configParameter, false, $objectKey),
164 153 $this->getParameterText($configParameter, true, $objectKey)
165 154 );
166 - }
167 -
168 - if ($configParameter instanceof SelectionConfigParameter) {
155 + } elseif (($configParameter instanceof SelectionConfigParameter) === true) {
169 156 return $this->convertSelectionParameter($configParameter, $objectKey, $overwrittenValues);
170 157 }
171 158
172 159 return null;
@@ -174,28 +161,12 @@
174 161
175 162 /**
176 163 * @throws Exception
177 164 */
178 - private function addConvertedParameter(
179 - Form $form,
180 - ConfigParameter $configParameter,
181 - ?string $objectKey = null,
182 - array $overwrittenValues = []
183 - ): void {
184 - $formElement = $this->convertConfigParameter($configParameter, $objectKey, $overwrittenValues);
185 -
186 - if ($formElement !== null) {
187 - $form->addElement($formElement);
188 - }
189 - }
190 -
191 - /**
192 - * @throws Exception
193 - */
194 165 public function getSettingsForm(array $parameters, ?string $objectKey = null): Form
195 166 {
196 167 $configParameters = $this->config->getConfigParameters();
197 - $form = $this->formFactory->createForm();
168 + $form = $this->formFactory->createFrom();
198 169
199 170 foreach ($parameters as $key => $parameter) {
200 171 $overwrittenValues = [];
201 172
@@ -200,9 +171,11 @@
200 171 $overwrittenValues = [];
201 172
202 173 if (is_array($parameter) === true) {
203 174 $overwrittenValues = array_map(
204 - fn($parameterKey) => $configParameters[$parameterKey] ?? null,
175 + function ($parameterKey) use ($configParameters) {
176 + return $configParameters[$parameterKey] ?? null;
177 + },
205 178 $parameter
206 179 );
207 180 $parameter = $key;
208 181 }
@@ -207,10 +180,18 @@
207 180 $parameter = $key;
208 181 }
209 182
210 183 if (is_string($parameter) === true && isset($configParameters[$parameter]) === true) {
211 - $this->addConvertedParameter($form, $configParameters[$parameter], $objectKey, $overwrittenValues);
212 - } elseif ($parameter instanceof FormElement) {
184 + $formElement = $this->convertConfigParameter(
185 + $configParameters[$parameter],
186 + $objectKey,
187 + $overwrittenValues
188 + );
189 +
190 + if ($formElement !== null) {
191 + $form->addElement($formElement);
192 + }
193 + } elseif (($parameter instanceof FormElement) === true) {
213 194 $form->addElement($parameter);
214 195 }
215 196 }
216 197
@@ -221,12 +202,17 @@
221 202 * @throws Exception
222 203 */
223 204 public function getSettingsFormByConfig(Config $config): Form
224 205 {
225 - $form = $this->formFactory->createForm();
206 + $form = $this->formFactory->createFrom();
207 + $configParameters = $config->getConfigParameters();
226 208
227 - foreach ($config->getConfigParameters() as $configParameter) {
228 - $this->addConvertedParameter($form, $configParameter);
209 + foreach ($configParameters as $configParameter) {
210 + $formElement = $this->convertConfigParameter($configParameter);
211 +
212 + if ($formElement !== null) {
213 + $form->addElement($formElement);
214 + }
229 215 }
230 216
231 217 return $form;
232 218 }