Completed
Push — middleware-wip ( f76eb7...e5756f )
by Romain
07:43
created

Substep::dataPreProcessor()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 6
nop 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Form\Definition\Step\Step\Substep;
15
16
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
17
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
18
use Romm\Formz\Form\Definition\AbstractFormDefinition;
19
use Romm\Formz\Form\Definition\Field\Field;
20
use Romm\Formz\Form\Definition\Step\Step\SupportedField;
21
22
class Substep extends AbstractFormDefinition implements DataPreProcessorInterface
23
{
24
    /**
25
     * @var string
26
     * @validate NotEmpty
27
     */
28
    protected $identifier;
29
30
    /**
31
     * @var \Romm\Formz\Form\Definition\Step\Step\SupportedField[]
32
     * @validate NotEmpty
33
     */
34
    protected $supportedFields;
35
36
    /**
37
     * @param string $identifier
38
     */
39
    public function __construct($identifier)
40
    {
41
        $this->identifier = $identifier;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getIdentifier()
48
    {
49
        return $this->identifier;
50
    }
51
52
    /**
53
     * @return SupportedField[]
54
     */
55
    public function getSupportedFields()
56
    {
57
        return $this->supportedFields;
58
    }
59
60
    /**
61
     * @param Field $field
62
     * @return bool
63
     */
64
    public function supportsField(Field $field)
65
    {
66
        foreach ($this->supportedFields as $supportedField) {
67
            if ($supportedField->getField() === $field) {
68
                return true;
69
            }
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * This function will parse the configuration for `supportedFields`: instead
77
     * of being forced to fill the `fieldName` option for each entry, the field
78
     * key of the entry can be the actual name of the field, and the value of
79
     * the entry can be anything but an array.
80
     *
81
     * @param DataPreProcessor $processor
82
     */
83
    public static function dataPreProcessor(DataPreProcessor $processor)
84
    {
85
        $data = $processor->getData();
86
87
        $supportedFields = (isset($data['supportedFields']))
88
            ? $data['supportedFields']
89
            : [];
90
91
        foreach ($supportedFields as $key => $supportedField) {
92
            $supportedField = is_array($supportedField)
93
                ? $supportedField
94
                : [];
95
            $supportedField['fieldName'] = $key;
96
97
            $supportedFields[$key] = $supportedField;
98
        }
99
100
        $data['supportedFields'] = $supportedFields;
101
102
        $processor->setData($data);
103
    }
104
}
105