Completed
Push — develop ( dfd31d...f6e11d )
by
unknown
17:02
created

Container::renderPre()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 Zend\Form\Element;
14
use Zend\Form\FieldsetInterface;
15
use Zend\Stdlib\PriorityList;
16
use Zend\View\Renderer\PhpRenderer as Renderer;
17
use Core\Entity\EntityInterface;
18
use Zend\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 \Zend\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
    public function setFormElementManager(ServiceLocatorInterface $formElementManager)
79
    {
80
        $this->formElementManager = $formElementManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $formElementManager of type object<Zend\ServiceManag...erviceLocatorInterface> is incompatible with the declared type object<Zend\Form\FormElementManager> of property $formElementManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
        return $this;
82
    }
83
84
    /**
85
     * Gets an iterator to iterate over the enabled formulars.
86
     *
87
     * @return \ArrayIterator
0 ignored issues
show
Documentation introduced by
Should the return type not be PriorityList?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
88
     * @see IteratorAggregate::getIterator()
89
     */
90
    public function getIterator()
91
    {
92
        $iterator = new PriorityList();
93
        $iterator->isLIFO(false);
94
95
        foreach ($this->activeForms as $key) {
96
            $spec = $this->forms[$key];
97
            $priority = isset($spec['priority']) ? $spec['priority'] : 0;
98
99
            $iterator->insert($key, $this->getForm($key), $priority);
100
        }
101
102
        return $iterator;
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:
0 ignored issues
show
Documentation introduced by
The doc-type array: could not be parsed: Unknown type name "array:" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method getForm does only exist in Core\Form\Container, but not in Zend\Form\FormInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 View Code Duplication
        if (!isset($options['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        $formInstance = $this->formElementManager->get($form['type'], $options);
314
        $formInstance->setParent($this);
315
        if (isset($form['attributes'])) {
316
            $formInstance->setAttributes($form['attributes']);
317
        }
318
319
        $formName = '';
320
        if (!empty($this->parent)) {
321
            $name = $this->getName();
322
            if (!empty($name)) {
323
                $formName .= $name . '.';
324
            }
325
        }
326
        $formName .= $form['name'];
327
        $formInstance->setName($formName);
328
        $formAction = $formInstance->getAttribute('action');
329
330
        if (empty($formAction)) {
331
            $formInstance->setAttribute('action', '?form=' . $formName);
332
        }
333
334
        if (isset($form['label'])) {
335
            $formInstance->setLabel($form['label']);
336
        }
337
338
        if (isset($form['disable_elements'])
339
            && $formInstance instanceof DisableElementsCapableInterface
340
            && $formInstance->isDisableElementsCapable()
341
        ) {
342
            $formInstance->disableElements($form['disable_elements']);
343
        }
344
345
        $entity = $this->getEntity($form['entity']);
346
        if ($entity) {
347
            $this->mapEntity($formInstance, $entity, isset($form['property']) ? $form['property'] : $key);
348
        }
349
350
        $formInstance->setParams($this->getParams());
351
352
        $this->forms[$key]['__instance__'] = $formInstance;
353
        $this->forms[$key]['options'] = $options;
354
        return $formInstance;
355
    }
356
357
    /**
358
     * Sets a form or form specification.
359
     *
360
     * if <b>$spec</b> is a string, it is used as form type, name is set to <b>$key</b>
361
     *
362
     * @param string       $key
363
     * @param string|array $spec
364
     * @param boolean      $enabled Should the formular be enabled or not
365
     *
366
     * @return self
367
     */
368
    public function setForm($key, $spec, $enabled = true)
369
    {
370
        if (is_object($spec)) {
371
            if ($spec instanceof FormParentInterface) {
372
                $spec->setParent($this);
373
            }
374
375
            $spec = [ '__instance__' => $spec, 'name' => $key, 'entity' => '*' ];
376
        }
377
378
        if (!is_array($spec)) {
379
            $spec = array('type' => $spec, 'name' => $key);
380
        }
381
        if (!isset($spec['name'])) {
382
            $spec['name'] = $key;
383
        }
384
        if (!isset($spec['entity'])) {
385
            $spec['entity'] = '*';
386
        }
387
        
388
        $this->forms[$key] = $spec;
389
390
        if ($enabled) {
391
            $this->enableForm($key);
392
        } elseif (true === $this->activeForms) {
393
            $this->activeForms = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type array of property $activeForms.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
394
        }
395
        return $this;
396
    }
397
    
398
    /**
399
     * Sets formulars or specifications.
400
     *
401
     * <b>$forms</b> must be in the format:
402
     * <pre>
403
     *    'name' => [spec]
404
     * </pre>
405
     *
406
     * <b>$spec</b> must be compatible with {@link setForm}.
407
     * Additionally you can include a key 'enabled' in the spec, which will override
408
     * <b>$enabled</b> only for the current formular.
409
     *
410
     * @param array $forms
411
     * @param boolean $enabled
412
     * @return \Core\Form\Container
413
     */
414
    public function setForms(array $forms, $enabled = true)
415
    {
416
        foreach ($forms as $key => $spec) {
417
            if (is_array($spec) && isset($spec['enabled'])) {
418
                $currentEnabled = $spec['enabled'];
419
                unset($spec['enabled']);
420
            } else {
421
                $currentEnabled = $enabled;
422
            }
423
            $this->setForm($key, $spec, $currentEnabled);
424
        }
425
        return $this;
426
    }
427
    
428
    /**
429
     * Enables a formular.
430
     *
431
     * Enabled formulars are included in the {@link getIterator()}
432
     *
433
     * Traverses in child containers through .dot-Notation.
434
     *
435
     * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
436
     * @return \Core\Form\Container
437
     */
438
    public function enableForm($key = null)
439
    {
440
        if (null === $key) {
441
            $this->activeForms = array_keys($this->forms);
442
            return $this;
443
        }
444
        
445
        if (!is_array($key)) {
446
            $key = array($key);
447
        }
448
        
449
        foreach ($key as $k) {
450
            if (false !== strpos($k, '.')) {
451
                // this seems not to be childkey.childform but actualkey.childkey
452
                list($childKey, $childForm) = explode('.', $k, 2);
453
                $child = $this->getForm($childKey);
454
                $child->enableForm($childForm);
0 ignored issues
show
Bug introduced by
The method enableForm does only exist in Core\Form\Container, but not in Zend\Form\FormInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
455
            } else {
456
                if (isset($this->forms[$k]) && !in_array($k, $this->activeForms)) {
457
                    $this->activeForms[] = $k;
458
                }
459
            }
460
        }
461
        
462
        return $this;
463
    }
464
465
    /**
466
     * Disables a formular.
467
     *
468
     * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
469
     *
470
     * @return self
471
     */
472
    public function disableForm($key = null)
473
    {
474
        if (null === $key) {
475
            $this->activeForms = array();
476
            return $this;
477
        }
478
        
479
        if (!is_array($key)) {
480
            $key = array($key);
481
        }
482
        
483
        foreach ($key as $k) {
484
            if (false !== strpos($k, '.')) {
485
                list($childKey, $childForm) = explode('.', $k, 2);
486
                $child = $this->getForm($childKey);
487
                $child->disableForm($childForm);
0 ignored issues
show
Bug introduced by
The method disableForm does only exist in Core\Form\Container, but not in Zend\Form\FormInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
488
            } elseif (isset($this->forms[$k]['__instance__'])) {
489
                unset($this->forms[$k]['__instance__']);
490
            }
491
        }
492
        $this->activeForms = array_filter(
493
            $this->activeForms,
494
            function ($item) use ($key) {
495
                return !in_array($item, $key);
496
            }
497
        );
498
        
499
        return $this;
500
    }
501
    
502
    /**
503
     * Sets the entity for formular binding.
504
     *
505
     * @param EntityInterface $entity
506
     * @return self
507
     */
508
    public function setEntity(EntityInterface $entity, $key='*')
509
    {
510
        $this->entities[$key] = $entity;
511
        
512
        foreach ($this->forms as $formKey => $form) {
513
            if (isset($form['__instance__']) && is_object($form['__instance__']) && $key == $form['entity']) {
514
                $this->mapEntity($form['__instance__'], $entity, isset($form['property']) ? $form['property'] : $formKey);
515
            }
516
        }
517
        return $this;
518
    }
519
520
521
    /**
522
     * Gets the entity.
523
     *
524
     * @return \Core\Entity\EntityInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be EntityInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
525
     */
526
    public function getEntity($key='*')
527
    {
528
        return isset($this->entities[$key]) ? $this->entities[$key] : null;
529
    }
530
    
531
    /**
532
     * Maps entity property to forms or child containers.
533
     *
534
     * @param \Zend\Form\FormInterface $form
535
     * @param \Core\Entity\EntityInterface $entity
536
     * @param string $property
537
     * @return void
538
     */
539
    protected function mapEntity($form, $entity, $property)
540
    {
541
        if (false === $property) {
542
            return;
543
        }
544
545
        if (true === $property) {
546
            $mapEntity = $entity;
547
        } else if ($entity->hasProperty($property)) {
548
            $getter = "get$property";
549
            $mapEntity = $entity->$getter();
550
        } else {
551
            return;
552
        }
553
        
554
        if ($form instanceof Container) {
555
            $form->setEntity($mapEntity);
556
        } else {
557
            $form->bind($mapEntity);
558
        }
559
    }
560
561
    /**
562
     * Return isValid
563
     *
564
     * @return bool
565
     */
566
    public function isValid()
567
    {
568
        $isValid = true;
569
        foreach ($this->activeForms as $activeFormKey) {
570
            $activeForm = $this->getForm($activeFormKey);
571
            $isValid &= $activeForm->isValid();
572
        }
573
        return $isValid;
574
    }
575
576
    /**
577
     * if fieldsets there is get method to have access to any element by name
578
     * this method is similar
579
     * get('form') gets a form
580
     * get('element') gets an element, if an element has the same name as a form, the form get's first access
581
     * get('form.element') gets an element of a form, this is more efficent because it doesn't expand all forms in the container,
582
     *      but just the one adressed
583
     * @param $key string
584
     * @return null|\Zend\Form\ElementInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null|\Zend\Form\ElementInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
585
     */
586
    public function get($key)
587
    {
588
        $return   = null;
589
        $lastKey  = null;
590
        $searchIn = $this->activeForms;
591
        $keySplit = explode('.', $key);
592
593
        while (0 < count($keySplit)) {
594
            $lastKey = array_shift($keySplit);
595
            foreach ($searchIn as $activeFormKey) {
0 ignored issues
show
Bug introduced by
The expression $searchIn of type null|object<Core\Form\Co...rm\FormInterface>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
596
                if ($lastKey == $activeFormKey) {
597
                    $searchIn = $this->getForm($activeFormKey);
598
                    unset($lastKey);
599
                    break;
600
                }
601
            }
602
        }
603
        if (!isset($lastKey) && !empty($keySplit)) {
604
            $lastKey = array_shift($keySplit);
605
        }
606
        if (isset($lastKey) && empty($keySplit)) {
607
            if ($searchIn instanceof FieldsetInterface) {
608
                // has reached a fieldset to search in
609
                $return = $searchIn->get($lastKey);
610
                unset($lastKey);
611
            } elseif (is_array($searchIn) || $searchIn instanceof \Traversable) {
612
                // is probably still in the container
613
                foreach ($searchIn as $activeKey) {
614
                    $activeForm = $this->getForm($activeKey);
615
                    if ($activeForm instanceof FieldsetInterface) {
616
                        $return = $activeForm->get($lastKey);
617
                    }
618
                }
619
            }
620
        }
621
        if (!isset($lastKey) && empty($keySplit) && !isset($return)) {
622
            $return = $searchIn;
623
        }
624
        return $return;
625
    }
626
627
    /**
628
     * @param $data
629
     * @return $this
630
     */
631
    public function setData($data)
632
    {
633
        $filteredData = array();
634
        foreach ($data as $key => $elem) {
635
            if (!array_key_exists($key, $this->params) && $key != 'formName') {
636
                $filteredData[$key] = $elem;
637
            }
638
            if ($key == 'formName' && is_string($elem)) {
639
                // you can activate a specific form with postData
640
                foreach ($this->activeForms as $activeFormKey) {
641
                    if ($activeFormKey == $elem) {
642
                        $this->enableForm($activeFormKey);
643
                    } else {
644
                        $this->disableForm($activeFormKey);
645
                    }
646
                }
647
            }
648
        }
649
        foreach ($this->activeForms as $activeFormKey) {
650
            $activeForm = $this->getForm($activeFormKey);
651
            $activeForm->setData($filteredData);
652
        }
653
        return $this;
654
    }
655
656
    /**
657
     * @param $parent
658
     * @return $this
659
     */
660
    public function setParent($parent)
661
    {
662
        $this->parent = $parent;
663
        return $this;
664
    }
665
666
    /**
667
     * @return mixed
668
     */
669
    public function getParent()
670
    {
671
        return $this->parent;
672
    }
673
674
675
    /**
676
     * @return bool
677
     */
678
    public function hasParent()
679
    {
680
        return isset($this->parent);
681
    }
682
683
    /**
684
     * @param Renderer $renderer
685
     * @return string
686
     */
687
    public function renderPre(Renderer $renderer)
0 ignored issues
show
Unused Code introduced by
The parameter $renderer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
688
    {
689
        return '';
690
    }
691
692
    /**
693
     * @param Renderer $renderer
694
     * @return string
695
     */
696
    public function renderPost(Renderer $renderer)
697
    {
698
        return '';
699
    }
700
701
    /**
702
     * get the actual active Form
703
     * @param bool $setDefault
704
     * @return mixed|null
705
     */
706
    public function getActiveFormActual($setDefault = true)
707
    {
708
        $key = null;
709
        if (!empty($this->activeForms)) {
710
            $key = $this->activeForms[0];
711
        }
712
        if (!isset($key) && $setDefault) {
713
            $formsAvailable = array_keys($this->forms);
714
            $key = array_shift($formsAvailable);
715
        }
716
        return $key;
717
    }
718
719
    /**
720
     * get the form before the actual active
721
     * @return null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
722
     */
723 View Code Duplication
    public function getActiveFormPrevious()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
724
    {
725
        $key = null;
726
        $actualKey = $this->getActiveFormActual();
727
        if (isset($actualKey)) {
728
            $forms = array_keys($this->forms);
729
            $formsFlip =  array_flip($forms);
730
            $index = $formsFlip[$actualKey];
731
            if (0 < $index) {
732
                $key = $forms[$index-1];
733
            }
734
        }
735
        return $key;
736
    }
737
738
739
    /**
740
     * Gets the form after the actual active
741
     * @return null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
742
     */
743 View Code Duplication
    public function getActiveFormNext()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
744
    {
745
        $key = null;
746
        $actualKey = $this->getActiveFormActual();
747
        if (isset($actualKey)) {
748
            $forms = array_keys($this->forms);
749
            $formsFlip =  array_flip($forms);
750
            $index = $formsFlip[$actualKey];
751
            if ($index < count($forms) - 1) {
752
                $key = $forms[$index+1];
753
            }
754
        }
755
        return $key;
756
    }
757
758
    /**
759
     * @param $key
760
     * @return string
761
     */
762
    public function getActionFor($key)
763
    {
764
        $form               = $this->forms[$key];
765
        $options            = isset($form['options']) ? $form['options'] : array();
766
767
        if (!isset($options['use_post_array'])) {
768
            $options['use_post_array'] = true;
769
        }
770
        if (!isset($options['use_files_array'])) {
771
            $options['use_files_array'] = false;
772
        }
773
774
        $formName     = (($name = $this->getName()) ? $name . '.' : '') . $form['name'];
775
        $action = '?form=' . $formName;
776
777
        return $action;
778
    }
779
}