| @@ -1,31 +1,86 @@ | ||
| 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 | 17 | use UserAccessManager\Config\BooleanConfigParameter; |
| 9 | 18 | use UserAccessManager\Config\Config; |
| 19 | +use UserAccessManager\Config\MainConfig; | |
| 10 | 20 | use UserAccessManager\Config\ConfigParameter; |
| 11 | -use UserAccessManager\Config\MainConfig; | |
| 12 | 21 | use UserAccessManager\Config\SelectionConfigParameter; |
| 13 | 22 | use UserAccessManager\Config\StringConfigParameter; |
| 14 | 23 | use UserAccessManager\Wrapper\Php; |
| 15 | 24 | use UserAccessManager\Wrapper\Wordpress; |
| 16 | 25 | |
| 26 | +/** | |
| 27 | + * Class FormHelper | |
| 28 | + * | |
| 29 | + * @package UserAccessManager\Form | |
| 30 | + */ | |
| 17 | 31 | class FormHelper |
| 18 | 32 | { |
| 33 | + /** | |
| 34 | + * @var Php | |
| 35 | + */ | |
| 36 | + private $php; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @var Wordpress | |
| 40 | + */ | |
| 41 | + private $wordpress; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @var MainConfig | |
| 45 | + */ | |
| 46 | + private $config; | |
| 47 | + | |
| 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 | + */ | |
| 19 | 61 | public function __construct( |
| 20 | - private Php $php, | |
| 21 | - private Wordpress $wordpress, | |
| 22 | - private MainConfig $config, | |
| 23 | - private FormFactory $formFactory | |
| 62 | + Php $php, | |
| 63 | + Wordpress $wordpress, | |
| 64 | + MainConfig $config, | |
| 65 | + FormFactory $formFactory | |
| 24 | 66 | ) { |
| 67 | + $this->php = $php; | |
| 68 | + $this->wordpress = $wordpress; | |
| 69 | + $this->config = $config; | |
| 70 | + $this->formFactory = $formFactory; | |
| 25 | 71 | } |
| 26 | 72 | |
| 27 | - private function getObjectText(string $ident, bool $description, $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) | |
| 28 | 83 | { |
| 29 | 84 | $ident .= ($description === true) ? '_DESC' : ''; |
| 30 | 85 | |
| 31 | 86 | if ($objectKey !== null) { |
| @@ -48,23 +103,35 @@ | ||
| 48 | 103 | |
| 49 | 104 | return (defined($ident) === true) ? constant($ident) : $ident; |
| 50 | 105 | } |
| 51 | 106 | |
| 52 | - 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) | |
| 53 | 114 | { |
| 54 | 115 | return $this->getObjectText( |
| 55 | - 'TXT_UAM_' . strtoupper($key) . '_SETTING', | |
| 116 | + 'TXT_UAM_'.strtoupper($key).'_SETTING', | |
| 56 | 117 | $description, |
| 57 | 118 | $key |
| 58 | 119 | ); |
| 59 | 120 | } |
| 60 | 121 | |
| 61 | - public function getParameterText( | |
| 62 | - ConfigParameter $configParameter, | |
| 63 | - bool $description = false, | |
| 64 | - ?string $objectKey = null | |
| 65 | - ): string { | |
| 66 | - $ident = 'TXT_UAM_' . strtoupper($configParameter->getId()); | |
| 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()); | |
| 67 | 134 | |
| 68 | 135 | return $this->getObjectText( |
| 69 | 136 | $ident, |
| 70 | 137 | $description, |
| @@ -72,15 +139,20 @@ | ||
| 72 | 139 | ); |
| 73 | 140 | } |
| 74 | 141 | |
| 75 | 142 | /** |
| 76 | - * @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 | |
| 77 | 152 | */ |
| 78 | - public function createMultipleFromElement( | |
| 79 | - string $value, | |
| 80 | - string $label, | |
| 81 | - ?ConfigParameter $parameter = null | |
| 82 | - ): MultipleFormElementValue { | |
| 153 | + public function createMultipleFromElement($value, $label, ConfigParameter $parameter = null) | |
| 154 | + { | |
| 83 | 155 | $value = $this->formFactory->createMultipleFormElementValue($value, $label); |
| 84 | 156 | |
| 85 | 157 | if ($parameter !== null) { |
| 86 | 158 | $convertedParameter = $this->convertConfigParameter($parameter); |
| @@ -93,19 +165,25 @@ | ||
| 93 | 165 | return $value; |
| 94 | 166 | } |
| 95 | 167 | |
| 96 | 168 | /** |
| 97 | - * @throws Exception | |
| 169 | + * @param SelectionConfigParameter $configParameter | |
| 170 | + * @param null|string $objectKey | |
| 171 | + * @param array $overwrittenValues | |
| 172 | + * | |
| 173 | + * @return mixed | |
| 174 | + * | |
| 175 | + * @throws \Exception | |
| 98 | 176 | */ |
| 99 | 177 | private function convertSelectionParameter( |
| 100 | 178 | SelectionConfigParameter $configParameter, |
| 101 | - ?string $objectKey = null, | |
| 179 | + $objectKey = null, | |
| 102 | 180 | array $overwrittenValues = [] |
| 103 | - ): mixed { | |
| 181 | + ) { | |
| 104 | 182 | $values = []; |
| 105 | 183 | |
| 106 | 184 | foreach ($configParameter->getSelections() as $selection) { |
| 107 | - $optionNameKey = 'TXT_UAM_' . strtoupper($configParameter->getId() . '_' . $selection); | |
| 185 | + $optionNameKey = 'TXT_UAM_'.strtoupper($configParameter->getId().'_'.$selection); | |
| 108 | 186 | $label = (defined($optionNameKey) === true) ? constant($optionNameKey) : $optionNameKey; |
| 109 | 187 | |
| 110 | 188 | if ($overwrittenValues === []) { |
| 111 | 189 | $values[] = $this->formFactory->createValueSetFromElementValue($selection, $label); |
| @@ -126,15 +204,21 @@ | ||
| 126 | 204 | ); |
| 127 | 205 | } |
| 128 | 206 | |
| 129 | 207 | /** |
| 130 | - * @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 | |
| 131 | 215 | */ |
| 132 | 216 | public function convertConfigParameter( |
| 133 | 217 | ConfigParameter $configParameter, |
| 134 | - ?string $objectKey = null, | |
| 218 | + $objectKey = null, | |
| 135 | 219 | array $overwrittenValues = [] |
| 136 | - ): Input|Radio|Select|null { | |
| 220 | + ) { | |
| 137 | 221 | if (($configParameter instanceof StringConfigParameter) === true) { |
| 138 | 222 | return $this->formFactory->createInput( |
| 139 | 223 | $configParameter->getId(), |
| 140 | 224 | $configParameter->getValue(), |
| @@ -152,8 +236,9 @@ | ||
| 152 | 236 | $this->getParameterText($configParameter, false, $objectKey), |
| 153 | 237 | $this->getParameterText($configParameter, true, $objectKey) |
| 154 | 238 | ); |
| 155 | 239 | } elseif (($configParameter instanceof SelectionConfigParameter) === true) { |
| 240 | + /** @var SelectionConfigParameter $configParameter */ | |
| 156 | 241 | return $this->convertSelectionParameter($configParameter, $objectKey, $overwrittenValues); |
| 157 | 242 | } |
| 158 | 243 | |
| 159 | 244 | return null; |
| @@ -159,11 +244,18 @@ | ||
| 159 | 244 | return null; |
| 160 | 245 | } |
| 161 | 246 | |
| 162 | 247 | /** |
| 163 | - * @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 | |
| 164 | 256 | */ |
| 165 | - public function getSettingsForm(array $parameters, ?string $objectKey = null): Form | |
| 257 | + public function getSettingsForm(array $parameters, $objectKey = null) | |
| 166 | 258 | { |
| 167 | 259 | $configParameters = $this->config->getConfigParameters(); |
| 168 | 260 | $form = $this->formFactory->createFrom(); |
| 169 | 261 | |
| @@ -172,9 +264,9 @@ | ||
| 172 | 264 | |
| 173 | 265 | if (is_array($parameter) === true) { |
| 174 | 266 | $overwrittenValues = array_map( |
| 175 | 267 | function ($parameterKey) use ($configParameters) { |
| 176 | - return $configParameters[$parameterKey] ?? null; | |
| 268 | + return isset($configParameters[$parameterKey]) ? $configParameters[$parameterKey] : null; | |
| 177 | 269 | }, |
| 178 | 270 | $parameter |
| 179 | 271 | ); |
| 180 | 272 | $parameter = $key; |
| @@ -198,11 +290,17 @@ | ||
| 198 | 290 | return $form; |
| 199 | 291 | } |
| 200 | 292 | |
| 201 | 293 | /** |
| 202 | - * @throws Exception | |
| 294 | + * Converts config parameters to a form. | |
| 295 | + * | |
| 296 | + * @param Config $config | |
| 297 | + * | |
| 298 | + * @return Form | |
| 299 | + * | |
| 300 | + * @throws \Exception | |
| 203 | 301 | */ |
| 204 | - public function getSettingsFormByConfig(Config $config): Form | |
| 302 | + public function getSettingsFormByConfig(Config $config) | |
| 205 | 303 | { |
| 206 | 304 | $form = $this->formFactory->createFrom(); |
| 207 | 305 | $configParameters = $config->getConfigParameters(); |
| 208 | 306 | |