Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c78a9b0

Browse filesBrowse files
committed
[Validator] Added ConstraintValidator::buildViolation() helper for BC with 2.4 API
1 parent 1c254a4 commit c78a9b0
Copy full SHA for c78a9b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

41 files changed

+692
-411
lines changed

‎src/Symfony/Component/Validator/ConstraintValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ConstraintValidator.php
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace Symfony\Component\Validator;
1313

14+
use Symfony\Component\Validator\Context\ExecutionContextInterface as ExecutionContextInterface2Dot5;
15+
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
16+
use Symfony\Component\Validator\Violation\LegacyConstraintViolationBuilder;
17+
1418
/**
1519
* Base class for constraint validators
1620
*
@@ -48,6 +52,47 @@ public function initialize(ExecutionContextInterface $context)
4852
$this->context = $context;
4953
}
5054

55+
/**
56+
* Wrapper for {@link ExecutionContextInterface::buildViolation} that
57+
* supports the 2.4 context API.
58+
*
59+
* @param string $message The violation message
60+
* @param array $parameters The message parameters
61+
*
62+
* @return ConstraintViolationBuilderInterface The violation builder
63+
*
64+
* @deprecated This method will be removed in Symfony 3.0.
65+
*/
66+
protected function buildViolation($message, array $parameters = array())
67+
{
68+
if ($this->context instanceof ExecutionContextInterface2Dot5) {
69+
return $this->context->buildViolation($message, $parameters);
70+
}
71+
72+
return new LegacyConstraintViolationBuilder($this->context, $message, $parameters);
73+
}
74+
75+
/**
76+
* Wrapper for {@link ExecutionContextInterface::buildViolation} that
77+
* supports the 2.4 context API.
78+
*
79+
* @param ExecutionContextInterface $context The context to use
80+
* @param string $message The violation message
81+
* @param array $parameters The message parameters
82+
*
83+
* @return ConstraintViolationBuilderInterface The violation builder
84+
*
85+
* @deprecated This method will be removed in Symfony 3.0.
86+
*/
87+
protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array())
88+
{
89+
if ($context instanceof ExecutionContextInterface2Dot5) {
90+
return $context->buildViolation($message, $parameters);
91+
}
92+
93+
return new LegacyConstraintViolationBuilder($context, $message, $parameters);
94+
}
95+
5196
/**
5297
* Returns a string representation of the type of the value.
5398
*

‎src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php
+9-6Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Provides a base class for the validation of property comparisons.
2020
*
2121
* @author Daniel Holmes <daniel@danielholmes.org>
22+
* @author Bernhard Schussek <bschussek@gmail.com>
2223
*/
2324
abstract class AbstractComparisonValidator extends ConstraintValidator
2425
{
@@ -35,12 +36,14 @@ public function validate($value, Constraint $constraint)
3536
return;
3637
}
3738

38-
if (!$this->compareValues($value, $constraint->value)) {
39-
$this->context->addViolation($constraint->message, array(
40-
'{{ value }}' => $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
41-
'{{ compared_value }}' => $this->formatValue($constraint->value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
42-
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value),
43-
));
39+
$comparedValue = $constraint->value;
40+
41+
if (!$this->compareValues($value, $comparedValue)) {
42+
$this->buildViolation($constraint->message)
43+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
44+
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
45+
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
46+
->addViolation();
4447
}
4548
}
4649

‎src/Symfony/Component/Validator/Constraints/BlankValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/BlankValidator.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function validate($value, Constraint $constraint)
3232
}
3333

3434
if ('' !== $value && null !== $value) {
35-
$this->context->addViolation($constraint->message, array(
36-
'{{ value }}' => $this->formatValue($value),
37-
));
35+
$this->buildViolation($constraint->message)
36+
->setParameter('{{ value }}', $this->formatValue($value))
37+
->addViolation();
3838
}
3939
}
4040
}

‎src/Symfony/Component/Validator/Constraints/CardScheme.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CardScheme.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*
1919
* @Annotation
2020
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
21+
*
22+
* @author Tim Nagel <t.nagel@infinite.net.au>
2123
*/
2224
class CardScheme extends Constraint
2325
{

‎src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php
+9-7Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
/**
1919
* Validates that a card number belongs to a specified scheme.
2020
*
21+
* @author Tim Nagel <t.nagel@infinite.net.au>
22+
* @author Bernhard Schussek <bschussek@gmail.com>
23+
*
2124
* @see http://en.wikipedia.org/wiki/Bank_card_number
2225
* @see http://www.regular-expressions.info/creditcard.html
23-
* @author Tim Nagel <t.nagel@infinite.net.au>
2426
*/
2527
class CardSchemeValidator extends ConstraintValidator
2628
{
@@ -113,9 +115,9 @@ public function validate($value, Constraint $constraint)
113115
}
114116

115117
if (!is_numeric($value)) {
116-
$this->context->addViolation($constraint->message, array(
117-
'{{ value }}' => $this->formatValue($value),
118-
));
118+
$this->buildViolation($constraint->message)
119+
->setParameter('{{ value }}', $this->formatValue($value))
120+
->addViolation();
119121

120122
return;
121123
}
@@ -131,8 +133,8 @@ public function validate($value, Constraint $constraint)
131133
}
132134
}
133135

