Container::getForm()   F
last analyzed

Complexity

Conditions 21
Paths 2308

Size

Total Lines 78
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 22.5548

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 78
c 0
b 0
f 0
ccs 39
cts 46
cp 0.8478
rs 0
cc 21
nc 2308
nop 2
crap 22.5548

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Core forms */
11
namespace Core\Form;
12
13
use Laminas\Form\Element;
14
use Laminas\Form\FieldsetInterface;
15
use Laminas\Stdlib\PriorityList;
16
use Laminas\View\Renderer\PhpRenderer as Renderer;
17
use Core\Entity\EntityInterface;
18
use Laminas\ServiceManager\ServiceLocatorInterface;
19
20
/**
21
 * Manages a group of formulars.
22
 *
23
 * The container is responsible for creating, populating and binding the formulars from or to
24
 * the corresponding entities.
25
 *
26
 * Formulars are lazy loaded. So it is possible to only retrieve one formular from the container
27
 * for asynchronous saving using ajax calls.
28
 *
29
 * @author Mathias Gelhausen <[email protected]>
30
 */
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 \Laminas\Form\FormElementManager
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 22
    public function setFormElementManager(ServiceLocatorInterface $formElementManager)
79
    {
80 22
        $this->formElementManager = $formElementManager;
0 ignored issues
show
Documentation Bug introduced by
$formElementManager is of type Laminas\ServiceManager\ServiceLocatorInterface, but the property $formElementManager was declared to be of type Laminas\Form\FormElementManager. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

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.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
81 22
        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 1
    public function getIterator()
91
    {
92 1
        $iterator = new PriorityList();
93 1
        $iterator->isLIFO(false);
94
95 1
        foreach ($this->activeForms as $key) {
96 1
            $spec = $this->forms[$key];
97 1
            $priority = isset($spec['priority']) ? $spec['priority'] : 0;
98
99 1
            $iterator->insert($key, $this->getForm($key), $priority);
100
        }
101
102 1
        return $iterator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $iterator returns the type Laminas\Stdlib\PriorityList which is incompatible with the documented return type ArrayIterator.
Loading history...
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 2
    public function setIsDisableCapable($flag)
121
    {
122 2
        $this->options['is_disable_capable'] = $flag;
123
124 2
        return $this;
125
    }
126
127
    /**
128
     * @return bool
129
     */
130 2
    public function isDisableCapable()
131
    {
132 2
        return isset($this->options['is_disable_capable'])
133 2
               ? $this->options['is_disable_capable'] : true;
134
    }
135
136
    /**
137
     * @param bool $flag
138
     * @return $this
139
     */
140 2
    public function setIsDisableElementsCapable($flag)
141
    {
142 2
        $this->options['is_disable_elements_capable'] = $flag;
143
144 2
        return $this;
145
    }
146
147
    /**
148
     * @return bool
149
     */
150 2
    public function isDisableElementsCapable()
151
    {
152 2
        return isset($this->options['is_disable_elements_capable'])
153 2
               ? $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 1
    public function setOptions($options)
190
    {
191 1
        parent::setOptions($options);
192
193 1
        if (isset($this->options['forms'])) {
194
            $this->setForms($this->options['forms']);
195
        }
196
197 1
        return $this;
198
    }
199
200
    /**
201
     * Sets formular parameters.
202
     *
203
     * @param array $params
204
     * @return \Core\Form\Container
205
     */
206 1
    public function setParams(array $params)
207
    {
208 1
        $this->params = array_merge($this->params, $params);
209
210 1
        foreach ($this->forms as $form) {
211 1
            if (isset($form['__instance__'])
212 1
                && is_object($form['__instance__'])
213 1
                && method_exists($form['__instance__'], 'setParams')
214
            ) {
215
                $form['__instance__']->setParams($params);
216
            }
217
        }
218 1
        return $this;
219
    }
220
221
    /**
222
     * Gets the formular parameters.
223
     *
224
     * @return array:
225
     */
226 1
    public function getParams()
227
    {
228 1
        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|\Laminas\Form\FormInterface
278
     * @since 0,25 added $asInstance parameter
279
     */
280 1
    public function getForm($key, $asInstance = true)
281
    {
282 1
        if (false !== strpos($key, '.')) {
283 1
            list($key, $childKey) = explode('.', $key, 2);
284 1
            $container = $this->getForm($key);
285 1
            return $container->getForm($childKey);
0 ignored issues
show
Bug introduced by
The method getForm() does not exist on Laminas\Form\FormInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

285
            return $container->/** @scrutinizer ignore-call */ getForm($childKey);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
286
        }
287
288 1
        if (!isset($this->forms[$key])) {
289
            return null;
290
        }
291
292 1
        $form = $this->forms[$key];
293
294 1
        if (!$asInstance) {
295
            return $form;
296
        }
297
298 1
        if (isset($form['__instance__']) && is_object($form['__instance__'])) {
299 1
            return $form['__instance__'];
300
        }
301
302 1
        $options = isset($form['options']) ? $form['options'] : array();
303 1
        if (!isset($options['name'])) {
304 1
            $options['name'] = isset($form['name']) ? $form['name'] : $key;
305
        }
306 1
        if (!isset($options['use_post_array'])) {
307 1
            $options['use_post_array'] = true;
308
        }
309 1
        if (!isset($options['use_files_array'])) {
310 1
            $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 1
        $formInstance = $this->formElementManager->get($form['type'], $options);
315 1
        $formInstance->setOptions(array_merge($formInstance->getOptions(), $options));
316 1
        $formInstance->setParent($this);
317
318 1
        if (isset($form['attributes'])) {
319
            $formInstance->setAttributes($form['attributes']);
320
        }
321
322 1
        $formName = $this->formatAction($form['name']);
323 1
        $formInstance->setName($formName);
324 1
        $formAction = $formInstance->getAttribute('action');
325
326 1
        if (empty($formAction)) {
327 1
            $formInstance->setAttribute('action', '?form=' . $formName);
328
        }
329
330
        // @TODO: [ZF3] which one is correct? $form[options][label] or $form[options]
331 1
        if (isset($form['label'])) {
332
            $formLabel = $form['label'];
333 1
        } elseif (isset($form['options']['label'])) {
334
            $formLabel = $form['options']['label'];
335
        }
336
337 1
        if (isset($formLabel)) {
338
            $formInstance->setLabel($formLabel);
339
        }
340
341 1
        if (isset($form['disable_elements'])
342 1
            && $formInstance instanceof DisableElementsCapableInterface
343 1
            && $formInstance->isDisableElementsCapable()
344
        ) {
345
            $formInstance->disableElements($form['disable_elements']);
346
        }
347
348 1
        $entity = $this->getEntity($form['entity']);
349 1
        if ($entity) {
0 ignored issues
show
introduced by
$entity is of type Core\Entity\EntityInterface, thus it always evaluated to true.
Loading history...
350 1
            $this->mapEntity($formInstance, $entity, isset($form['property']) ? $form['property'] : $key);
0 ignored issues
show
Bug introduced by
It seems like $formInstance can also be of type Core\Form\DisableElementsCapableInterface; however, parameter $form of Core\Form\Container::mapEntity() does only seem to accept Laminas\Form\FormInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

350
            $this->mapEntity(/** @scrutinizer ignore-type */ $formInstance, $entity, isset($form['property']) ? $form['property'] : $key);
Loading history...
351
        }
352
353 1
        $formInstance->setParams($this->getParams());
0 ignored issues
show
Bug introduced by
The method setParams() does not exist on Core\Form\DisableElementsCapableInterface. It seems like you code against a sub-type of Core\Form\DisableElementsCapableInterface such as Core\Form\Container or Core\Form\Form. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

353
        $formInstance->/** @scrutinizer ignore-call */ 
354
                       setParams($this->getParams());
Loading history...
354
355 1
        $this->forms[$key]['__instance__'] = $formInstance;
356 1
        $this->forms[$key]['options'] = $options;
357 1
        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);
0 ignored issues
show
Bug introduced by
The method executeAction() does not exist on Laminas\Form\FormInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

374
            return $container->/** @scrutinizer ignore-call */ executeAction($childKey, $data);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 6
    public function setForm($key, $spec, $enabled = true)
393
    {
394 6
        if (is_object($spec)) {
0 ignored issues
show
introduced by
The condition is_object($spec) is always false.
Loading history...
395 1
            if ($spec instanceof FormParentInterface) {
396 1
                $spec->setParent($this);
397
            }
398
399 1
            $spec = [ '__instance__' => $spec, 'name' => $key, 'entity' => '*' ];
400
        }
401
402 6
        if (!is_array($spec)) {
403 3
            $spec = array('type' => $spec, 'name' => $key);
404
        }
405 6
        if (!isset($spec['name'])) {
406 3
            $spec['name'] = $key;
407
        }
408 6
        if (!isset($spec['entity'])) {
409 5
            $spec['entity'] = '*';
410
        }
411
412 6
        $this->forms[$key] = $spec;
413
414 6
        if ($enabled) {
415 5
            $this->enableForm($key);
416 1
        } elseif (true === $this->activeForms) {
0 ignored issues
show
introduced by
The condition true === $this->activeForms is always false.
Loading history...
417
            $this->activeForms = false;
418
        }
419 6
        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 1
    public function setForms(array $forms, $enabled = true)
439
    {
440 1
        foreach ($forms as $key => $spec) {
441 1
            if (is_array($spec) && isset($spec['enabled'])) {
442
                $currentEnabled = $spec['enabled'];
443
                unset($spec['enabled']);
444
            } else {
445 1
                $currentEnabled = $enabled;
446
            }
447 1
            $this->setForm($key, $spec, $currentEnabled);
448
        }
449 1
        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 5
    public function enableForm($key = null)
463
    {
464 5
        if (null === $key) {
465
            $this->activeForms = array_keys($this->forms);
466
            return $this;
467
        }
468
469 5
        if (!is_array($key)) {
0 ignored issues
show
introduced by
The condition is_array($key) is always false.
Loading history...
470 5
            $key = array($key);
471
        }
472
473 5
        foreach ($key as $k) {
474 5
            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);
0 ignored issues
show
Bug introduced by
The method enableForm() does not exist on Laminas\Form\FormInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

478
                $child->/** @scrutinizer ignore-call */ 
479
                        enableForm($childForm);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
479
            } else {
480 5
                if (isset($this->forms[$k]) && !in_array($k, $this->activeForms)) {
481 5
                    $this->activeForms[] = $k;
482
                }
483
            }
484
        }
485
486 5
        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)) {
0 ignored issues
show
introduced by
The condition is_array($key) is always false.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method disableForm() does not exist on Laminas\Form\FormInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

511
                $child->/** @scrutinizer ignore-call */ 
512
                        disableForm($childForm);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 5
    public function setEntity($entity, $key='*')
533
    {
534 5
        if (!$entity instanceof EntityInterface) {
535
            throw new \InvalidArgumentException(sprintf('$entity must be instance of %s', EntityInterface::class));
536
        }
537
538 5
        $this->entities[$key] = $entity;
539
540 5
        foreach ($this->forms as $formKey => $form) {
541 1
            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 5
        return $this;
546
    }
547
548
549
    /**
550
     * Gets the entity.
551
     *
552
     * @return \Core\Entity\EntityInterface
553
     */
554 18
    public function getEntity($key='*')
555
    {
556 18
        return isset($this->entities[$key]) ? $this->entities[$key] : null;
557
    }
558
559
    /**
560
     * Maps entity property to forms or child containers.
561
     *
562
     * @param \Laminas\Form\FormInterface $form
563
     * @param \Core\Entity\EntityInterface $entity
564
     * @param string $property
565
     * @return void
566
     */
567 1
    protected function mapEntity($form, $entity, $property)
568
    {
569 1
        if (false === $property) {
0 ignored issues
show
introduced by
The condition false === $property is always false.
Loading history...
570
            return;
571
        }
572
573 1
        if (true === $property) {
0 ignored issues
show
introduced by
The condition true === $property is always false.
Loading history...
574 1
            $mapEntity = $entity;
575 1
        } elseif ($entity->hasProperty($property) || is_callable([$entity, "get$property"])) {
576 1
            $getter = "get$property";
577 1
            $mapEntity = $entity->$getter();
578
        } else {
579
            return;
580
        }
581 1
        if ($form instanceof Container) {
582 1
            $form->setEntity($mapEntity);
583
        } else {
584 1
            $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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $isValid also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
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|\Laminas\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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return also could return the type array which is incompatible with the documented return type Laminas\Form\ElementInterface|null.
Loading history...
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 3
    public function setParent($parent)
688
    {
689 3
        $this->parent = $parent;
690 3
        return $this;
691
    }
692
693
    /**
694
     * @return mixed
695
     */
696 1
    public function getParent()
697
    {
698 1
        return $this->parent;
699
    }
700
701
702
    /**
703
     * @return bool
704
     */
705 8
    public function hasParent()
706
    {
707 8
        return isset($this->parent);
708
    }
709
710
    /**
711
     * @param Renderer $renderer
712
     * @return string
713
     */
714 2
    public function renderPre(Renderer $renderer)
715
    {
716 2
        return '';
717
    }
718
719
    /**
720
     * @param Renderer $renderer
721
     * @return string
722
     */
723 2
    public function renderPost(Renderer $renderer)
724
    {
725 2
        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 7
    public function formatAction($name)
792
    {
793 7
        return sprintf('%s%s', $this->hasParent() ? $this->getName() . '.' : '', $name);
794
    }
795
796
    /**
797
     * @param $key
798
     * @return string|null
799
     */
800 1
    public function getActionFor($key)
801
    {
802 1
        if (isset($this->forms[$key])) {
803 1
            return '?form=' . $this->formatAction($this->forms[$key]['name']);
804
        }
805
    }
806
}
807