| @@ -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; |
| @@ -13,19 +26,62 @@ | ||
| 13 | 26 | use UserAccessManager\Config\StringConfigParameter; |
| 14 | 27 | use UserAccessManager\Wrapper\Php; |
| 15 | 28 | use UserAccessManager\Wrapper\Wordpress; |
| 16 | 29 | |
| 30 | +/** | |
| 31 | + * Class FormHelper | |
| 32 | + * | |
| 33 | + * @package UserAccessManager\Form | |
| 34 | + */ | |
| 17 | 35 | class FormHelper |
| 18 | 36 | { |
| 37 | + /** | |
| 38 | + * @var Php | |
| 39 | + */ | |
| 40 | + private $php; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * @var Wordpress | |
| 44 | + */ | |
| 45 | + private $wordpress; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * @var MainConfig | |
| 49 | + */ | |
| 50 | + private $config; | |
| 51 | + | |
| 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 | + */ | |
| 19 | 64 | public function __construct( |
| 20 | - private Php $php, | |
| 21 | - private Wordpress $wordpress, | |
| 22 | - private MainConfig $config, | |
| 23 | - private FormFactory $formFactory | |
| 65 | + Php $php, | |
| 66 | + Wordpress $wordpress, | |
| 67 | + MainConfig $config, | |
| 68 | + FormFactory $formFactory | |
| 24 | 69 | ) { |
| 70 | + $this->php = $php; | |
| 71 | + $this->wordpress = $wordpress; | |
| 72 | + $this->config = $config; | |
| 73 | + $this->formFactory = $formFactory; | |
| 25 | 74 | } |
| 26 | 75 | |
| 27 | - private function getObjectText(string $ident, bool $description, $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 | |
| 28 | 84 | { |
| 29 | 85 | $ident .= ($description === true) ? '_DESC' : ''; |
| 30 | 86 | |
| 31 | 87 | if ($objectKey !== null) { |
| @@ -48,9 +104,14 @@ | ||
| 48 | 104 | |
| 49 | 105 | return (defined($ident) === true) ? constant($ident) : $ident; |
| 50 | 106 | } |
| 51 | 107 | |
| 52 | - 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 | |
| 53 | 114 | { |
| 54 | 115 | return $this->getObjectText( |
| 55 | 116 | 'TXT_UAM_' . strtoupper($key) . '_SETTING', |
| 56 | 117 | $description, |
| @@ -57,13 +118,17 @@ | ||
| 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 { | |
| 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 | + { | |
| 66 | 131 | $ident = 'TXT_UAM_' . strtoupper($configParameter->getId()); |
| 67 | 132 | |
| 68 | 133 | return $this->getObjectText( |
| 69 | 134 | $ident, |
| @@ -72,8 +137,13 @@ | ||
| 72 | 137 | ); |
| 73 | 138 | } |
| 74 | 139 | |
| 75 | 140 | /** |
| 141 | + * Creates a multiple form element. | |
| 142 | + * @param string $value | |
| 143 | + * @param string $label | |
| 144 | + * @param ConfigParameter|null $parameter | |
| 145 | + * @return MultipleFormElementValue | |
| 76 | 146 | * @throws Exception |
| 77 | 147 | */ |
| 78 | 148 | public function createMultipleFromElement( |
| 79 | 149 | string $value, |
| @@ -93,15 +163,19 @@ | ||
| 93 | 163 | return $value; |
| 94 | 164 | } |
| 95 | 165 | |
| 96 | 166 | /** |
| 167 | + * @param SelectionConfigParameter $configParameter | |
| 168 | + * @param null|string $objectKey | |
| 169 | + * @param array $overwrittenValues | |
| 170 | + * @return mixed | |
| 97 | 171 | * @throws Exception |
| 98 | 172 | */ |
| 99 | 173 | private function convertSelectionParameter( |
| 100 | 174 | SelectionConfigParameter $configParameter, |
| 101 | - ?string $objectKey = null, | |
| 175 | + $objectKey = null, | |
| 102 | 176 | array $overwrittenValues = [] |
| 103 | - ): mixed { | |
| 177 | + ) { | |
| 104 | 178 | $values = []; |
| 105 | 179 | |
| 106 | 180 | foreach ($configParameter->getSelections() as $selection) { |
| 107 | 181 | $optionNameKey = 'TXT_UAM_' . strtoupper($configParameter->getId() . '_' . $selection); |
| @@ -126,15 +200,19 @@ | ||
| 126 | 200 | ); |
| 127 | 201 | } |
| 128 | 202 | |
| 129 | 203 | /** |
| 204 | + * @param ConfigParameter $configParameter | |
| 205 | + * @param null|string $objectKey | |
| 206 | + * @param array $overwrittenValues | |
| 207 | + * @return null|Input|Radio|Select | |
| 130 | 208 | * @throws Exception |
| 131 | 209 | */ |
| 132 | 210 | public function convertConfigParameter( |
| 133 | 211 | ConfigParameter $configParameter, |
| 134 | - ?string $objectKey = null, | |
| 212 | + $objectKey = null, | |
| 135 | 213 | array $overwrittenValues = [] |
| 136 | - ): Input|Radio|Select|null { | |
| 214 | + ) { | |
| 137 | 215 | if (($configParameter instanceof StringConfigParameter) === true) { |
| 138 | 216 | return $this->formFactory->createInput( |
| 139 | 217 | $configParameter->getId(), |
| 140 | 218 | $configParameter->getValue(), |
| @@ -152,8 +230,9 @@ | ||
| 152 | 230 | $this->getParameterText($configParameter, false, $objectKey), |
| 153 | 231 | $this->getParameterText($configParameter, true, $objectKey) |
| 154 | 232 | ); |
| 155 | 233 | } elseif (($configParameter instanceof SelectionConfigParameter) === true) { |
| 234 | + /** @var SelectionConfigParameter $configParameter */ | |
| 156 | 235 | return $this->convertSelectionParameter($configParameter, $objectKey, $overwrittenValues); |
| 157 | 236 | } |
| 158 | 237 | |
| 159 | 238 | return null; |
| @@ -159,11 +238,15 @@ | ||
| 159 | 238 | return null; |
| 160 | 239 | } |
| 161 | 240 | |
| 162 | 241 | /** |
| 242 | + * Returns the settings form for the given config parameters. | |
| 243 | + * @param array $parameters | |
| 244 | + * @param string|null $objectKey | |
| 245 | + * @return Form | |
| 163 | 246 | * @throws Exception |
| 164 | 247 | */ |
| 165 | - public function getSettingsForm(array $parameters, ?string $objectKey = null): Form | |
| 248 | + public function getSettingsForm(array $parameters, $objectKey = null): Form | |
| 166 | 249 | { |
| 167 | 250 | $configParameters = $this->config->getConfigParameters(); |
| 168 | 251 | $form = $this->formFactory->createFrom(); |
| 169 | 252 | |
| @@ -172,9 +255,9 @@ | ||
| 172 | 255 | |
| 173 | 256 | if (is_array($parameter) === true) { |
| 174 | 257 | $overwrittenValues = array_map( |
| 175 | 258 | function ($parameterKey) use ($configParameters) { |
| 176 | - return $configParameters[$parameterKey] ?? null; | |
| 259 | + return isset($configParameters[$parameterKey]) ? $configParameters[$parameterKey] : null; | |
| 177 | 260 | }, |
| 178 | 261 | $parameter |
| 179 | 262 | ); |
| 180 | 263 | $parameter = $key; |
| @@ -198,8 +281,11 @@ | ||
| 198 | 281 | return $form; |
| 199 | 282 | } |
| 200 | 283 | |
| 201 | 284 | /** |
| 285 | + * Converts config parameters to a form. | |
| 286 | + * @param Config $config | |
| 287 | + * @return Form | |
| 202 | 288 | * @throws Exception |
| 203 | 289 | */ |
| 204 | 290 | public function getSettingsFormByConfig(Config $config): Form |
| 205 | 291 | { |