134-
$this->context->addViolation($constraint->message, array(
135-
'{{ value }}' => $this->formatValue($value),
136-
));
136+
$this->buildViolation($constraint->message)
137+
->setParameter('{{ value }}', $this->formatValue($value))
138+
->addViolation();
137139
}
138140
}

‎src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/ChoiceValidator.php
+17-43Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1716
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1817
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1918

@@ -64,63 +63,38 @@ public function validate($value, Constraint $constraint)
6463
if ($constraint->multiple) {
6564
foreach ($value as $_value) {
6665
if (!in_array($_value, $choices, $constraint->strict)) {
67-
if ($this->context instanceof ExecutionContextInterface) {
68-
$this->context->buildViolation($constraint->multipleMessage)
69-
->setParameter('{{ value }}', $this->formatValue($_value))
70-
->addViolation();
71-
} else {
72-
// 2.4 API
73-
$this->context->addViolation($constraint->multipleMessage, array(
74-
'{{ value }}' => $this->formatValue($_value),
75-
));
76-
}
66+
$this->buildViolation($constraint->multipleMessage)
67+
->setParameter('{{ value }}', $this->formatValue($_value))
68+
->setInvalidValue($_value)
69+
->addViolation();
70+
71+
return;
7772
}
7873
}
7974

8075
$count = count($value);
8176

8277
if ($constraint->min !== null && $count < $constraint->min) {
83-
if ($this->context instanceof ExecutionContextInterface) {
84-
$this->context->buildViolation($constraint->minMessage)
85-
->setParameter('{{ limit }}', $constraint->min)
86-
->setPlural((int) $constraint->min)
87-
->addViolation();
88-
} else {
89-
// 2.4 API
90-
$this->context->addViolation($constraint->minMessage, array(
91-
'{{ limit }}' => $constraint->min,
92-
), $value, (int) $constraint->min);
93-
}
78+
$this->buildViolation($constraint->minMessage)
79+
->setParameter('{{ limit }}', $constraint->min)
80+
->setPlural((int) $constraint->min)
81+
->addViolation();
9482

9583
return;
9684
}
9785

9886
if ($constraint->max !== null && $count > $constraint->max) {
99-
if ($this->context instanceof ExecutionContextInterface) {
100-
$this->context->buildViolation($constraint->maxMessage)
101-
->setParameter('{{ limit }}', $constraint->max)
102-
->setPlural((int) $constraint->max)
103-
->addViolation();
104-
} else {
105-
// 2.4 API
106-
$this->context->addViolation($constraint->maxMessage, array(
107-
'{{ limit }}' => $constraint->max,
108-
), $value, (int) $constraint->max);
109-
}
87+
$this->buildViolation($constraint->maxMessage)
88+
->setParameter('{{ limit }}', $constraint->max)
89+
->setPlural((int) $constraint->max)
90+
->addViolation();
11091

11192
return;
11293
}
11394
} elseif (!in_array($value, $choices, $constraint->strict)) {
114-
if ($this->context instanceof ExecutionContextInterface) {
115-
$this->context->buildViolation($constraint->message)
116-
->setParameter('{{ value }}', $this->formatValue($value))
117-
->addViolation();
118-
} else {
119-
// 2.4 API
120-
$this->context->addViolation($constraint->message, array(
121-
'{{ value }}' => $this->formatValue($value),
122-
));
123-
}
95+
$this->buildViolation($constraint->message)
96+
->setParameter('{{ value }}', $this->formatValue($value))
97+
->addViolation();
12498
}
12599
}
126100
}

‎src/Symfony/Component/Validator/Constraints/CollectionValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CollectionValidator.php
+10-24Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,22 @@ public function validate($value, Constraint $constraint)
7070
}
7171
}
7272
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
73-
if ($context instanceof ExecutionContextInterface) {
74-
$context->buildViolation($constraint->missingFieldsMessage)
75-
->atPath('['.$field.']')
76-
->setParameter('{{ field }}', $this->formatValue($field))
77-
->setInvalidValue(null)
78-
->addViolation();
79-
} else {
80-
// 2.4 API
81-
$context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
82-
'{{ field }}' => $this->formatValue($field),
83-
), null);
84-
}
73+
$this->buildViolationInContext($context, $constraint->missingFieldsMessage)
74+
->atPath('['.$field.']')
75+
->setParameter('{{ field }}', $this->formatValue($field))
76+
->setInvalidValue(null)
77+
->addViolation();
8578
}
8679
}
8780

