Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit6f94e5d

Browse files
committed
[Validator] Added ConstraintValidator::buildViolation() helper for BC with 2.4 API
1 parent1c254a4 commit6f94e5d

File tree

49 files changed

+832
-585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+832
-585
lines changed

‎src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,9 @@ public function validate($entity, Constraint $constraint)
137137

138138
$errorPath =null !==$constraint->errorPath ?$constraint->errorPath :$fields[0];
139139

140-
if ($this->contextinstanceof ExecutionContextInterface) {
141-
$this->context->buildViolation($constraint->message)
142-
->atPath($errorPath)
143-
->setInvalidValue($criteria[$fields[0]])
144-
->addViolation();
145-
}else {
146-
// 2.4 API
147-
$this->context->addViolationAt($errorPath,$constraint->message,array(),$criteria[$fields[0]]);
148-
}
140+
$this->buildViolation($constraint->message)
141+
->atPath($errorPath)
142+
->setInvalidValue($criteria[$fields[0]])
143+
->addViolation();
149144
}
150145
}

‎src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php‎

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -116,40 +116,20 @@ public function validate($form, Constraint $constraint)
116116
? (string)$form->getViewData()
117117
:gettype($form->getViewData());
118118

119-
if ($this->contextinstanceof ExecutionContextInterface) {
120-
$this->context->buildViolation($config->getOption('invalid_message'))
121-
->setParameters(array_replace(array('{{ value }}' =>$clientDataAsString),$config->getOption('invalid_message_parameters')))
122-
->setInvalidValue($form->getViewData())
123-
->setCode(Form::ERR_INVALID)
124-
->addViolation();
125-
}else {
126-
// 2.4 API
127-
$this->context->addViolation(
128-
$config->getOption('invalid_message'),
129-
array_replace(array('{{ value }}' =>$clientDataAsString),$config->getOption('invalid_message_parameters')),
130-
$form->getViewData(),
131-
null,
132-
Form::ERR_INVALID
133-
);
134-
}
119+
$this->buildViolation($config->getOption('invalid_message'))
120+
->setParameters(array_replace(array('{{ value }}' =>$clientDataAsString),$config->getOption('invalid_message_parameters')))
121+
->setInvalidValue($form->getViewData())
122+
->setCode(Form::ERR_INVALID)
123+
->addViolation();
135124
}
136125
}
137126

138127
// Mark the form with an error if it contains extra fields
139128
if (count($form->getExtraData()) >0) {
140-
if ($this->contextinstanceof ExecutionContextInterface) {
141-
$this->context->buildViolation($config->getOption('extra_fields_message'))
142-
->setParameter('{{ extra_fields }}',implode('", "',array_keys($form->getExtraData())))
143-
->setInvalidValue($form->getExtraData())
144-
->addViolation();
145-
}else {
146-
// 2.4 API
147-
$this->context->addViolation(
148-
$config->getOption('extra_fields_message'),
149-
array('{{ extra_fields }}' =>implode('", "',array_keys($form->getExtraData()))),
150-
$form->getExtraData()
151-
);
152-
}
129+
$this->buildViolation($config->getOption('extra_fields_message'))
130+
->setParameter('{{ extra_fields }}',implode('", "',array_keys($form->getExtraData())))
131+
->setInvalidValue($form->getExtraData())
132+
->addViolation();
153133
}
154134

155135
// Mark the form with an error if the uploaded size was too large
@@ -159,19 +139,10 @@ public function validate($form, Constraint $constraint)
159139
$max =$this->serverParams->getPostMaxSize();
160140

161141
if (!empty($max) &&$length >$max) {
162-
if ($this->contextinstanceof ExecutionContextInterface) {
163-
$this->context->buildViolation($config->getOption('post_max_size_message'))
164-
->setParameter('{{ max }}',$this->serverParams->getNormalizedIniPostMaxSize())
165-
->setInvalidValue($length)
166-
->addViolation();
167-
}else {
168-
// 2.4 API
169-
$this->context->addViolation(
170-
$config->getOption('post_max_size_message'),
171-
array('{{ max }}' =>$this->serverParams->getNormalizedIniPostMaxSize()),
172-
$length
173-
);
174-
}
142+
$this->buildViolation($config->getOption('post_max_size_message'))
143+
->setParameter('{{ max }}',$this->serverParams->getNormalizedIniPostMaxSize())
144+
->setInvalidValue($length)
145+
->addViolation();
175146
}
176147
}
177148
}

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

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespaceSymfony\Component\Validator;
1313

14+
useSymfony\Component\Validator\Context\ExecutionContextInterfaceasExecutionContextInterface2Dot5;
15+
useSymfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
16+
useSymfony\Component\Validator\Violation\LegacyConstraintViolationBuilder;
17+
1418
/**
1519
* Base class for constraint validators
1620
*
@@ -24,14 +28,14 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
2428
* Whether to format {@link \DateTime} objects as RFC-3339 dates
2529
* ("Y-m-d H:i:s").
2630
*
27-
* @varinteger
31+
* @varint
2832
*/
2933
constPRETTY_DATE =1;
3034

3135
/**
3236
* Whether to cast objects with a "__toString()" method to strings.
3337
*
34-
* @varinteger
38+
* @varint
3539
*/
3640
constOBJECT_TO_STRING =2;
3741

@@ -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+
protectedfunctionbuildViolation($message,array$parameters =array())
67+
{
68+
if ($this->contextinstanceof ExecutionContextInterface2Dot5) {
69+
return$this->context->buildViolation($message,$parameters);
70+
}
71+
72+
returnnewLegacyConstraintViolationBuilder($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+
protectedfunctionbuildViolationInContext(ExecutionContextInterface$context,$message,array$parameters =array())
88+
{
89+
if ($contextinstanceof ExecutionContextInterface2Dot5) {
90+
return$context->buildViolation($message,$parameters);
91+
}
92+
93+
returnnewLegacyConstraintViolationBuilder($context,$message,$parameters);
94+
}
95+
5196
/**
5297
* Returns a string representation of the type of the value.
5398
*
@@ -82,7 +127,7 @@ protected function formatTypeOf($value)
82127
* confused by the violation message.
83128
*
84129
* @param mixed $value The value to format as string
85-
* @paraminteger $format A bitwise combination of the format
130+
* @paramint $format A bitwise combination of the format
86131
* constants in this class
87132
*
88133
* @return string The string representation of the passed value
@@ -142,7 +187,7 @@ protected function formatValue($value, $format = 0)
142187
* {@link formatValue()}. The values are then concatenated with commas.
143188
*
144189
* @param array $values A list of values
145-
* @paraminteger $format A bitwise combination of the format
190+
* @paramint $format A bitwise combination of the format
146191
* constants in this class
147192
*
148193
* @return string The string representation of the value list

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

Lines 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
abstractclass AbstractComparisonValidatorextends 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‎

Lines 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‎

Lines 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 CardSchemeextends Constraint
2325
{

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

Lines 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 CardSchemeValidatorextends 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‎

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

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

@@ -64,63 +63,38 @@ public function validate($value, Constraint $constraint)
6463
if ($constraint->multiple) {
6564
foreach ($valueas$_value) {
6665
if (!in_array($_value,$choices,$constraint->strict)) {
67-
if ($this->contextinstanceof 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->contextinstanceof 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->contextinstanceof 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->contextinstanceof 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
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp