| Total Complexity | 55 |
| Total Lines | 266 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SelectViewHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SelectViewHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 94 | class SelectViewHelper extends AbstractFormFieldViewHelper |
||
| 95 | { |
||
| 96 | /** |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $tagName = 'select'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var mixed |
||
| 103 | */ |
||
| 104 | protected $selectedValue; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Initialize arguments. |
||
| 108 | */ |
||
| 109 | public function initializeArguments() |
||
| 110 | { |
||
| 111 | parent::initializeArguments(); |
||
| 112 | $this->registerUniversalTagAttributes(); |
||
| 113 | $this->registerTagAttribute('size', 'string', 'Size of input field'); |
||
| 114 | $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads'); |
||
| 115 | $this->registerArgument('options', 'array', 'Associative array with internal IDs as key, and the values are displayed in the select box. Can be combined with or replaced by child f:form.select.* nodes.'); |
||
| 116 | $this->registerArgument('optionsAfterContent', 'boolean', 'If true, places auto-generated option tags after those rendered in the tag content. If false, automatic options come first.', false, false); |
||
| 117 | $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.'); |
||
| 118 | $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.'); |
||
| 119 | $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', false, false); |
||
| 120 | $this->registerArgument('selectAllByDefault', 'boolean', 'If specified options are selected if none was set before.', false, false); |
||
| 121 | $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error'); |
||
| 122 | $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.'); |
||
| 123 | $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.'); |
||
| 124 | $this->registerArgument('multiple', 'boolean', 'If set multiple options may be selected.', false, false); |
||
| 125 | $this->registerArgument('required', 'boolean', 'If set no empty value is allowed.', false, false); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Render the tag. |
||
| 130 | * |
||
| 131 | * @return string rendered tag. |
||
| 132 | */ |
||
| 133 | public function render() |
||
| 134 | { |
||
| 135 | if (isset($this->arguments['required']) && $this->arguments['required']) { |
||
| 136 | $this->tag->addAttribute('required', 'required'); |
||
| 137 | } |
||
| 138 | $name = $this->getName(); |
||
| 139 | if (isset($this->arguments['multiple']) && $this->arguments['multiple']) { |
||
| 140 | $this->tag->addAttribute('multiple', 'multiple'); |
||
| 141 | $name .= '[]'; |
||
| 142 | } |
||
| 143 | $this->tag->addAttribute('name', $name); |
||
| 144 | $options = $this->getOptions(); |
||
| 145 | |||
| 146 | $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer(); |
||
| 147 | |||
| 148 | $this->addAdditionalIdentityPropertiesIfNeeded(); |
||
| 149 | $this->setErrorClassAttribute(); |
||
| 150 | $content = ''; |
||
| 151 | |||
| 152 | // register field name for token generation. |
||
| 153 | $this->registerFieldNameForFormTokenGeneration($name); |
||
| 154 | // in case it is a multi-select, we need to register the field name |
||
| 155 | // as often as there are elements in the box |
||
| 156 | if (isset($this->arguments['multiple']) && $this->arguments['multiple']) { |
||
| 157 | $content .= $this->renderHiddenFieldForEmptyValue(); |
||
| 158 | // Register the field name additional times as required by the total number of |
||
| 159 | // options. Since we already registered it once above, we start the counter at 1 |
||
| 160 | // instead of 0. |
||
| 161 | $optionsCount = count($options); |
||
| 162 | for ($i = 1; $i < $optionsCount; $i++) { |
||
| 163 | $this->registerFieldNameForFormTokenGeneration($name); |
||
| 164 | } |
||
| 165 | // save the parent field name so that any child f:form.select.option |
||
| 166 | // tag will know to call registerFieldNameForFormTokenGeneration |
||
| 167 | // this is the reason why "self::class" is used instead of static::class (no LSB) |
||
| 168 | $viewHelperVariableContainer->addOrUpdate( |
||
| 169 | self::class, |
||
| 170 | 'registerFieldNameForFormTokenGeneration', |
||
| 171 | $name |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | |||
| 175 | $viewHelperVariableContainer->addOrUpdate(self::class, 'selectedValue', $this->getSelectedValue()); |
||
| 176 | $prependContent = $this->renderPrependOptionTag(); |
||
| 177 | $tagContent = $this->renderOptionTags($options); |
||
| 178 | $childContent = $this->renderChildren(); |
||
| 179 | $viewHelperVariableContainer->remove(self::class, 'selectedValue'); |
||
| 180 | $viewHelperVariableContainer->remove(self::class, 'registerFieldNameForFormTokenGeneration'); |
||
| 181 | if (isset($this->arguments['optionsAfterContent']) && $this->arguments['optionsAfterContent']) { |
||
| 182 | $tagContent = $childContent . $tagContent; |
||
| 183 | } else { |
||
| 184 | $tagContent .= $childContent; |
||
| 185 | } |
||
| 186 | $tagContent = $prependContent . $tagContent; |
||
| 187 | |||
| 188 | $this->tag->forceClosingTag(true); |
||
| 189 | $this->tag->setContent($tagContent); |
||
| 190 | $content .= $this->tag->render(); |
||
| 191 | return $content; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Render prepended option tag |
||
| 196 | * |
||
| 197 | * @return string rendered prepended empty option |
||
| 198 | */ |
||
| 199 | protected function renderPrependOptionTag() |
||
| 200 | { |
||
| 201 | $output = ''; |
||
| 202 | if ($this->hasArgument('prependOptionLabel')) { |
||
| 203 | $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : ''; |
||
| 204 | $label = $this->arguments['prependOptionLabel']; |
||
| 205 | $output .= $this->renderOptionTag($value, $label, false) . LF; |
||
| 206 | } |
||
| 207 | return $output; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Render the option tags. |
||
| 212 | * |
||
| 213 | * @param array $options the options for the form. |
||
| 214 | * @return string rendered tags. |
||
| 215 | */ |
||
| 216 | protected function renderOptionTags($options) |
||
| 217 | { |
||
| 218 | $output = ''; |
||
| 219 | foreach ($options as $value => $label) { |
||
| 220 | $isSelected = $this->isSelected($value); |
||
| 221 | $output .= $this->renderOptionTag($value, $label, $isSelected) . LF; |
||
| 222 | } |
||
| 223 | return $output; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Render the option tags. |
||
| 228 | * |
||
| 229 | * @return array an associative array of options, key will be the value of the option tag |
||
| 230 | */ |
||
| 231 | protected function getOptions() |
||
| 232 | { |
||
| 233 | if (!is_array($this->arguments['options']) && !$this->arguments['options'] instanceof \Traversable) { |
||
| 234 | return []; |
||
| 235 | } |
||
| 236 | $options = []; |
||
| 237 | $optionsArgument = $this->arguments['options']; |
||
| 238 | foreach ($optionsArgument as $key => $value) { |
||
| 239 | if (is_object($value) || is_array($value)) { |
||
| 240 | if ($this->hasArgument('optionValueField')) { |
||
| 241 | $key = ObjectAccess::getPropertyPath($value, $this->arguments['optionValueField']); |
||
| 242 | if (is_object($key)) { |
||
| 243 | if (method_exists($key, '__toString')) { |
||
| 244 | $key = (string)$key; |
||
| 245 | } else { |
||
| 246 | throw new Exception('Identifying value for object of class "' . (is_object($value) ? get_class($value) : gettype($value)) . '" was an object.', 1247827428); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) { |
||
|
|
|||
| 250 | // @todo use $this->persistenceManager->isNewObject() once it is implemented |
||
| 251 | $key = $this->persistenceManager->getIdentifierByObject($value); |
||
| 252 | } elseif (is_object($value) && method_exists($value, '__toString')) { |
||
| 253 | $key = (string)$value; |
||
| 254 | } elseif (is_object($value)) { |
||
| 255 | throw new Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696); |
||
| 256 | } |
||
| 257 | if ($this->hasArgument('optionLabelField')) { |
||
| 258 | $value = ObjectAccess::getPropertyPath($value, $this->arguments['optionLabelField']); |
||
| 259 | if (is_object($value)) { |
||
| 260 | if (method_exists($value, '__toString')) { |
||
| 261 | $value = (string)$value; |
||
| 262 | } else { |
||
| 263 | throw new Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.', 1247827553); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } elseif (is_object($value) && method_exists($value, '__toString')) { |
||
| 267 | $value = (string)$value; |
||
| 268 | } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) { |
||
| 269 | // @todo use $this->persistenceManager->isNewObject() once it is implemented |
||
| 270 | $value = $this->persistenceManager->getIdentifierByObject($value); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | $options[$key] = $value; |
||
| 274 | } |
||
| 275 | if ($this->arguments['sortByOptionLabel']) { |
||
| 276 | asort($options, SORT_LOCALE_STRING); |
||
| 277 | } |
||
| 278 | return $options; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Render the option tags. |
||
| 283 | * |
||
| 284 | * @param mixed $value Value to check for |
||
| 285 | * @return bool TRUE if the value should be marked a s selected; FALSE otherwise |
||
| 286 | */ |
||
| 287 | protected function isSelected($value) |
||
| 288 | { |
||
| 289 | $selectedValue = $this->getSelectedValue(); |
||
| 290 | if ($value === $selectedValue || (string)$value === $selectedValue) { |
||
| 291 | return true; |
||
| 292 | } |
||
| 293 | if ($this->hasArgument('multiple')) { |
||
| 294 | if ($selectedValue === null && $this->arguments['selectAllByDefault'] === true) { |
||
| 295 | return true; |
||
| 296 | } |
||
| 297 | if (is_array($selectedValue) && in_array($value, $selectedValue)) { |
||
| 298 | return true; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | return false; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Retrieves the selected value(s) |
||
| 306 | * |
||
| 307 | * @return mixed value string or an array of strings |
||
| 308 | */ |
||
| 309 | protected function getSelectedValue() |
||
| 310 | { |
||
| 311 | $this->setRespectSubmittedDataValue(true); |
||
| 312 | $value = $this->getValueAttribute(); |
||
| 313 | if (!is_array($value) && !$value instanceof \Traversable) { |
||
| 314 | return $this->getOptionValueScalar($value); |
||
| 315 | } |
||
| 316 | $selectedValues = []; |
||
| 317 | foreach ($value as $selectedValueElement) { |
||
| 318 | $selectedValues[] = $this->getOptionValueScalar($selectedValueElement); |
||
| 319 | } |
||
| 320 | return $selectedValues; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the option value for an object |
||
| 325 | * |
||
| 326 | * @param mixed $valueElement |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | protected function getOptionValueScalar($valueElement) |
||
| 330 | { |
||
| 331 | if (is_object($valueElement)) { |
||
| 332 | if ($this->hasArgument('optionValueField')) { |
||
| 333 | return ObjectAccess::getPropertyPath($valueElement, $this->arguments['optionValueField']); |
||
| 334 | } |
||
| 335 | // @todo use $this->persistenceManager->isNewObject() once it is implemented |
||
| 336 | if ($this->persistenceManager->getIdentifierByObject($valueElement) !== null) { |
||
| 337 | return $this->persistenceManager->getIdentifierByObject($valueElement); |
||
| 338 | } |
||
| 339 | return (string)$valueElement; |
||
| 340 | } |
||
| 341 | return $valueElement; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Render one option tag |
||
| 346 | * |
||
| 347 | * @param string $value value attribute of the option tag (will be escaped) |
||
| 348 | * @param string $label content of the option tag (will be escaped) |
||
| 349 | * @param bool $isSelected specifies whether or not to add selected attribute |
||
| 350 | * @return string the rendered option tag |
||
| 351 | */ |
||
| 352 | protected function renderOptionTag($value, $label, $isSelected) |
||
| 360 | } |
||
| 361 | } |
||
| 362 |