8881
if (!$constraint->allowExtraFields) {
8982
foreach ($value as $field => $fieldValue) {
9083
if (!isset($constraint->fields[$field])) {
91-
if ($context instanceof ExecutionContextInterface) {
92-
$context->buildViolation($constraint->extraFieldsMessage)
93-
->atPath('['.$field.']')
94-
->setParameter('{{ field }}', $this->formatValue($field))
95-
->setInvalidValue($fieldValue)
96-
->addViolation();
97-
} else {
98-
// 2.4 API
99-
$context->addViolationAt('['.$field.']', $constraint->extraFieldsMessage, array(
100-
'{{ field }}' => $this->formatValue($field),
101-
), $fieldValue);
102-
}
84+
$this->buildViolationInContext($context, $constraint->extraFieldsMessage)
85+
->atPath('['.$field.']')
86+
->setParameter('{{ field }}', $this->formatValue($field))
87+
->setInvalidValue($fieldValue)
88+
->addViolation();
10389
}
10490
}
10591
}

‎src/Symfony/Component/Validator/Constraints/CountValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CountValidator.php
+12-48Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1716
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1817

1918
/**
@@ -36,59 +35,24 @@ public function validate($value, Constraint $constraint)
3635

3736
$count = count($value);
3837

39-
if ($constraint->min == $constraint->max && $count != $constraint->min) {
40-
if ($this->context instanceof ExecutionContextInterface) {
41-
$this->context->buildViolation($constraint->exactMessage)
42-
->setParameter('{{ count }}', $count)
43-
->setParameter('{{ limit }}', $constraint->min)
44-
->setInvalidValue($value)
45-
->setPlural((int) $constraint->min)
46-
->addViolation();
47-
} else {
48-
// 2.4 API
49-
$this->context->addViolation($constraint->exactMessage, array(
50-
'{{ count }}' => $count,
51-
'{{ limit }}' => $constraint->min,
52-
), $value, (int) $constraint->min);
53-
}
54-
55-
return;
56-
}
57-
5838
if (null !== $constraint->max && $count > $constraint->max) {
59-
if ($this->context instanceof ExecutionContextInterface) {
60-
$this->context->buildViolation($constraint->maxMessage)
61-
->setParameter('{{ count }}', $count)
62-
->setParameter('{{ limit }}', $constraint->max)
63-
->setInvalidValue($value)
64-
->setPlural((int) $constraint->max)
65-
->addViolation();
66-
} else {
67-
// 2.4 API
68-
$this->context->addViolation($constraint->maxMessage, array(
69-
'{{ count }}' => $count,
70-
'{{ limit }}' => $constraint->max,
71-
), $value, (int) $constraint->max);
72-
}
39+
$this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
40+
->setParameter('{{ count }}', $count)
41+
->setParameter('{{ limit }}', $constraint->max)
42+
->setInvalidValue($value)
43+
->setPlural((int) $constraint->max)
44+
->addViolation();
7345

7446
return;
7547
}
7648

7749
if (null !== $constraint->min && $count < $constraint->min) {
78-
if ($this->context instanceof ExecutionContextInterface) {
79-
$this->context->buildViolation($constraint->minMessage)
80-
->setParameter('{{ count }}', $count)
81-
->setParameter('{{ limit }}', $constraint->min)
82-
->setInvalidValue($value)
83-
->setPlural((int) $constraint->min)
84-
->addViolation();
85-
} else {
86-
// 2.4 API
87-
$this->context->addViolation($constraint->minMessage, array(
88-
'{{ count }}' => $count,
89-
'{{ limit }}' => $constraint->min,
90-
), $value, (int) $constraint->min);
91-
}
50+
$this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
51+
->setParameter('{{ count }}', $count)
52+
->setParameter('{{ limit }}', $constraint->min)
53+
->setInvalidValue($value)
54+
->setPlural((int) $constraint->min)
55+
->addViolation();
9256
}
9357
}
9458
}

‎src/Symfony/Component/Validator/Constraints/CountryValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CountryValidator.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public function validate($value, Constraint $constraint)
4646
$countries = Intl::getRegionBundle()->getCountryNames();
4747

4848
if (!isset($countries[$value])) {
49-
$this->context->addViolation($constraint->message, array(
50-
'{{ value }}' => $this->formatValue($value),
51-
));
49+
$this->buildViolation($constraint->message)
50+
->setParameter('{{ value }}', $this->formatValue($value))
51+
->addViolation();
5252
}
5353
}
5454
}

‎src/Symfony/Component/Validator/Constraints/CurrencyValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CurrencyValidator.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public function validate($value, Constraint $constraint)
4646
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();
4747

4848
if (!isset($currencies[$value])) {
49-
$this->context->addViolation($constraint->message, array(
50-
'{{ value }}' => $this->formatValue($value),
51-
));
49+
$this->buildViolation($constraint->message)
50+
->setParameter('{{ value }}', $this->formatValue($value))
51+
->addViolation();
5252
}
5353
}
5454
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.