Completed
Push — master ( 6b5246...8691e9 )
by David
04:53 queued 02:40
created

DefaultManipulator::getFieldsConfig()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 6.7273
cc 7
eloc 15
nc 6
nop 2
1
<?php
2
3
namespace A2lix\AutoFormBundle\Form\Manipulator;
4
5
use A2lix\AutoFormBundle\ObjectInfo\ObjectInfoInterface;
6
7
/**
8
 * @author David ALLIX
9
 */
10
class DefaultManipulator implements FormManipulatorInterface
11
{
12
    /** @var ObjectInfoInterface */
13
    private $objectInfo;
14
    /** @var array */
15
    private $excludedFields;
16
17
    /**
18
     * @param ObjectInfoInterface $objectInfo
19
     * @param array               $excludedFields
20
     */
21
    public function __construct(ObjectInfoInterface $objectInfo, array $excludedFields = [])
22
    {
23
        $this->objectInfo = $objectInfo;
24
        $this->excludedFields = $excludedFields;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getFieldsConfig($class, array $formFieldsConfig)
31
    {
32
        $objectFieldsConfig = $this->objectInfo->getFieldsConfig($class);
33
34
        // Filtering to remove excludedFields
35
        $usuableObjectFieldsConfig = $this->filteringUsuableFields($objectFieldsConfig);
36
37
        if (empty($formFieldsConfig)) {
38
            return $usuableObjectFieldsConfig;
39
        }
40
41
        // Check unknows fields
42
        $unknowsFields = array_diff(array_keys($formFieldsConfig), array_keys($usuableObjectFieldsConfig));
43
        if (count($unknowsFields)) {
44
            throw new \RuntimeException(sptrinf("Field(s) '%s' doesn't exist in %s", implode(', ', $unknowsFields), $class));
45
        }
46
47
        foreach ($formFieldsConfig as $formFieldName => $formFieldConfig) {
48
            if (null === $formFieldConfig) {
49
                continue;
50
            }
51
52
            // If display undesired, remove
53
            if (isset($formFieldConfig['display']) && (false === $formFieldConfig['display'])) {
54
                unset($usuableObjectFieldsConfig[$formFieldName]);
55
            }
56
57
            // Override with formFieldsConfig priority
58
            $usuableObjectFieldsConfig[$formFieldName] = $formFieldConfig + $usuableObjectFieldsConfig[$formFieldName];
59
        }
60
61
        return $usuableObjectFieldsConfig;
62
    }
63
64
    /**
65
     * @param array $objectFieldsConfig
66
     *
67
     * @return array
68
     */
69
    private function filteringUsuableFields(array $objectFieldsConfig)
70
    {
71
        $usualableFields = [];
72
73
        foreach ($objectFieldsConfig as $fieldName => $fieldConfig) {
74
            if (!in_array($fieldName, $this->excludedFields, true)) {
75
                $usualableFields[$fieldName] = $fieldConfig;
76
            }
77
        }
78
79
        return $usualableFields;
80
    }
81
}
82