8000 [Validator] Extracted message interpolation logic of ConstraintViolat… · symfony/symfony@46f751c · GitHub
[go: up one dir, main page]

Skip to content

Commit 46f751c

Browse files
committed
[Validator] Extracted message interpolation logic of ConstraintViolation and used the Translation component for that
1 parent 8c320b0 commit 46f751c

20 files changed

+364
-49
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ CHANGELOG
2828
As of Symfony 2.3, this method will be typed against `MetadataFactoryInterface` instead.
2929
* [BC BREAK] the switches `traverse` and `deep` in the `Valid` constraint and in `GraphWalker::walkReference`
3030
are ignored for arrays now. Arrays are always traversed recursively.
31+
* added dependency to Translation component
32+
* violation messages are now translated with a TranslatorInterface implementation
3133

3234
2.1.0
3335
-----

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
class ConstraintViolation implements ConstraintViolationInterface
2020
{
21+
/**
22+
* @var string
23+
*/
24+
private $message;
25+
2126
/**
2227
* @var string
2328
*/
@@ -56,6 +61,7 @@ class ConstraintViolation implements ConstraintViolationInterface
5661
/**
5762
* Creates a new constraint violation.
5863
*
64+
* @param string $message The violation message.
5965
* @param string $messageTemplate The raw violation message.
6066
* @param array $messageParameters The parameters to substitute
6167
* in the raw message.
@@ -70,8 +76,9 @@ class ConstraintViolation implements ConstraintViolationInterface
7076
* @param mixed $code The error code of the
7177
* violation, if any.
7278
*/
73-
public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null, $code = null)
79+
public function __construct($message, $messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null, $code = null)
7480
{
81+
$this->message = $message;
7582
$this->messageTemplate = $messageTemplate;
7683
$this->messageParameters = $messageParameters;
7784
$this->messagePluralization = $messagePluralization;
@@ -132,15 +139,7 @@ public function getMessagePluralization()
132139
*/
133140
public function getMessage()
134141
{
135-
$parameters = $this->messageParameters;
136-
137-
foreach ($parameters as $i => $parameter) {
138-
if (is_array($parameter)) {
139-
$parameters[$i] = 'Array';
140-
}
141-
}
142-
143-
return strtr($this->messageTemplate, $parameters);
142+
return $this->message;
144143
}
145144

146145
/**
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator;
13+
14+
use Symfony\Component\Translation\TranslatorInterface;
15+
16+
/**
17+
* Simple translator implementation that simply replaces the parameters in
18+
* the message IDs.
19+
*
20+
* Does not support translation domains or locales.
21+
*
22+
* @author Bernhard Schussek <bschussek@gmail.com>
23+
*/
24+
class DefaultTranslator implements TranslatorInterface
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
30+
{
31+
return strtr($id, $parameters);
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
38+
{
39+
return strtr($id, $parameters);
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function setLocale($locale)
46+
{
47+
throw new \BadMethodCallException('Unsupported method.');
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function getLocale()
54+
{
55+
throw new \BadMethodCallException('Unsupported method.');
56+
}
57+
}

src/Symfony/Component/Validator/ExecutionContext.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Validator;
1313

14+
use Symfony\Component\Translation\TranslatorInterface;
15+
1416
/**
1517
* Default implementation of {@link ExecutionContextInterface}.
1618
*
@@ -26,6 +28,16 @@ class ExecutionContext implements ExecutionContextInterface
2628
*/
2729
private $globalContext;
2830

31+
/**
32+
* @var TranslatorInterface
33+
*/
34+
private $translator;
35+
36+
/**
37+
* @var null|string
38+
*/
39+
private $translationDomain;
40+
2941
/**
3042
* @var MetadataInterface
3143
*/
@@ -49,19 +61,23 @@ class ExecutionContext implements ExecutionContextInterface
4961
/**
5062
* Creates a new execution context.
5163
*
52-
* @param GlobalExecutionContextInterface $globalContext The global context storing node-independent state.
53-
* @param MetadataInterface $metadata The metadata of the validated node.
54-
* @param mixed $value The value of the validated node.
55-
* @param string $group The current validation group.
56-
* @param string $propertyPath The property path to the current node.
64+
* @param GlobalExecutionContextInterface $globalContext The global context storing node-independent state.
65+
* @param TranslatorInterface $translator The translator for translating violation messages.
66+
* @param null|string $translationDomain The domain of the validation messages.
67+
* @param MetadataInterface $metadata The metadata of the validated node.
68+
* @param mixed $value The value of the validated node.
69+
* @param string $group The current validation group.
70+
* @param string $propertyPath The property path to the current node.
5771
*/
58-
public function __construct(GlobalExecutionContextInterface $globalContext, MetadataInterface $metadata = null, $value = null, $group = null, $propertyPath = '')
72+
public function __construct(GlobalExecutionContextInterface $globalContext, TranslatorInterface $translator, $translationDomain = null, MetadataInterface $metadata = null, $value = null, $group = null, $propertyPath = '')
5973
{
6074
if (null === $group) {
6175
$group = Constraint::DEFAULT_GROUP;
6276
}
6377

6478
$this->globalContext = $globalContext;
79+
$this->translator = $translator;
80+
$this->translationDomain = $translationDomain;
6581
$this->metadata = $metadata;
6682
$this->value = $value;
6783
$this->propertyPath = $propertyPath;
@@ -74,6 +90,9 @@ public function __construct(GlobalExecutionContextInterface $globalContext, Meta
7490
public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
7591
{
7692
$this->globalContext->getViolations()->add(new ConstraintViolation(
93+
null === $pluralization
94+
? $this->translator->trans($message, $params, $this->translationDomain)
95+
: $this->translator->transChoice($message, $pluralization, $params, $this->translationDomain),
7796
$message,
7897
$params,
7998
$this->globalContext->getRoot(),
@@ -103,6 +122,9 @@ public function addViolationAtPath($propertyPath, $message, array $params = arra
103122
trigger_error('addViolationAtPath() is deprecated since version 2.2 and will be removed in 2.3.', E_USER_DEPRECATED);
104123

105124
$this->globalContext->getViolations()->add(new ConstraintViolation(
125+
null === $pluralization
126+
? $this->translator->trans($message, $params, $this->translationDomain)
127+
: $this->translator->transChoice($message, $pluralization, $params, $this->translationDomain),
106128
$message,
107129
$params,
108130
$this->globalContext->getRoot(),
@@ -146,6 +168,9 @@ public function addViolationAtSubPath($subPath, $message, array $params = array(
146168
public function addViolationAt($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
147169
{
148170
$this->globalContext->getViolations()->add(new ConstraintViolation(
171+
null === $pluralization
172+
? $this->translator->trans($message, $params, $this->translationDomain)
173+
: $this->translator->transChoice($message, $pluralization, $params, $this->translationDomain),
149174
$message,
150175
$params,
151176
$this->globalContext->getRoot(),

src/Symfony/Component/Validator/GraphWalker.php

+18Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Translation\TranslatorInterface;
1516
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1617
use Symfony\Component\Validator\Mapping\ClassMetadata;
1718
use Symfony\Component\Validator\Mapping\MemberMetadata;
@@ -39,6 +40,16 @@ class GraphWalker
3940
*/
4041
private $metadataFactory;
4142

43+
/**
44+
* @var TranslatorInterface
45+
*/
46+
private $translator;
47+
48+
/**
49+
* @var null|string
50+
*/
51+
private $translationDomain;
52+
4253
/**
4354
* @var array
4455
*/
@@ -49,16 +60,20 @@ class GraphWalker
4960
*
5061
* @param ValidationVisitor $visitor
5162
* @param MetadataFactoryInterface $metadataFactory
63+
* @param TranslatorInterface $translator
64+
* @param null|string $translationDomain
5265
* @param array $validatedObjects
5366
*
5467
* @deprecated Deprecated since version 2.2, to be removed in 2.3.
5568
*/
56-
public function __construct(ValidationVisitor $visitor, MetadataFactoryInterface $metadataFactory, array &$validatedObjects = array())
69+
public function __construct(ValidationVisitor $visitor, MetadataFactoryInterface $metadataFactory, TranslatorInterface $translator, $translationDomain = null, array &$validatedObjects = array())
5770
{
5871
trigger_error('GraphWalker is deprecated since version 2.2 and will be removed in 2.3. This class has been replaced by ValidationVisitorInterface and MetadataInterface.', E_USER_DEPRECATED);
5972

6073
$this->visitor = $visitor;
6174
$this->metadataFactory = $metadataFactory;
75+
$this->translator = $translator;
76+
$this->translationDomain = $translationDomain;
6277
$this->validatedObjects = &$validatedObjects;
6378
}
6479

@@ -208,6 +223,8 @@ public function walkConstraint(Constraint $constraint, $value, $group, $property
208223

209224
$context = new ExecutionContext(
210225
$this->visitor,
226+
$this->translator,
227+
$this->translationDomain,
211228
$metadata,
212229
$value,
213230
$group,

src/Symfony/Component/Validator/Tests/ConstraintViolationListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ public function testToString()
129129

130130
protected function getViolation($message, $root = null, $propertyPath = null)
131131
{
132-
return new ConstraintViolation($message, array(), $root, $propertyPath, null);
132+
return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
133133
}
134134
}

src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ConstraintViolationTest extends \PHPUnit_Framework_TestCase
1818
public function testToStringHandlesArrays()
1919
{
2020
$violation = new ConstraintViolation(
21+
'Array',
2122
'{{ value }}',
2223
array('{{ value }}' => array(1, 2, 3)),
2324
'Root',

0 commit comments

Comments
 (0)
0