JqueryValidationExtension::fieldRulesViewData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
namespace Boekkooi\Bundle\JqueryValidationBundle\Twig;
3
4
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContext;
5
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
6
use Symfony\Component\Form\FormView;
7
use Twig_Extension;
8
use Twig_SimpleFunction;
9
10
/**
11
 * @author Warnar Boekkooi <[email protected]>
12
 */
13
class JqueryValidationExtension extends Twig_Extension
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18
    public function getFunctions()
19
    {
20
        return array(
21
             new Twig_SimpleFunction(
22
                'form_jquery_validation',
23
                array($this, 'renderJavascript'),
24
                array('needs_environment' => true, 'pre_escape' => array('html', 'js'), 'is_safe' => array('html', 'js'))
25
            ),
26
        );
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function getName()
33
    {
34
        return 'boekkooi_jquery_validation';
35
    }
36
37
    public function renderJavascript(\Twig_Environment $twig, FormView $view)
38
    {
39
        if (!isset($view->vars['rule_context'])) {
40
            return '';
41
        }
42
        /** @var \Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContext $rootContext */
43
        $template = 'BoekkooiJqueryValidationBundle:Form:form_validate.js.twig';
44
        $rootContext = $context = $view->vars['rule_context'];
45
        $rootView = $view;
46
47
        // The given view is not the root form
48
        if ($view->parent !== null) {
49
            $template = 'BoekkooiJqueryValidationBundle:Form:dynamic_validate.js.twig';
50
            $rootView = FormHelper::getViewRoot($view);
51
            $rootContext = $rootView->vars['rule_context'];
52
        }
53
54
        // Create template variables
55
        $templateVars = array(
56
            'form' => $rootView,
57
            'fields' => $this->fieldRulesViewData($context),
58
            'validation_groups' => $this->validationGroupsViewData($rootContext),
59
        );
60
        $templateVars['enforce_validation_groups'] = count($templateVars['validation_groups']) > 1;
61
        $templateVars['enabled_validation_groups'] = count($rootContext->getButtons()) === 0 ? $templateVars['validation_groups'] : array();
62
63
        // Only add buttons from the root form
64
        if ($view->parent === null) {
65
            $templateVars['buttons'] = $this->buttonsViewData($context);
66
        }
67
68
        $js = $twig->render($template, $templateVars);
69
70
        return preg_replace('/\s+/', ' ', $js);
71
    }
72
73
    protected function validationGroupsViewData(FormRuleContext $context)
74
    {
75
        $it = new \RecursiveIteratorIterator(
76
            new \RecursiveArrayIterator($context->getGroups())
77
        );
78
79
        return array_unique(array_filter(iterator_to_array($it, false)));
80
    }
81
82
    /**
83
     * Transform the buttons in the given form into a array that can easily be used by twig.
84
     *
85
     * @param FormRuleContext $context
86
     * @return array
87
     */
88
    protected function buttonsViewData(FormRuleContext $context)
89
    {
90
        $buttonNames = $context->getButtons();
91
92
        $buttons = array();
93
        foreach ($buttonNames as $name) {
94
            $groups = $context->getGroup($name);
95
96
            $buttons[] = array(
97
                'name' => $name,
98
                'cancel' => count($groups) === 0,
99
                'validation_groups' => $groups,
100
            );
101
        }
102
103
        return $buttons;
104
    }
105
106
    protected function fieldRulesViewData(FormRuleContext $context)
107
    {
108
        $fields = array();
109
        foreach ($context->all() as $name => $rules) {
110
            $fields[] = array(
111
                'name' => $name,
112
                'rules' => $rules,
113
            );
114
        }
115
116
        return $fields;
117
    }
118
}
119