ValueToDuplicatesTransformerPass::getKeys()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Processor;
3
4
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContextBuilder;
5
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Condition\FieldDependency;
6
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
7
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleProcessorContext;
8
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\TransformerRule;
9
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
10
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToDuplicatesTransformer;
11
12
/**
13
 * @author Warnar Boekkooi <[email protected]>
14
 */
15
class ValueToDuplicatesTransformerPass extends ViewTransformerProcessor
16
{
17
    /**
18
     * @var null|\ReflectionProperty
19
     */
20
    private $keyReflectionCache = null;
21
22 4
    public function process(FormRuleProcessorContext $context, FormRuleContextBuilder $collection)
23
    {
24 4
        $form = $context->getForm();
25 4
        $formConfig = $form->getConfig();
26 4
        if (!$formConfig->getCompound()) {
27 1
            return;
28
        }
29
30 3
        $transformer = $this->findTransformer($formConfig, ValueToDuplicatesTransformer::class);
31 3
        if ($transformer === null) {
32 1
            return;
33
        }
34
35 2
        $keys = $this->getKeys($transformer);
36 2
        if (empty($keys)) {
37 1
            return;
38
        }
39
40 1
        $formView = $context->getView();
41
42
        // Use children here since we need the full_name
43 1
        $primary = array_shift($keys);
44 1
        $primaryView = $formView->children[$primary];
45
46
        // Copy all rules to the first child/key element
47 1
        $ruleCollection = $collection->get($formView);
48 1
        if (!empty($ruleCollection)) {
49 1
            $collection->add(
50 1
                $primaryView,
51
                $ruleCollection
52 1
            );
53 1
        }
54 1
        $collection->remove($formView);
55
56
        // Get correct error message if one is set.
57 1
        $invalidMessage = $this->getFormRuleMessage($formConfig);
58
59
        // Create equalTo rules for all other fields
60 1
        $equalToPrimaryRule = new TransformerRule(
61 1
            'equalTo',
62 1
            FormHelper::generateCssSelector($primaryView),
63 1
            $invalidMessage,
64 1
            array( new FieldDependency($primaryView) )
65 1
        );
66 1
        foreach ($keys as $childName) {
67 1
            $childCollection = new RuleCollection();
68 1
            $childCollection->set('equalTo', $equalToPrimaryRule);
69
70 1
            $collection->add(
71 1
                $formView->children[$childName],
72
                $childCollection
73 1
            );
74 1
        }
75 1
    }
76
77 2
    private function getKeys($transformer)
78
    {
79 2
        if ($this->keyReflectionCache === null) {
80
            // Using reflection since we want to support more then just the repeated form type.
81 2
            $reflection = new \ReflectionProperty(get_class($transformer), 'keys');
82 2
            $reflection->setAccessible(true);
83 2
            $this->keyReflectionCache = $reflection;
84 2
        }
85
86 2
        return $this->keyReflectionCache->getValue($transformer);
87
    }
88
}
89