FormHelper::isSymfony3Compatible()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Util;
3
4
use Boekkooi\Bundle\JqueryValidationBundle\Exception\InvalidArgumentException;
5
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException;
6
use Boekkooi\Bundle\JqueryValidationBundle\Exception\UnsupportedException;
7
use Symfony\Component\Form\AbstractType;
8
use Symfony\Component\Form\FormInterface;
9
use Symfony\Component\Form\FormView;
10
use Symfony\Component\Form\ResolvedFormTypeInterface;
11
use Symfony\Component\Validator\Constraint;
12
13
/**
14
 * @author Warnar Boekkooi <[email protected]>
15
 */
16
final class FormHelper
17
{
18 3
    public static function isSymfony3Compatible()
19
    {
20 3
        return method_exists(AbstractType::class, 'getBlockPrefix');
21
    }
22
23 3
    public static function isSymfony2Compatible()
24
    {
25 3
        return method_exists(ResolvedFormTypeInterface::class, 'getName');
26
    }
27
28 3
    public static function isType(ResolvedFormTypeInterface $type, $typeName)
29
    {
30
        do {
31
            if (
32 3
                self::isSymfony3Compatible() &&
33
                get_class($type->getInnerType()) === $typeName
34 3
            ) {
35
                return true;
36
            }
37
38
            if (
39 3
                self::isSymfony2Compatible() &&
40 3
                $type->getName() === $typeName
41 3
            ) {
42 2
                return true;
43
            }
44
45 2
            $type = $type->getParent();
46 2
        } while ($type !== null);
47
48 1
        return false;
49
    }
50
51
    /**
52
     * @param FormView|string $form
53
     * @return string
54
     */
55 4
    public static function getFormName($form)
56
    {
57 4
        if ($form instanceof FormView && isset($form->vars['full_name'])) {
58 1
            return $form->vars['full_name'];
59
        }
60 3
        if (is_string($form)) {
61 1
            return $form;
62
        }
63
64 2
        throw new InvalidArgumentException('Expected form to be a string or instance of FormView with a full_name.');
65
    }
66
67
    /**
68
     * @param FormView $view
69
     * @return FormView
70
     */
71 1
    public static function getViewRoot(FormView $view)
72
    {
73 1
        $root = $view;
74 1
        while ($root->parent !== null) {
75 1
            $root = $root->parent;
76 1
        }
77
78 1
        return $root;
79
    }
80
81
    /**
82
     * Retrieve the (jquery) validation groups that are configured for the given FormInterface instance.
83
     *
84
     * @param FormInterface $form
85
     * @return array|null|false
86
     */
87 5
    public static function getValidationGroups(FormInterface $form)
88
    {
89 5
        $cfg = $form->getConfig();
90
91 5
        if ($cfg->hasOption('jquery_validation_groups')) {
92 1
            $groups = $cfg->getOption('jquery_validation_groups');
93 1
        } else {
94 4
            $groups = $cfg->getOption('validation_groups');
95
        }
96
97
        # Is the default validation group used
98 5
        if ($groups === null) {
99 1
            return array(Constraint::DEFAULT_GROUP);
100
        }
101
102
        # Is the validation suppressed
103 4
        if ($groups === false) {
104 1
            return array();
105
        }
106
107
        # Is a unsupported group used
108 3
        if (!is_string($groups) && is_callable($groups)) {
109
            throw new UnsupportedException('Callable validation_groups are not supported. Disable jquery_validation or set jquery_validation_groups');
110
        }
111
112 3
        return (array) $groups;
113
    }
114
115
    public static function generateCssSelector(FormView $view)
116
    {
117
        $vars = $view->vars;
118
        if (!empty($vars['attr']['id'])) {
119
            return trim(sprintf('#%s', $vars['attr']['id']));
120
        }
121
122
        if (isset($vars['full_name'])) {
123
            $root = $view;
124
            while ($root->parent !== null) {
125
                $root = $root->parent;
126
            }
127
128
            if ($view === $root) {
129
                return sprintf('form[name="%s"]', $vars['full_name']);
130
            }
131
132
            $formSelector = self::generateCssSelector($root);
133
134
            if ($vars['compound'] && !empty($vars['id'])) {
135
                return trim(sprintf('%s #%s', $formSelector, $vars['id']));
136
            }
137
            return trim(sprintf('%s *[name="%s"]', $formSelector, $vars['full_name']));
138
        }
139
140
        throw new LogicException();
141
    }
142
}
143