Skip to content

Commit a0b1349

Browse files
committed
Merge branch '2.5'
* 2.5: (37 commits) [Validator] Backported constraint validator tests from 2.5 [Validator] Backported constraint validator tests from 2.5 [DIC] Fixed: anonymous services are always private Fix toolbar vertical alignment. [HttpFoundation] MongoDbSessionHandler supports auto expiry via configurable expiry_field [Validator] Fix little typo in ExecutionContextInterface::buildViolation() method comments fix dependencies on HttpFoundation component [FrameworkBundle] add missing attribute to XSD Allow basic auth in url. Improve regex. Add tests. fix typos and syntax in Profiler controller method comments resolve parameters before the configs are processed add symfony/yaml suggestion to composer.json [HttpKernel] added an analyze of environment parameters for built-in server. remove volatile tests [Console] fixed style creation when providing an unknown tag option change command to which available under most unix systems add way to test command under windows fix shell command injection [Form] allowed CallbackTransformer to use callable [Process] Added process synchronization to the incremental output tests ... Conflicts: src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php src/Symfony/Component/HttpKernel/Kernel.php src/Symfony/Component/HttpKernel/composer.json src/Symfony/Component/Validator/Constraints/AllValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Constraints/LegacyAllValidator.php src/Symfony/Component/Validator/Constraints/LegacyCollectionValidator.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php
2 parents 03cb008 + 3b38440 commit a0b1349

37 files changed

+326
-630
lines changed

