8000 [Validator] Clearly separated classes supporting the API <2.5/2.5+ · symfony/symfony@bc29591 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc29591

Browse files
committed
[Validator] Clearly separated classes supporting the API <2.5/2.5+
1 parent a3555fb commit bc29591

13 files changed

+1025
-928
lines changed

src/Symfony/Component/Validator/Context/ExecutionContext.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313

1414
use Symfony\Component\Translation\TranslatorInterface;
1515
use Symfony\Component\Validator\ClassBasedInterface;
16+
use Symfony\Component\Validator\Constraint;
1617
use Symfony\Component\Validator\ConstraintViolation;
1718
use Symfony\Component\Validator\ConstraintViolationList;
19+
use Symfony\Component\Validator\Exception\BadMethodCallException;
20+
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
1821
use Symfony\Component\Validator\Group\GroupManagerInterface;
1922
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
23+
use Symfony\Component\Validator\MetadataFactoryInterface;
2024
use Symfony\Component\Validator\Node\Node;
2125
use Symfony\Component\Validator\Util\PropertyPath;
2226
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -30,7 +34,7 @@
3034
*
3135
* @see ExecutionContextInterface
3236
*/
33-
class ExecutionContext implements ExecutionContextInterface
37+
class ExecutionContext implements ExecutionContextInterface, LegacyExecutionContextInterface
3438
{
3539
/**
3640
* The root value of the validated object graph.
@@ -151,8 +155,12 @@ public function popNode()
151155
/**
152156
* {@inheritdoc}
153157
*/
154-
public function addViolation($message, array $parameters = array())
158+
public function addViolation($message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
155159
{
160+
// The parameters $invalidValue and following are ignored by the new
161+
// API, as they are not present in the new interface anymore.
162+
// You should use buildViolation() instead.
163+
156164
$this->violations->add(new ConstraintViolation(
157165
$this->translator->trans($message, $parameters, $this->translationDomain),
158166
$message,
@@ -259,4 +267,49 @@ public function getPropertyPath($subPath = '')
259267

260268
return PropertyPath::append($propertyPath, $subPath);
261269
}
270+
271+
/**
272+
* {@inheritdoc}
273+
*/
274+
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
275+
{
276+
throw new BadMethodCallException(
277+
'addViolationAt() is not supported anymore in the new API. '.
278+
'Please use buildViolation() or enable the legacy mode.'
279+
);
280+
}
281+
282+
/**
283+
* {@inheritdoc}
284+
*/
285+
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
286+
{
287+
throw new BadMethodCallException(
288+
'validate() is not supported anymore in the new API. '.
289+
'Please use getValidator() or enable the legacy mode.'
290+
);
291+
}
292+
293+
/**
294+
* {@inheritdoc}
295+
*/
296+
public function validateValue($value, $constraints, $subPath = '', $groups = null)
297+
{
298+
throw new BadMethodCallException(
299+
'validateValue() is not supported anymore in the new API. '.
300+
'Please use getValidator() or enable the legacy mode.'
301+
);
302+
}
303+
304+
/**
305+
* {@inheritdoc}
306+
*/
307+
public function getMetadataFactory()
308+
{
309+
throw new BadMethodCallException(
310+
'getMetadataFactory() is not supported anymore in the new API. '.
311+
'Please use getMetadataFor() or hasMetadataFor() or enable the '.
312+
'legacy mode.'
313+
);
314+
}
262315
}

src/Symfony/Component/Validator/Context/ExecutionContextManager.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ public function startContext($root)
110110
);
111111
}
112112

113-
$this->currentContext = new LegacyExecutionContext(
113+
$this->currentContext = $this->createContext(
114114
$root,
115115
$this->validator,
116116
$this->groupManager,
117117
$this->translator,
118118
$this->translationDomain
119119
);
120+
120121
$this->contextStack->push($this->currentContext);
121122

122123
return $this->currentContext;
@@ -188,4 +189,27 @@ public function leaveNode(Node $node)
188189

189190
$this->currentContext->popNode();
190191
}
192+
193+
/**
194+
* Creates a new context.
195+
*
196+
* Can be overridden by subclasses.
197+
*
198+
* @param mixed $root The root value of the
199+
* validated object graph
200+
* @param ValidatorInterface $validator The validator
201+
* @param GroupManagerInterface $groupManager The manager for accessing
202+
* the currently validated
203+
* group
204+
* @param TranslatorInterface $translator The translator
205+
* @param string|null $translationDomain The translation domain to
206+
* use for translating
207+
* violation messages
208+
*
209+
* @return ExecutionContextInterface The created context
< 10000 div aria-hidden="true" class="position-absolute top-0 d-flex user-select-none DiffLineTableCellParts-module__comment-indicator--eI0hb">
210+
*/
211+
protected function createContext($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain)
212+
{
213+
return new ExecutionContext($root, $validator, $groupManager, $translator, $translationDomain);
214+
}
191215
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Context;
13+
14+
use Symfony\Component\Translation\TranslatorInterface;
15+
use Symfony\Component\Validator\Group\GroupManagerInterface;
16+
use Symfony\Component\Validator\Validator\ValidatorInterface;
17+
18+
/**
19+
* A context manager that creates contexts compatible to the API < Symfony 2.5.
20+
*
21+
* @since 2.5
22+
* @author Bernhard Schussek <bschussek@gmail.com>
23+
*
24+
* @see ExecutionContextManagerInterface
25+
* @see \Symfony\Component\Validator\NodeVisitor\NodeVisitorInterface
26+
*/
27+
class LegacyExecutionContextManager extends ExecutionContextManager
28+
{
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function createContext($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain)
33+
{
34+
return new LegacyExecutionContext($root, $validator, $groupManager, $translator, $translationDomain);
35+
}
36+
}

src/Symfony/Component/Validator/Tests/Fixtures/FakeMetadataFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Fixtures;
1313

14-
use Symfony\Component\Validator\MetadataFactoryInterface;
1514
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
1615
use Symfony\Component\Validator\Mapping\ClassMetadata;
16+
use Symfony\Component\Validator\MetadataFactoryInterface;
17+
use Symfony\Component\Validator\MetadataInterface;
1718

1819
class FakeMetadataFactory implements MetadataFactoryInterface
1920
{
@@ -53,4 +54,9 @@ public function addMetadata(ClassMetadata $metadata)
5354
{
5455
$this->metadatas[$metadata->getClassName()] = $metadata;
5556
}
57+
58+
public function addMetadataForValue($value, MetadataInterface $metadata)
59+
{
60+
$this->metadatas[$value] = $metadata;
61+
}
5662
}

0 commit comments

Comments
 (0)
0