| Total Complexity | 144 |
| Total Lines | 773 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Container extends Element implements |
||
| 32 | DisableElementsCapableInterface, |
||
| 33 | FormParentInterface, |
||
| 34 | \IteratorAggregate, |
||
| 35 | \Countable |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Available/Loaded forms or specification. |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $forms = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Active formulars keys. |
||
| 45 | * |
||
| 46 | * Formulars which key is herein are included in the iterator. |
||
| 47 | * @see getIterator() |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $activeForms = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The form element manager. |
||
| 54 | * @var \Zend\Form\FormElementManager\FormElementManagerV3Polyfill |
||
| 55 | */ |
||
| 56 | protected $formElementManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Entity to bind to the formulars. |
||
| 60 | * |
||
| 61 | * @var \Core\Entity\EntityInterface[] |
||
| 62 | */ |
||
| 63 | protected $entities; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Parameters to pass to the formulars. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $params = array(); |
||
| 71 | |||
| 72 | protected $parent; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param ServiceLocatorInterface $formElementManager |
||
| 76 | * @return Container |
||
| 77 | */ |
||
| 78 | public function setFormElementManager(ServiceLocatorInterface $formElementManager) |
||
| 79 | { |
||
| 80 | $this->formElementManager = $formElementManager; |
||
|
|
|||
| 81 | return $this; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Gets an iterator to iterate over the enabled formulars. |
||
| 86 | * |
||
| 87 | * @return \ArrayIterator |
||
| 88 | * @see IteratorAggregate::getIterator() |
||
| 89 | */ |
||
| 90 | public function getIterator() |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Gets the count of enabled formulars |
||
| 107 | * |
||
| 108 | * @return int |
||
| 109 | * @see Countable::count() |
||
| 110 | */ |
||
| 111 | public function count() |
||
| 112 | { |
||
| 113 | return count($this->activeForms); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param bool $flag |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function setIsDisableCapable($flag) |
||
| 121 | { |
||
| 122 | $this->options['is_disable_capable'] = $flag; |
||
| 123 | |||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function isDisableCapable() |
||
| 131 | { |
||
| 132 | return isset($this->options['is_disable_capable']) |
||
| 133 | ? $this->options['is_disable_capable'] : true; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param bool $flag |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function setIsDisableElementsCapable($flag) |
||
| 141 | { |
||
| 142 | $this->options['is_disable_elements_capable'] = $flag; |
||
| 143 | |||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isDisableElementsCapable() |
||
| 151 | { |
||
| 152 | return isset($this->options['is_disable_elements_capable']) |
||
| 153 | ? $this->options['is_disable_elements_capable'] : true; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param array $map |
||
| 158 | */ |
||
| 159 | public function disableElements(array $map) |
||
| 160 | { |
||
| 161 | foreach ($map as $key => $name) { |
||
| 162 | if (is_numeric($key)) { |
||
| 163 | if (isset($this->forms[$name])) { |
||
| 164 | $form = $this->getForm($name); |
||
| 165 | if (false !== $form->getOption('is_disable_capable')) { |
||
| 166 | $this->disableForm($name); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (!isset($this->forms[$key])) { |
||
| 173 | continue; |
||
| 174 | } |
||
| 175 | |||
| 176 | if (isset($this->forms[$key]['__instance__'])) { |
||
| 177 | $form = $this->forms[$key]['__instance__']; |
||
| 178 | |||
| 179 | if ($form instanceof DisableElementsCapableInterface |
||
| 180 | && $form->isDisableElementsCapable() |
||
| 181 | ) { |
||
| 182 | $form->disableElements($name); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | $this->forms[$key]['disable_elements'] = $name; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | public function setOptions($options) |
||
| 190 | { |
||
| 191 | parent::setOptions($options); |
||
| 192 | |||
| 193 | if (isset($this->options['forms'])) { |
||
| 194 | $this->setForms($this->options['forms']); |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Sets formular parameters. |
||
| 202 | * |
||
| 203 | * @param array $params |
||
| 204 | * @return \Core\Form\Container |
||
| 205 | */ |
||
| 206 | public function setParams(array $params) |
||
| 207 | { |
||
| 208 | $this->params = array_merge($this->params, $params); |
||
| 209 | |||
| 210 | foreach ($this->forms as $form) { |
||
| 211 | if (isset($form['__instance__']) |
||
| 212 | && is_object($form['__instance__']) |
||
| 213 | && method_exists($form['__instance__'], 'setParams') |
||
| 214 | ) { |
||
| 215 | $form['__instance__']->setParams($params); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Gets the formular parameters. |
||
| 223 | * |
||
| 224 | * @return array: |
||
| 225 | */ |
||
| 226 | public function getParams() |
||
| 227 | { |
||
| 228 | return $this->params; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Sets a formular parameter. |
||
| 233 | * |
||
| 234 | * @param string $key |
||
| 235 | * @param mixed $value |
||
| 236 | * @return \Core\Form\Container |
||
| 237 | */ |
||
| 238 | public function setParam($key, $value) |
||
| 239 | { |
||
| 240 | $this->params[$key] = $value; |
||
| 241 | |||
| 242 | foreach ($this->forms as $form) { |
||
| 243 | if (isset($form['__instance__']) |
||
| 244 | && is_object($form['__instance__']) |
||
| 245 | && method_exists($form['__instance__'], 'setParam') |
||
| 246 | ) { |
||
| 247 | $form['__instance__']->setParam($key, $value); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Gets the value of a formular parameter. |
||
| 255 | * |
||
| 256 | * Returns the provided <b>$default</b> value or null, if parameter does |
||
| 257 | * not exist. |
||
| 258 | * |
||
| 259 | * @param string $key |
||
| 260 | * @param mixed $default |
||
| 261 | * @return mixed |
||
| 262 | */ |
||
| 263 | public function getParam($key, $default = null) |
||
| 264 | { |
||
| 265 | return isset($this->params[$key]) ? $this->params[$key] : $default; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Gets a specific formular. |
||
| 270 | * |
||
| 271 | * This formular will be created upon the first retrievement. |
||
| 272 | * If created, the formular gets passed the formular parameters set in this container. |
||
| 273 | * |
||
| 274 | * @param string $key |
||
| 275 | * @param bool $asInstance if set to false, the specification array is returned, and no instance created. |
||
| 276 | * |
||
| 277 | * @return null|\Core\Form\Container|\Zend\Form\FormInterface |
||
| 278 | * @since 0,25 added $asInstance parameter |
||
| 279 | */ |
||
| 280 | public function getForm($key, $asInstance = true) |
||
| 281 | { |
||
| 282 | if (false !== strpos($key, '.')) { |
||
| 283 | list($key, $childKey) = explode('.', $key, 2); |
||
| 284 | $container = $this->getForm($key); |
||
| 285 | return $container->getForm($childKey); |
||
| 286 | } |
||
| 287 | |||
| 288 | if (!isset($this->forms[$key])) { |
||
| 289 | return null; |
||
| 290 | } |
||
| 291 | |||
| 292 | $form = $this->forms[$key]; |
||
| 293 | |||
| 294 | if (!$asInstance) { |
||
| 295 | return $form; |
||
| 296 | } |
||
| 297 | |||
| 298 | if (isset($form['__instance__']) && is_object($form['__instance__'])) { |
||
| 299 | return $form['__instance__']; |
||
| 300 | } |
||
| 301 | |||
| 302 | $options = isset($form['options']) ? $form['options'] : array(); |
||
| 303 | if (!isset($options['name'])) { |
||
| 304 | $options['name'] = isset($form['name']) ? $form['name'] : $key; |
||
| 305 | } |
||
| 306 | if (!isset($options['use_post_array'])) { |
||
| 307 | $options['use_post_array'] = true; |
||
| 308 | } |
||
| 309 | if (!isset($options['use_files_array'])) { |
||
| 310 | $options['use_files_array'] = false; |
||
| 311 | } |
||
| 312 | |||
| 313 | //@TODO: [ZF3] Passing options in $formElementManager->get is not working need to do manually set options |
||
| 314 | $formInstance = $this->formElementManager->get($form['type'], $options); |
||
| 315 | $formInstance->setOptions(array_merge($formInstance->getOptions(), $options)); |
||
| 316 | $formInstance->setParent($this); |
||
| 317 | |||
| 318 | if (isset($form['attributes'])) { |
||
| 319 | $formInstance->setAttributes($form['attributes']); |
||
| 320 | } |
||
| 321 | |||
| 322 | $formName = $this->formatAction($form['name']); |
||
| 323 | $formInstance->setName($formName); |
||
| 324 | $formAction = $formInstance->getAttribute('action'); |
||
| 325 | |||
| 326 | if (empty($formAction)) { |
||
| 327 | $formInstance->setAttribute('action', '?form=' . $formName); |
||
| 328 | } |
||
| 329 | |||
| 330 | // @TODO: [ZF3] which one is correct? $form[options][label] or $form[options] |
||
| 331 | if (isset($form['label'])) { |
||
| 332 | $formLabel = $form['label']; |
||
| 333 | } elseif (isset($form['options']['label'])) { |
||
| 334 | $formLabel = $form['options']['label']; |
||
| 335 | } |
||
| 336 | |||
| 337 | if (isset($formLabel)) { |
||
| 338 | $formInstance->setLabel($formLabel); |
||
| 339 | } |
||
| 340 | |||
| 341 | if (isset($form['disable_elements']) |
||
| 342 | && $formInstance instanceof DisableElementsCapableInterface |
||
| 343 | && $formInstance->isDisableElementsCapable() |
||
| 344 | ) { |
||
| 345 | $formInstance->disableElements($form['disable_elements']); |
||
| 346 | } |
||
| 347 | |||
| 348 | $entity = $this->getEntity($form['entity']); |
||
| 349 | if ($entity) { |
||
| 350 | $this->mapEntity($formInstance, $entity, isset($form['property']) ? $form['property'] : $key); |
||
| 351 | } |
||
| 352 | |||
| 353 | $formInstance->setParams($this->getParams()); |
||
| 354 | |||
| 355 | $this->forms[$key]['__instance__'] = $formInstance; |
||
| 356 | $this->forms[$key]['options'] = $options; |
||
| 357 | return $formInstance; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Execute an arbitrary action |
||
| 362 | * |
||
| 363 | * @param string $name Name of an action |
||
| 364 | * @param array $data Arbitrary data |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | public function executeAction($name, array $data = []) |
||
| 368 | { |
||
| 369 | if (false !== strpos($name, '.')) { |
||
| 370 | list($name, $childKey) = explode('.', $name, 2); |
||
| 371 | $container = $this->getForm($name); |
||
| 372 | |||
| 373 | // execute child container's action |
||
| 374 | return $container->executeAction($childKey, $data); |
||
| 375 | } |
||
| 376 | |||
| 377 | // this container defines no actions |
||
| 378 | return []; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Sets a form or form specification. |
||
| 383 | * |
||
| 384 | * if <b>$spec</b> is a string, it is used as form type, name is set to <b>$key</b> |
||
| 385 | * |
||
| 386 | * @param string $key |
||
| 387 | * @param string|array $spec |
||
| 388 | * @param boolean $enabled Should the formular be enabled or not |
||
| 389 | * |
||
| 390 | * @return self |
||
| 391 | */ |
||
| 392 | public function setForm($key, $spec, $enabled = true) |
||
| 393 | { |
||
| 394 | if (is_object($spec)) { |
||
| 395 | if ($spec instanceof FormParentInterface) { |
||
| 396 | $spec->setParent($this); |
||
| 397 | } |
||
| 398 | |||
| 399 | $spec = [ '__instance__' => $spec, 'name' => $key, 'entity' => '*' ]; |
||
| 400 | } |
||
| 401 | |||
| 402 | if (!is_array($spec)) { |
||
| 403 | $spec = array('type' => $spec, 'name' => $key); |
||
| 404 | } |
||
| 405 | if (!isset($spec['name'])) { |
||
| 406 | $spec['name'] = $key; |
||
| 407 | } |
||
| 408 | if (!isset($spec['entity'])) { |
||
| 409 | $spec['entity'] = '*'; |
||
| 410 | } |
||
| 411 | |||
| 412 | $this->forms[$key] = $spec; |
||
| 413 | |||
| 414 | if ($enabled) { |
||
| 415 | $this->enableForm($key); |
||
| 416 | } elseif (true === $this->activeForms) { |
||
| 417 | $this->activeForms = false; |
||
| 418 | } |
||
| 419 | return $this; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Sets formulars or specifications. |
||
| 424 | * |
||
| 425 | * <b>$forms</b> must be in the format: |
||
| 426 | * <pre> |
||
| 427 | * 'name' => [spec] |
||
| 428 | * </pre> |
||
| 429 | * |
||
| 430 | * <b>$spec</b> must be compatible with {@link setForm}. |
||
| 431 | * Additionally you can include a key 'enabled' in the spec, which will override |
||
| 432 | * <b>$enabled</b> only for the current formular. |
||
| 433 | * |
||
| 434 | * @param array $forms |
||
| 435 | * @param boolean $enabled |
||
| 436 | * @return \Core\Form\Container |
||
| 437 | */ |
||
| 438 | public function setForms(array $forms, $enabled = true) |
||
| 439 | { |
||
| 440 | foreach ($forms as $key => $spec) { |
||
| 441 | if (is_array($spec) && isset($spec['enabled'])) { |
||
| 442 | $currentEnabled = $spec['enabled']; |
||
| 443 | unset($spec['enabled']); |
||
| 444 | } else { |
||
| 445 | $currentEnabled = $enabled; |
||
| 446 | } |
||
| 447 | $this->setForm($key, $spec, $currentEnabled); |
||
| 448 | } |
||
| 449 | return $this; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Enables a formular. |
||
| 454 | * |
||
| 455 | * Enabled formulars are included in the {@link getIterator()} |
||
| 456 | * |
||
| 457 | * Traverses in child containers through .dot-Notation. |
||
| 458 | * |
||
| 459 | * @param string $key |
||
| 460 | * @return \Core\Form\Container |
||
| 461 | */ |
||
| 462 | public function enableForm($key = null) |
||
| 463 | { |
||
| 464 | if (null === $key) { |
||
| 465 | $this->activeForms = array_keys($this->forms); |
||
| 466 | return $this; |
||
| 467 | } |
||
| 468 | |||
| 469 | if (!is_array($key)) { |
||
| 470 | $key = array($key); |
||
| 471 | } |
||
| 472 | |||
| 473 | foreach ($key as $k) { |
||
| 474 | if (false !== strpos($k, '.')) { |
||
| 475 | // this seems not to be childkey.childform but actualkey.childkey |
||
| 476 | list($childKey, $childForm) = explode('.', $k, 2); |
||
| 477 | $child = $this->getForm($childKey); |
||
| 478 | $child->enableForm($childForm); |
||
| 479 | } else { |
||
| 480 | if (isset($this->forms[$k]) && !in_array($k, $this->activeForms)) { |
||
| 481 | $this->activeForms[] = $k; |
||
| 482 | } |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | return $this; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Disables a formular. |
||
| 491 | * |
||
| 492 | * @param string $key |
||
| 493 | * |
||
| 494 | * @return self |
||
| 495 | */ |
||
| 496 | public function disableForm($key = null) |
||
| 497 | { |
||
| 498 | if (null === $key) { |
||
| 499 | $this->activeForms = array(); |
||
| 500 | return $this; |
||
| 501 | } |
||
| 502 | |||
| 503 | if (!is_array($key)) { |
||
| 504 | $key = array($key); |
||
| 505 | } |
||
| 506 | |||
| 507 | foreach ($key as $k) { |
||
| 508 | if (false !== strpos($k, '.')) { |
||
| 509 | list($childKey, $childForm) = explode('.', $k, 2); |
||
| 510 | $child = $this->getForm($childKey); |
||
| 511 | $child->disableForm($childForm); |
||
| 512 | } elseif (isset($this->forms[$k]['__instance__'])) { |
||
| 513 | unset($this->forms[$k]['__instance__']); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | $this->activeForms = array_filter( |
||
| 517 | $this->activeForms, |
||
| 518 | function ($item) use ($key) { |
||
| 519 | return !in_array($item, $key); |
||
| 520 | } |
||
| 521 | ); |
||
| 522 | |||
| 523 | return $this; |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param mixed $entity |
||
| 528 | * @param string $key |
||
| 529 | * @throws \InvalidArgumentException |
||
| 530 | * @return Container |
||
| 531 | */ |
||
| 532 | public function setEntity($entity, $key='*') |
||
| 533 | { |
||
| 534 | if (!$entity instanceof EntityInterface) { |
||
| 535 | throw new \InvalidArgumentException(sprintf('$entity must be instance of %s', EntityInterface::class)); |
||
| 536 | } |
||
| 537 | |||
| 538 | $this->entities[$key] = $entity; |
||
| 539 | |||
| 540 | foreach ($this->forms as $formKey => $form) { |
||
| 541 | if (isset($form['__instance__']) && is_object($form['__instance__']) && $key == $form['entity']) { |
||
| 542 | $this->mapEntity($form['__instance__'], $entity, isset($form['property']) ? $form['property'] : $formKey); |
||
| 543 | } |
||
| 544 | } |
||
| 545 | return $this; |
||
| 546 | } |
||
| 547 | |||
| 548 | |||
| 549 | /** |
||
| 550 | * Gets the entity. |
||
| 551 | * |
||
| 552 | * @return \Core\Entity\EntityInterface |
||
| 553 | */ |
||
| 554 | public function getEntity($key='*') |
||
| 555 | { |
||
| 556 | return isset($this->entities[$key]) ? $this->entities[$key] : null; |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Maps entity property to forms or child containers. |
||
| 561 | * |
||
| 562 | * @param \Zend\Form\FormInterface $form |
||
| 563 | * @param \Core\Entity\EntityInterface $entity |
||
| 564 | * @param string $property |
||
| 565 | * @return void |
||
| 566 | */ |
||
| 567 | protected function mapEntity($form, $entity, $property) |
||
| 568 | { |
||
| 569 | if (false === $property) { |
||
| 570 | return; |
||
| 571 | } |
||
| 572 | |||
| 573 | if (true === $property) { |
||
| 574 | $mapEntity = $entity; |
||
| 575 | } elseif ($entity->hasProperty($property) || is_callable([$entity, "get$property"])) { |
||
| 576 | $getter = "get$property"; |
||
| 577 | $mapEntity = $entity->$getter(); |
||
| 578 | } else { |
||
| 579 | return; |
||
| 580 | } |
||
| 581 | if ($form instanceof Container) { |
||
| 582 | $form->setEntity($mapEntity); |
||
| 583 | } else { |
||
| 584 | $form->bind($mapEntity); |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Return isValid |
||
| 590 | * |
||
| 591 | * @return bool |
||
| 592 | */ |
||
| 593 | public function isValid() |
||
| 594 | { |
||
| 595 | $isValid = true; |
||
| 596 | foreach ($this->activeForms as $activeFormKey) { |
||
| 597 | $activeForm = $this->getForm($activeFormKey); |
||
| 598 | $isValid &= $activeForm->isValid(); |
||
| 599 | } |
||
| 600 | return $isValid; |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * if fieldsets there is get method to have access to any element by name |
||
| 605 | * this method is similar |
||
| 606 | * get('form') gets a form |
||
| 607 | * get('element') gets an element, if an element has the same name as a form, the form get's first access |
||
| 608 | * get('form.element') gets an element of a form, this is more efficent because it doesn't expand all forms in the container, |
||
| 609 | * but just the one adressed |
||
| 610 | * @param $key string |
||
| 611 | * @return null|\Zend\Form\ElementInterface |
||
| 612 | */ |
||
| 613 | public function get($key) |
||
| 614 | { |
||
| 615 | $return = null; |
||
| 616 | $lastKey = null; |
||
| 617 | $searchIn = $this->activeForms; |
||
| 618 | $keySplit = explode('.', $key); |
||
| 619 | |||
| 620 | while (0 < count($keySplit)) { |
||
| 621 | $lastKey = array_shift($keySplit); |
||
| 622 | foreach ($searchIn as $activeFormKey) { |
||
| 623 | if ($lastKey == $activeFormKey) { |
||
| 624 | $searchIn = $this->getForm($activeFormKey); |
||
| 625 | unset($lastKey); |
||
| 626 | break; |
||
| 627 | } |
||
| 628 | } |
||
| 629 | } |
||
| 630 | if (!isset($lastKey) && !empty($keySplit)) { |
||
| 631 | $lastKey = array_shift($keySplit); |
||
| 632 | } |
||
| 633 | if (isset($lastKey) && empty($keySplit)) { |
||
| 634 | if ($searchIn instanceof FieldsetInterface) { |
||
| 635 | // has reached a fieldset to search in |
||
| 636 | $return = $searchIn->get($lastKey); |
||
| 637 | unset($lastKey); |
||
| 638 | } elseif (is_array($searchIn) || $searchIn instanceof \Traversable) { |
||
| 639 | // is probably still in the container |
||
| 640 | foreach ($searchIn as $activeKey) { |
||
| 641 | $activeForm = $this->getForm($activeKey); |
||
| 642 | if ($activeForm instanceof FieldsetInterface) { |
||
| 643 | $return = $activeForm->get($lastKey); |
||
| 644 | } |
||
| 645 | } |
||
| 646 | } |
||
| 647 | } |
||
| 648 | if (!isset($lastKey) && empty($keySplit) && !isset($return)) { |
||
| 649 | $return = $searchIn; |
||
| 650 | } |
||
| 651 | return $return; |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @param $data |
||
| 656 | * @return $this |
||
| 657 | */ |
||
| 658 | public function setData($data) |
||
| 659 | { |
||
| 660 | $filteredData = array(); |
||
| 661 | foreach ($data as $key => $elem) { |
||
| 662 | if (!array_key_exists($key, $this->params) && $key != 'formName') { |
||
| 663 | $filteredData[$key] = $elem; |
||
| 664 | } |
||
| 665 | if ($key == 'formName' && is_string($elem)) { |
||
| 666 | // you can activate a specific form with postData |
||
| 667 | foreach ($this->activeForms as $activeFormKey) { |
||
| 668 | if ($activeFormKey == $elem) { |
||
| 669 | $this->enableForm($activeFormKey); |
||
| 670 | } else { |
||
| 671 | $this->disableForm($activeFormKey); |
||
| 672 | } |
||
| 673 | } |
||
| 674 | } |
||
| 675 | } |
||
| 676 | foreach ($this->activeForms as $activeFormKey) { |
||
| 677 | $activeForm = $this->getForm($activeFormKey); |
||
| 678 | $activeForm->setData($filteredData); |
||
| 679 | } |
||
| 680 | return $this; |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param $parent |
||
| 685 | * @return $this |
||
| 686 | */ |
||
| 687 | public function setParent($parent) |
||
| 688 | { |
||
| 689 | $this->parent = $parent; |
||
| 690 | return $this; |
||
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @return mixed |
||
| 695 | */ |
||
| 696 | public function getParent() |
||
| 697 | { |
||
| 698 | return $this->parent; |
||
| 699 | } |
||
| 700 | |||
| 701 | |||
| 702 | /** |
||
| 703 | * @return bool |
||
| 704 | */ |
||
| 705 | public function hasParent() |
||
| 706 | { |
||
| 707 | return isset($this->parent); |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * @param Renderer $renderer |
||
| 712 | * @return string |
||
| 713 | */ |
||
| 714 | public function renderPre(Renderer $renderer) |
||
| 715 | { |
||
| 716 | return ''; |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @param Renderer $renderer |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | public function renderPost(Renderer $renderer) |
||
| 724 | { |
||
| 725 | return ''; |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * get the actual active Form |
||
| 730 | * @param bool $setDefault |
||
| 731 | * @return mixed|null |
||
| 732 | */ |
||
| 733 | public function getActiveFormActual($setDefault = true) |
||
| 734 | { |
||
| 735 | $key = null; |
||
| 736 | if (!empty($this->activeForms)) { |
||
| 737 | $key = $this->activeForms[0]; |
||
| 738 | } |
||
| 739 | if (!isset($key) && $setDefault) { |
||
| 740 | $formsAvailable = array_keys($this->forms); |
||
| 741 | $key = array_shift($formsAvailable); |
||
| 742 | } |
||
| 743 | return $key; |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * get the form before the actual active |
||
| 748 | * @return null |
||
| 749 | */ |
||
| 750 | public function getActiveFormPrevious() |
||
| 751 | { |
||
| 752 | $key = null; |
||
| 753 | $actualKey = $this->getActiveFormActual(); |
||
| 754 | if (isset($actualKey)) { |
||
| 755 | $forms = array_keys($this->forms); |
||
| 756 | $formsFlip = array_flip($forms); |
||
| 757 | $index = $formsFlip[$actualKey]; |
||
| 758 | if (0 < $index) { |
||
| 759 | $key = $forms[$index-1]; |
||
| 760 | } |
||
| 761 | } |
||
| 762 | return $key; |
||
| 763 | } |
||
| 764 | |||
| 765 | |||
| 766 | /** |
||
| 767 | * Gets the form after the actual active |
||
| 768 | * @return null |
||
| 769 | */ |
||
| 770 | public function getActiveFormNext() |
||
| 771 | { |
||
| 772 | $key = null; |
||
| 773 | $actualKey = $this->getActiveFormActual(); |
||
| 774 | if (isset($actualKey)) { |
||
| 775 | $forms = array_keys($this->forms); |
||
| 776 | $formsFlip = array_flip($forms); |
||
| 777 | $index = $formsFlip[$actualKey]; |
||
| 778 | if ($index < count($forms) - 1) { |
||
| 779 | $key = $forms[$index+1]; |
||
| 780 | } |
||
| 781 | } |
||
| 782 | return $key; |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Format an action name |
||
| 787 | * |
||
| 788 | * @param string $name Name of an action |
||
| 789 | * @return string Formatted name of an action |
||
| 790 | */ |
||
| 791 | public function formatAction($name) |
||
| 792 | { |
||
| 793 | return sprintf('%s%s', $this->hasParent() ? $this->getName() . '.' : '', $name); |
||
| 794 | } |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @param $key |
||
| 798 | * @return string|null |
||
| 799 | */ |
||
| 800 | public function getActionFor($key) |
||
| 804 | } |
||
| 805 | } |
||
| 806 | } |
||
| 807 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.