PluginProbe
User Access Manager / 2.2.2
User Access Manager v2.2.2
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 +181 -109 2.3.192.2.2 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * FormHelper.php
4 + *
5 + * The FormHelper class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Form;
@@ -4,147 +17,203 @@
4 17
5 18 namespace UserAccessManager\Form;
6 19
7 20 use Exception;
8 -use UserAccessManager\Config\Parameter\BooleanConfigParameter;
21 +use UserAccessManager\Config\BooleanConfigParameter;
9 22 use UserAccessManager\Config\Config;
10 -use UserAccessManager\Config\Parameter\ConfigParameter;
23 +use UserAccessManager\Config\ConfigParameter;
11 24 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;
25 +use UserAccessManager\Config\SelectionConfigParameter;
26 +use UserAccessManager\Config\StringConfigParameter;
19 27 use UserAccessManager\Wrapper\Php;
20 28 use UserAccessManager\Wrapper\Wordpress;
21 29
30 +/**
31 + * Class FormHelper
32 + *
33 + * @package UserAccessManager\Form
34 + */
22 35 class FormHelper
23 36 {
24 - public function __construct(
25 - private Php $php,
26 - private Wordpress $wordpress,
27 - private MainConfig $config,
28 - private FormFactory $formFactory
29 - ) {
30 - }
37 + /**
38 + * @var Php
39 + */
40 + private $php;
31 41
32 - private function resolveTextConstant(string $ident): string
33 - {
34 - return defined($ident) ? constant($ident) : $ident;
35 - }
42 + /**
43 + * @var Wordpress
44 + */
45 + private $wordpress;
36 46
37 - private function getPublicObject(string $objectKey): ?object
38 - {
39 - $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
40 - + $this->wordpress->getTaxonomies(['public' => true], 'objects');
47 + /**
48 + * @var MainConfig
49 + */
50 + private $config;
41 51
42 - return $objects[$objectKey] ?? null;
52 + /**
53 + * @var FormFactory
54 + */
55 + private $formFactory;
56 +
57 + /**
58 + * FormHelper constructor.
59 + * @param Php $php
60 + * @param Wordpress $wordpress
61 + * @param MainConfig $config
62 + * @param FormFactory $formFactory
63 + */
64 + public function __construct(
65 + Php $php,
66 + Wordpress $wordpress,
67 + MainConfig $config,
68 + FormFactory $formFactory
69 + ) {
70 + $this->php = $php;
71 + $this->wordpress = $wordpress;
72 + $this->config = $config;
73 + $this->formFactory = $formFactory;
43 74 }
44 75
45 - private function getObjectText(string $ident, bool $description, ?string $objectKey = null): string
76 + /**
77 + * Returns the right translation string.
78 + * @param string $ident
79 + * @param bool $description
80 + * @param null $objectKey
81 + * @return mixed|string
82 + */
83 + private function getObjectText(string $ident, $description = false, $objectKey = null): string
46 84 {
47 85 $ident .= ($description === true) ? '_DESC' : '';
48 - $object = ($objectKey !== null) ? $this->getPublicObject($objectKey) : null;
49 86
50 - if ($object === null) {
51 - return $this->resolveTextConstant($ident);
52 - }
87 + if ($objectKey !== null) {
88 + $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
89 + + $this->wordpress->getTaxonomies(['public' => true], 'objects');
53 90
54 - $text = $this->resolveTextConstant(str_replace(strtoupper($objectKey), 'OBJECT', $ident));
55 - $placeholderCount = substr_count($text, '%s');
91 + if (isset($objects[$objectKey]) === true) {
92 + $ident = str_replace(strtoupper($objectKey), 'OBJECT', $ident);
93 + $text = (defined($ident) === true) ? constant($ident) : $ident;
94 + $count = substr_count($text, '%s');
56 95
57 - if ($placeholderCount === 0) {
58 - return $text;
96 + if ($count > 0) {
97 + $arguments = $this->php->arrayFill(0, $count, $objects[$objectKey]->labels->name);
98 + $text = vsprintf($text, $arguments);
99 + }
100 +
101 + return $text;
102 + }
59 103 }
60 104
61 - return vsprintf($text, $this->php->arrayFill(0, $placeholderCount, $object->labels->name));
105 + return (defined($ident) === true) ? constant($ident) : $ident;
62 106 }
63 107
64 - public function getText(string $key, bool $description = false): string
108 + /**
109 + * @param string $key
110 + * @param bool $description
111 + * @return string
112 + */
113 + public function getText(string $key, $description = false): string
65 114 {
66 - return $this->getObjectText('TXT_UAM_' . strtoupper($key) . '_SETTING', $description, $key);
115 + return $this->getObjectText(
116 + 'TXT_UAM_' . strtoupper($key) . '_SETTING',
117 + $description,
118 + $key
119 + );
67 120 }
68 121
69 - public function getParameterText(
70 - ConfigParameter $configParameter,
71 - bool $description = false,
72 - ?string $objectKey = null
73 - ): string {
74 - return $this->getObjectText('TXT_UAM_' . strtoupper($configParameter->getId()), $description, $objectKey);
122 + /**
123 + * Returns the label for the parameter.
124 + * @param ConfigParameter $configParameter
125 + * @param bool $description
126 + * @param string $objectKey
127 + * @return string
128 + */
129 + public function getParameterText(ConfigParameter $configParameter, $description = false, $objectKey = null): string
130 + {
131 + $ident = 'TXT_UAM_' . strtoupper($configParameter->getId());
132 +
133 + return $this->getObjectText(
134 + $ident,
135 + $description,
136 + $objectKey
137 + );
75 138 }
76 139
77 140 /**
141 + * Creates a multiple form element.
142 + * @param string $value
143 + * @param string $label
144 + * @param ConfigParameter|null $parameter
145 + * @return MultipleFormElementValue
78 146 * @throws Exception
79 147 */
80 - public function createMultipleFormElementValue(
148 + public function createMultipleFromElement(
81 149 string $value,
82 150 string $label,
83 151 ?ConfigParameter $parameter = null
84 152 ): MultipleFormElementValue {
85 - $elementValue = $this->formFactory->createMultipleFormElementValue($value, $label);
86 - $subElement = ($parameter !== null) ? $this->convertConfigParameter($parameter) : null;
153 + $value = $this->formFactory->createMultipleFormElementValue($value, $label);
87 154
88 - if ($subElement !== null) {
89 - $elementValue->setSubElement($subElement);
155 + if ($parameter !== null) {
156 + $convertedParameter = $this->convertConfigParameter($parameter);
157 +
158 + if ($convertedParameter !== null) {
159 + $value->setSubElement($convertedParameter);
160 + }
90 161 }
91 162
92 - return $elementValue;
163 + return $value;
93 164 }
94 165
95 166 /**
167 + * @param SelectionConfigParameter $configParameter
168 + * @param null|string $objectKey
169 + * @param array $overwrittenValues
170 + * @return mixed
96 171 * @throws Exception
97 172 */
98 173 private function convertSelectionParameter(
99 174 SelectionConfigParameter $configParameter,
100 - ?string $objectKey = null,
175 + $objectKey = null,
101 176 array $overwrittenValues = []
102 - ): Select|Radio {
177 + ) {
103 178 $values = [];
104 179
105 180 foreach ($configParameter->getSelections() as $selection) {
106 - $optionLabel = $this->resolveTextConstant(
107 - 'TXT_UAM_' . strtoupper($configParameter->getId() . '_' . $selection)
108 - );
181 + $optionNameKey = 'TXT_UAM_' . strtoupper($configParameter->getId() . '_' . $selection);
182 + $label = (defined($optionNameKey) === true) ? constant($optionNameKey) : $optionNameKey;
109 183
110 - $overwrittenValue = $overwrittenValues[$selection] ?? null;
111 - $values[] = ($overwrittenValues === [])
112 - ? $this->formFactory->createValueSetFormElementValue($selection, $optionLabel)
113 - : $this->createMultipleFormElementValue($selection, $optionLabel, $overwrittenValue);
184 + if ($overwrittenValues === []) {
185 + $values[] = $this->formFactory->createValueSetFromElementValue($selection, $label);
186 + } else {
187 + $parameter = (isset($overwrittenValues[$selection]) === true) ? $overwrittenValues[$selection] : null;
188 + $values[] = $this->createMultipleFromElement($selection, $label, $parameter);
189 + }
114 190 }
115 191
116 - $label = $this->getParameterText($configParameter, false, $objectKey);
117 - $description = $this->getParameterText($configParameter, true, $objectKey);
192 + $objectMethod = $overwrittenValues === [] ? 'createSelect' : 'createRadio';
118 193
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(
194 + return $this->formFactory->{$objectMethod}(
130 195 $configParameter->getId(),
131 196 $values,
132 197 $configParameter->getValue(),
133 - $label,
134 - $description
198 + $this->getParameterText($configParameter, false, $objectKey),
199 + $this->getParameterText($configParameter, true, $objectKey)
135 200 );
136 201 }
137 202
138 203 /**
204 + * @param ConfigParameter $configParameter
205 + * @param null|string $objectKey
206 + * @param array $overwrittenValues
207 + * @return null|Input|Radio|Select
139 208 * @throws Exception
140 209 */
141 210 public function convertConfigParameter(
142 211 ConfigParameter $configParameter,
143 - ?string $objectKey = null,
212 + $objectKey = null,
144 213 array $overwrittenValues = []
145 - ): Input|Radio|Select|null {
146 - if ($configParameter instanceof StringConfigParameter) {
214 + ) {
215 + if (($configParameter instanceof StringConfigParameter) === true) {
147 216 return $this->formFactory->createInput(
148 217 $configParameter->getId(),
149 218 $configParameter->getValue(),
150 219 $this->getParameterText($configParameter, false, $objectKey),
@@ -149,24 +218,21 @@
149 218 $configParameter->getValue(),
150 219 $this->getParameterText($configParameter, false, $objectKey),
151 220 $this->getParameterText($configParameter, true, $objectKey)
152 221 );
153 - }
222 + } elseif (($configParameter instanceof BooleanConfigParameter) === true) {
223 + $yes = $this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES);
224 + $no = $this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO);
154 225
155 - if ($configParameter instanceof BooleanConfigParameter) {
156 226 return $this->formFactory->createRadio(
157 227 $configParameter->getId(),
158 - [
159 - $this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES),
160 - $this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO)
161 - ],
228 + [$yes, $no],
162 229 $configParameter->getValue(),
163 230 $this->getParameterText($configParameter, false, $objectKey),
164 231 $this->getParameterText($configParameter, true, $objectKey)
165 232 );
166 - }
167 -
168 - if ($configParameter instanceof SelectionConfigParameter) {
233 + } elseif (($configParameter instanceof SelectionConfigParameter) === true) {
234 + /** @var SelectionConfigParameter $configParameter */
169 235 return $this->convertSelectionParameter($configParameter, $objectKey, $overwrittenValues);
170 236 }
171 237
172 238 return null;
@@ -172,30 +238,18 @@
172 238 return null;
173 239 }
174 240
175 241 /**
242 + * Returns the settings form for the given config parameters.
243 + * @param array $parameters
244 + * @param string|null $objectKey
245 + * @return Form
176 246 * @throws Exception
177 247 */
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 - public function getSettingsForm(array $parameters, ?string $objectKey = null): Form
248 + public function getSettingsForm(array $parameters, $objectKey = null): Form
195 249 {
196 250 $configParameters = $this->config->getConfigParameters();
197 - $form = $this->formFactory->createForm();
251 + $form = $this->formFactory->createFrom();
198 252
199 253 foreach ($parameters as $key => $parameter) {
200 254 $overwrittenValues = [];
201 255
@@ -200,9 +254,11 @@
200 254 $overwrittenValues = [];
201 255
202 256 if (is_array($parameter) === true) {
203 257 $overwrittenValues = array_map(
204 - fn($parameterKey) => $configParameters[$parameterKey] ?? null,
258 + function ($parameterKey) use ($configParameters) {
259 + return isset($configParameters[$parameterKey]) ? $configParameters[$parameterKey] : null;
260 + },
205 261 $parameter
206 262 );
207 263 $parameter = $key;
208 264 }
@@ -207,10 +263,18 @@
207 263 $parameter = $key;
208 264 }
209 265
210 266 if (is_string($parameter) === true && isset($configParameters[$parameter]) === true) {
211 - $this->addConvertedParameter($form, $configParameters[$parameter], $objectKey, $overwrittenValues);
212 - } elseif ($parameter instanceof FormElement) {
267 + $formElement = $this->convertConfigParameter(
268 + $configParameters[$parameter],
269 + $objectKey,
270 + $overwrittenValues
271 + );
272 +
273 + if ($formElement !== null) {
274 + $form->addElement($formElement);
275 + }
276 + } elseif (($parameter instanceof FormElement) === true) {
213 277 $form->addElement($parameter);
214 278 }
215 279 }
216 280
@@ -217,16 +281,24 @@
217 281 return $form;
218 282 }
219 283
220 284 /**
285 + * Converts config parameters to a form.
286 + * @param Config $config
287 + * @return Form
221 288 * @throws Exception
222 289 */
223 290 public function getSettingsFormByConfig(Config $config): Form
224 291 {
225 - $form = $this->formFactory->createForm();
292 + $form = $this->formFactory->createFrom();
293 + $configParameters = $config->getConfigParameters();
226 294
227 - foreach ($config->getConfigParameters() as $configParameter) {
228 - $this->addConvertedParameter($form, $configParameter);
295 + foreach ($configParameters as $configParameter) {
296 + $formElement = $this->convertConfigParameter($configParameter);
297 +
298 + if ($formElement !== null) {
299 + $form->addElement($formElement);
300 + }
229 301 }
230 302
231 303 return $form;
232 304 }