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