ConstraintValidator.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@
2020
*/
2121
abstract class ConstraintValidator implements ConstraintValidatorInterface
2222
{
23+
/**
24+
* Whether to format {@link \DateTime} objects as RFC-3339 dates
25+
* ("Y-m-d H:i:s").
26+
*
27+
* @var integer
28+
*/
29+
const PRETTY_DATE = 1;
30+
31+
/**
32+
* Whether to cast objects with a "__toString()" method to strings.
33+
*
34+
* @var integer
35+
*/
36+
const OBJECT_TO_STRING = 2;
37+
2338
/**
2439
* @var ExecutionContextInterface
2540
*/
@@ -66,15 +81,15 @@ protected function formatTypeOf($value)
6681
* won't know what an "object", "array" or "resource" is and will be
6782
* confused by the violation message.
6883
*
69-
* @param mixed $value The value to format as string
70-
* @param bool $prettyDateTime Whether to format {@link \DateTime}
71-
* objects as RFC-3339 dates ("Y-m-d H:i:s")
84+
* @param mixed $value The value to format as string
85+
* @param integer $format A bitwise combination of the format
86+
* constants in this class
7287
*
7388
* @return string The string representation of the passed value
7489
*/
75-
protected function formatValue($value, $prettyDateTime = false)
90+
protected function formatValue($value, $format = 0)
7691
{
77-
if ($prettyDateTime && $value instanceof \DateTime) {
92+
if (($format & self::PRETTY_DATE) && $value instanceof \DateTime) {
7893
if (class_exists('IntlDateFormatter')) {
7994
$locale = \Locale::getDefault();
8095
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
@@ -86,6 +101,10 @@ protected function formatValue($value, $prettyDateTime = false)
86101
}
87102

88103
if (is_object($value)) {
104+
if ($format & self::OBJECT_TO_STRING && method_exists($value, '__toString')) {
105+
return $value->__toString();
106+
}
107+
89108
return 'object';
90109
}
91110

@@ -122,18 +141,18 @@ protected function formatValue($value, $prettyDateTime = false)
122141
* Each of the values is converted to a string using
123142
* {@link formatValue()}. The values are then concatenated with commas.
124143
*
125-
* @param array $values A list of values
126-
* @param bool $prettyDateTime Whether to format {@link \DateTime}
127-
* objects as RFC-3339 dates ("Y-m-d H:i:s")
144+
* @param array $values A list of values
145+
* @param integer $format A bitwise combination of the format
146+
* constants in this class
128147
*
129148
* @return string The string representation of the value list
130149
*
131150
* @see formatValue()
132151
*/
133-
protected function formatValues(array $values, $prettyDateTime = false)
152+
protected function formatValues(array $values, $format = 0)
134153
{
135154
foreach ($values as $key => $value) {
136-
$values[$key] = $this->formatValue($value, $prettyDateTime);
155+
$values[$key] = $this->formatValue($value, $format);
137156
}
138157

139158
return implode(', ', $values);

Constraints/AbstractComparisonValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function validate($value, Constraint $constraint)
3737

3838
if (!$this->compareValues($value, $constraint->value)) {
3939
$this->context->addViolation($constraint->message, array(
40-
'{{ value }}' => $this->formatValue($value, true),
41-
'{{ compared_value }}' => $this->formatValue($constraint->value, true),
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),
4242
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
4343
));
4444
}

Constraints/AllValidator.php

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

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1718

1819
/**
@@ -40,10 +41,18 @@ public function validate($value, Constraint $constraint)
4041
}
4142

4243
$context = $this->context;
43-
$validator = $context->getValidator()->inContext($context);
4444

45-
foreach ($value as $key => $element) {
46-
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
45+
if ($context instanceof ExecutionContextInterface) {
46+
$validator = $context->getValidator()->inContext($context);
47+
48+
foreach ($value as $key => $element) {
49+
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
50+
}
51+
} else {
52+
// 2.4 API
53+
foreach ($value as $key => $element) {
54+
$context->validateValue($element, $constraint->constraints, '['.$key.']');
55+
}
4756
}
4857
}
4958
}

Constraints/ChoiceValidator.php

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

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1617
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1718
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1819

@@ -63,35 +64,63 @@ public function validate($value, Constraint $constraint)
6364
if ($constraint->multiple) {
6465
foreach ($value as $_value) {
6566
if (!in_array($_value, $choices, $constraint->strict)) {
66-
$this->context->buildViolation($constraint->multipleMessage)
67-
->setParameter('{{ value }}', $this->formatValue($_value))
68-
->addViolation();
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+
}
6977
}
7078
}
7179

7280
$count = count($value);
7381

7482
if ($constraint->min !== null && $count < $constraint->min) {
75-
$this->context->buildViolation($constraint->minMessage)
76-
->setParameter('{{ limit }}', $constraint->min)
77-
->setPlural((int) $constraint->min)
78-
->addViolation();
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+
}
7994

8095
return;
8196
}
8297

8398
if ($constraint->max !== null && $count > $constraint->max) {
84-
$this->context->buildViolation($constraint->maxMessage)
85-
->setParameter('{{ limit }}', $constraint->max)
86-
->setPlural((int) $constraint->max)
87-
->addViolation();
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+
}
88110

89111
return;
90112
}
91113
} elseif (!in_array($value, $choices, $constraint->strict)) {
92-
$this->context->buildViolation($constraint->message)
93-
->setParameter('{{ value }}', $this->formatValue($value))
94-
->addViolation();
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+
}
95124
}
96125
}
97126
}

Constraints/CollectionValidator.php

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

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1718

1819
/**
@@ -49,7 +50,6 @@ public function validate($value, Constraint $constraint)
4950
// remove the initialize() method and pass the context as last argument
5051
// to validate() instead.
5152
$context = $this->context;
52-
$validator = $context->getValidator()->inContext($context);
5353

5454
foreach ($constraint->fields as $field => $fieldConstraint) {
5555
// bug fix issue #2779
@@ -58,26 +58,47 @@ public function validate($value, Constraint $constraint)
5858

5959
if ($existsInArray || $existsInArrayAccess) {
6060
if (count($fieldConstraint->constraints) > 0) {
61-
$validator->atPath('['.$field.']')
62-
->validate($value[$field], $fieldConstraint->constraints);
61+
if ($context instanceof ExecutionContextInterface) {
62+
$context->getValidator()
63+
->inContext($context)
64+
->atPath('['.$field.']')
65+
->validate($value[$field], $fieldConstraint->constraints);
66+
} else {
67+
// 2.4 API
68+
$context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']');
69+
}
6370
}
6471
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
65-
$context->buildViolation($constraint->missingFieldsMessage)
66-
->atPath('['.$field.']')
67-
->setParameter('{{ field }}', $this->formatValue($field))
68-
->setInvalidValue(null)
69-
->addViolation();
72+
if ($context instanceof ExecutionContextInterface) {
73+
$context->buildViolation($constraint->missingFieldsMessage)
74+
->atPath('['.$field.']')
75+
->setParameter('{{ field }}', $this->formatValue($field))
76+
->setInvalidValue(null)
77+
->addViolation();
78+
} else {
79+
// 2.4 API
80+
$context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
81+
'{{ field }}' => $this->formatValue($field)
82+
), null);
83+
}
7084
}
7185
}
7286

7387
if (!$constraint->allowExtraFields) {
7488
foreach ($value as $field => $fieldValue) {
7589
if (!isset($constraint->fields[$field])) {
76-
$context->buildViolation($constraint->extraFieldsMessage)
77-
->atPath('['.$field.']')
78-
->setParameter('{{ field }}', $this->formatValue($field))
79-
->setInvalidValue($fieldValue)
80-
->addViolation();
90+
if ($context instanceof ExecutionContextInterface) {
91+
$context->buildViolation($constraint->extraFieldsMessage)
92+
->atPath('['.$field.']')
93+
->setParameter('{{ field }}', $this->formatValue($field))
94+
->setInvalidValue($fieldValue)
95+
->addViolation();
96+
} else {
97+
// 2.4 API
98+
$context->addViolationAt('['.$field.']', $constraint->extraFieldsMessage, array(
99+
'{{ field }}' => $this->formatValue($field)
100+
), $fieldValue);
101+
}
81102
}
82103
}
83104
}

Constraints/CountValidator.php

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

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1718

1819
/**
@@ -36,34 +37,58 @@ public function validate($value, Constraint $constraint)
3637
$count = count($value);
3738

3839
if ($constraint->min == $constraint->max && $count != $constraint->min) {
39-
$this->context->buildViolation($constraint->exactMessage)
40-
->setParameter('{{ count }}', $count)
41-
->setParameter('{{ limit }}', $constraint->min)
42-
->setInvalidValue($value)
43-
->setPlural((int) $constraint->min)
44-
->addViolation();
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+
}
4554

4655
return;
4756
}
4857

4958
if (null !== $constraint->max && $count > $constraint->max) {
50-
$this->context->buildViolation($constraint->maxMessage)
51-
->setParameter('{{ count }}', $count)
52-
->setParameter('{{ limit }}', $constraint->max)
53-
->setInvalidValue($value)
54-
->setPlural((int) $constraint->max)
55-
->addViolation();
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+
}
5673

5774
return;
5875
}
5976

6077
if (null !== $constraint->min && $count < $constraint->min) {
61-
$this->context->buildViolation($constraint->minMessage)
62-
->setParameter('{{ count }}', $count)
63-
->setParameter('{{ limit }}', $constraint->min)
64-
->setInvalidValue($value)
65-
->setPlural((int) $constraint->min)
66-
->addViolation();
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+
}
6792
}
6893
}
6994
}

0 commit comments

Comments
 (0)