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