Completed
Push — master ( 84be1e...ae3369 )
by Roman
07:05
created

UnobtrusiveValidationExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace RomanTymoshyk\UnobtrusiveValidationBundle\Form\Extension;
3
4
use Symfony\Component\Form\AbstractTypeExtension;
5
use Symfony\Component\Form\FormInterface;
6
use Symfony\Component\Form\FormView;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use Symfony\Component\Translation\TranslatorInterface;
9
use Symfony\Component\Validator\Constraint;
10
11
class UnobtrusiveValidationExtension extends AbstractTypeExtension
12
{
13
    /**
14
     * @var TranslatorInterface
15
     */
16
    protected $translator;
17
18
    /**
19
     * @var null|string
20
     */
21
    private $translationDomain;
22
23
    /**
24
     * @param TranslatorInterface $translator The translator for translating error messages
25
     * @param null|string $translationDomain The translation domain for translating
26
     */
27
    public function __construct(TranslatorInterface $translator, $translationDomain = null)
28
    {
29
        $this->translator = $translator;
30
        $this->translationDomain = $translationDomain;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function configureOptions(OptionsResolver $resolver)
37
    {
38
        parent::configureOptions($resolver);
39
40
        $resolver->setDefaults(
41
            array(
42
                'upload_max_size_message' => 'test'
43
            )
44
        );
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function buildView(FormView $view, FormInterface $form, array $options)
51
    {
52
        if (count($options['constraints']) > 0) {
53
            $view->vars['attr']['data-val'] = 'true';
54
55
            $label = $this->trans($options['label']);
56
57
            /** @var Constraint $constraint */
58
            foreach ($options['constraints'] as $constraint) {
59
                switch (get_class($constraint)) {
60
                    case 'Symfony\Component\Validator\Constraints\Required':
61
                        $view->vars['attr']['data-val-required'] = $this->trans(
62
                            'The \'{{ field_name }}\' field is required.',
63
                            array('{{ field_name }}' => $label)
64
                        );
65
                        break;
66
                    case 'Symfony\Component\Validator\Constraints\Regex':
67
                        $view->vars['attr']['data-val-regex'] = $this->trans(
68
                            'The \'{{ field_name }}\' value is not valid',
69
                            array('{{ field_name }}' => $label)
70
                        );
71
                        $view->vars['attr']['data-val-regex-pattern'] = $constraint->htmlPattern;
72
                        break;
73
                    case 'Symfony\Component\Validator\Constraints\Range':
74
                        if (!empty($constraint->min)) {
75
                            $view->vars['attr']['data-val-range-min'] = $constraint->min;
76
                            $view->vars['attr']['data-val-range'] = $this->trans(
77
                                'The field \'{{ field_name }}\' value should be {{ limit }} or more.',
78
                                array(
79
                                    '{{ field_name }}' => $label,
80
                                    '{{ limit }}' => $constraint->min
81
                                )
82
                            );
83
                        }
84
                        if (!empty($constraint->max)) {
85
                            $view->vars['attr']['data-val-range-max'] = $constraint->max;
86
                            $view->vars['attr']['data-val-range'] = $this->trans(
87
                                'The field \'{{ field_name }}\' value should be {{ limit }} or less.',
88
                                array(
89
                                    '{{ field_name }}' => $label,
90
                                    '{{ limit }}' => $constraint->max
91
                                )
92
                            );
93
                        }
94
95
                        if (!empty($constraint->min) && !empty($constraint->max)) {
96
                            $view->vars['attr']['data-val-range'] = $this->trans(
97
                                'The field \'{{ field_name }}\' value should be in range {{ min }} to {{ max }}.',
98
                                array(
99
                                    '{{ field_name }}' => $label,
100
                                    '{{ min }}' => $constraint->min,
101
                                    '{{ max }}' => $constraint->max
102
                                )
103
                            );
104
                        }
105
                        break;
106
                    case 'Symfony\Component\Validator\Constraints\Length':
107
                        if (!empty($constraint->min)) {
108
                            $view->vars['attr']['data-val-length-min'] = $constraint->min;
109
                            $view->vars['attr']['data-val-length'] = $this->trans(
110
                                'The field \'{{ field_name }}\' should have {{ limit }} or more.',
111
                                array(
112
                                    '{{ field_name }}' => $label,
113
                                    '{{ limit }}' => $constraint->min
114
                                )
115
                            );
116
                        }
117
118
                        if (!empty($constraint->max)) {
119
                            $view->vars['attr']['data-val-length-max'] = $constraint->max;
120
                            $view->vars['attr']['data-val-length'] = $this->trans(
121
                                'The field \'{{ field_name }}\' should have {{ limit }} or less.',
122
                                array(
123
                                    '{{ field_name }}' => $label,
124
                                    '{{ limit }}' => $constraint->max
125
                                )
126
                            );
127
                        }
128
129
                        if (!empty($constraint->min) && !empty($constraint->max)) {
130
                            $view->vars['attr']['data-val-length'] = $this->trans(
131
                                'The field \'{{ field_name }}\' should have from {{ min }} to {{ max }} characters.',
132
                                array(
133
                                    '{{ field_name }}' => $label,
134
                                    '{{ min }}' => $constraint->min,
135
                                    '{{ max }}' => $constraint->max
136
                                )
137
                            );
138
                        }
139
                        break;
140
                    case 'Symfony\Component\Validator\Constraints\Type':
141
                        switch ($constraint->type) {
142
                            case 'integer':
143
                            case 'int':
144
                            case 'long':
145
                                $view->vars['attr']['data-val-digits'] = $this->trans(
146
                                    'The \'{{ field_name }}\' field value is not a valid integer.',
147
                                    array('{{ field_name }}' => $label)
148
                                );
149
                                break;
150
                            case 'float':
151
                            case 'numeric':
152
                            case 'real':
153
                                $view->vars['attr']['data-val-number'] = $this->trans(
154
                                    'The \'{{ field_name }}\' field value is not a valid number.',
155
                                    array('{{ field_name }}' => $label)
156
                                );
157
                                break;
158
                        }
159
                        break;
160
                    case 'Symfony\Component\Validator\Constraints\Date':
161
                        $view->vars['attr']['data-val-date'] = $this->trans(
162
                            'The \'{{ field_name }}\' field value is not a valid date.',
163
                            array('{{ field_name }}' => $label)
164
                        );
165
                        break;
166
                    case 'Symfony\Component\Validator\Constraints\Email':
167
                        $view->vars['attr']['data-val-email'] = $this->trans(
168
                            'The \'{{ field_name }}\' field value is not a valid email address.',
169
                            array('{{ field_name }}' => $label)
170
                        );
171
                        break;
172
                    case 'Symfony\Component\Validator\Constraints\CardScheme':
173
                        $view->vars['attr']['data-val-creditcard'] = $this->trans(
174
                            'The \'{{ field_name }}\' field value is not a valid credit card number.',
175
                            array('{{ field_name }}' => $label)
176
                        );
177
                        break;
178
                    case 'Symfony\Component\Validator\Constraints\Url':
179
                        $view->vars['attr']['data-val-url'] = $this->trans(
180
                            'The \'{{ field_name }}\' field value is not a valid url.',
181
                            array('{{ field_name }}' => $label)
182
                        );
183
                        break;
184
                }
185
            }
186
        }
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function getExtendedType()
193
    {
194
        return 'Symfony\Component\Form\Extension\Core\Type\FormType';
195
    }
196
197
    /**
198
     * Translates the given message.
199
     *
200
     * @param string $id The message id (may also be an object that can be cast to string)
201
     * @param array $parameters An array of parameters for the message
202
     * @param string|null $domain The domain for the message or null to use the default
0 ignored issues
show
Bug introduced by
There is no parameter named $domain. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
203
     * @param string|null $locale The locale or null to use the default
0 ignored issues
show
Bug introduced by
There is no parameter named $locale. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
204
     *
205
     * @return string The translated string
206
     */
207
    public function trans($id, array $parameters = array())
208
    {
209
        return $this->translator !== null && $this->translationDomain !== false ? $this->translator->trans(
210
            $id,
211
            $parameters,
212
            $this->translationDomain
213
        ) : $id;
214
    }
215
216
    /**
217
     * Translates the given choice message by choosing a translation according to a number.
218
     *
219
     * @param string $id The message id (may also be an object that can be cast to string)
220
     * @param int $number The number to use to find the indice of the message
221
     * @param array $parameters An array of parameters for the message
222
     * @param string|null $domain The domain for the message or null to use the default
0 ignored issues
show
Bug introduced by
There is no parameter named $domain. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
223
     * @param string|null $locale The locale or null to use the default
0 ignored issues
show
Bug introduced by
There is no parameter named $locale. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
224
     *
225
     * @return string The translated string
226
     */
227
    public function transChoice($id, $number, array $parameters = array())
228
    {
229
        return $this->translator !== null && $this->translationDomain !== false ? $this->translator->transChoice(
230
            $id,
231
            $number,
232
            $parameters,
233
            $this->translationDomain
234
        ) : $id;
235
    }
236
}