Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ae68c9

Browse files
committed
[Validator] Made tests green (yay!)
1 parent 680f1ee commit 8ae68c9

27 files changed

+693
-119
lines changed

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
namespace Symfony\Component\Validator\Context;
1313

14+
use Symfony\Component\Translation\TranslatorInterface;
1415
use Symfony\Component\Validator\ClassBasedInterface;
16+
use Symfony\Component\Validator\ConstraintViolation;
1517
use Symfony\Component\Validator\ConstraintViolationList;
1618
use Symfony\Component\Validator\Group\GroupManagerInterface;
1719
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
1820
use Symfony\Component\Validator\Node\Node;
21+
use Symfony\Component\Validator\Util\PropertyPath;
1922
use Symfony\Component\Validator\Validator\ValidatorInterface;
23+
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
2024

2125
/**
2226
* @since %%NextVersion%%
@@ -48,18 +52,30 @@ class ExecutionContext implements ExecutionContextInterface
4852
*/
4953
private $groupManager;
5054

51-
public function __construct(ValidatorInterface $validator, GroupManagerInterface $groupManager)
55+
/**
56+
* @var TranslatorInterface
57+
*/
58+
private $translator;
59+
60+
/**
61+
* @var string
62+
*/
63+
private $translationDomain;
64+
65+
public function __construct($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
5266
{
67+
$this->root = $root;
5368
$this->validator = $validator;
5469
$this->groupManager = $groupManager;
70+
$this->translator = $translator;
71+
$this->translationDomain = $translationDomain;
5572
$this->violations = new ConstraintViolationList();
73+
$this->nodeStack = new \SplStack();
5674
}
5775

5876
public function pushNode(Node $node)
5977
{
60-
if (null === $this->node) {
61-
$this->root = $node->value;
62-
} else {
78+
if (null !== $this->node) {
6379
$this->nodeStack->push($this->node);
6480
}
6581

@@ -89,13 +105,32 @@ public function popNode()
89105
return $poppedNode;
90106
}
91107

92-
public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
108+
public function addViolation($message, array $parameters = array())
93109
{
110+
$this->violations->add(new ConstraintViolation(
111+
$this->translator->trans($message, $parameters, $this->translationDomain),
112+
$message,
113+
$parameters,
114+
$this->root,
115+
$this->getPropertyPath(),
116+
$this->getValue(),
117+
null,
118+
null
119+
));
94120
}
95121

96-
public function buildViolation($message)
122+
public function buildViolation($message, array $parameters = array())
97123
{
98-
124+
return new ConstraintViolationBuilder(
125+
$this->violations,
126+
$message,
127+
$parameters,
128+
$this->root,
129+
$this->getPropertyPath(),
130+
$this->getValue(),
131+
$this->translator,
132+
$this->translationDomain
133+
);
99134
}
100135

101136
public function getViolations()
@@ -141,15 +176,7 @@ public function getPropertyPath($subPath = '')
141176
{
142177
$propertyPath = $this->node ? $this->node->propertyPath : '';
143178

144-
if (strlen($subPath) > 0) {
145-
if ('[' === $subPath{1}) {
146-
return $propertyPath.$subPath;
147-
}
148-
149-
return $propertyPath ? $propertyPath.'.'.$subPath : $subPath;
150-
}
151-
152-
return $propertyPath;
179+
return PropertyPath::append($propertyPath, $subPath);
153180
}
154181

155182
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function getValidator();
3030
* Adds a violation at the current node of the validation graph.
3131
*
3232
* @param string $message The error message.
33-
* @param array $params The parameters substituted in the error message.
33+
* @param array $parameters The parameters substituted in the error message.
3434
*
3535
* @api
3636
*/
37-
public function addViolation($message, array $params = array());
37+
public function addViolation($message, array $parameters = array());
3838

3939
public function buildViolation($message);
4040

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

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

1212
namespace Symfony\Component\Validator\Context;
1313

14+
use Symfony\Component\Translation\TranslatorInterface;
1415
use Symfony\Component\Validator\Group\GroupManagerInterface;
1516
use Symfony\Component\Validator\Node\Node;
1617
use Symfony\Component\Validator\NodeVisitor\AbstractVisitor;
@@ -42,9 +43,21 @@ class ExecutionContextManager extends AbstractVisitor implements ExecutionContex
4243
*/
4344
private $contextStack;
4445

45-
public function __construct(GroupManagerInterface $groupManager)
46+
/**
47+
* @var TranslatorInterface
48+
*/
49+
private $translator;
50+
51+
/**
52+
* @var string|null
53+
*/
54+
private $translationDomain;
55+
56+
public function __construct(GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
4657
{
4758
$this->groupManager = $groupManager;
59+
$this->translator = $translator;
60+
$this->translationDomain = $translationDomain;
4861
$this->contextStack = new \SplStack();
4962
}
5063

@@ -53,13 +66,23 @@ public function initialize(ValidatorInterface $validator)
5366
$this->validator = $validator;
5467
}
5568

56-
public function startContext()
69+
public function startContext($root)
5770
{
71+
if (null === $this->validator) {
72+
// TODO error, call initialize() first
73+
}
74+
5875
if (null !== $this->currentContext) {
5976
$this->contextStack->push($this->currentContext);
6077
}
6178

62-
$this->currentContext = new ExecutionContext($this->validator, $this->groupManager);
79+
$this->currentContext = new LegacyExecutionContext(
80+
$root,
81+
$this->validator,
82+
$this->groupManager,
83+
$this->translator,
84+
$this->translationDomain
85+
);
6386

6487
return $this->currentContext;
6588
}
@@ -100,7 +123,7 @@ public function afterTraversal(array $nodes)
100123
public function enterNode(Node $node)
101124
{
102125
if (null === $this->currentContext) {
103-
// error no context started
126+
// TODO error call startContext() first
104127
}
105128

106129
$this->currentContext->pushNode($node);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
interface ExecutionContextManagerInterface
1919
{
2020
/**
21+
* @param mixed $root
22+
*
2123
* @return ExecutionContextInterface The started context
2224
*/
23-
public function startContext();
25+
public function startContext($root);
2426

2527
/**
2628
* @return ExecutionContextInterface The stopped context

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

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,97 @@
1111

1212
namespace Symfony\Component\Validator\Context;
1313

14+
use Symfony\Component\Translation\TranslatorInterface;
15+
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1416
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
17+
use Symfony\Component\Validator\Group\GroupManagerInterface;
18+
use Symfony\Component\Validator\Validator\ValidatorInterface;
19+
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
1520

1621
/**
1722
* @since %%NextVersion%%
1823
* @author Bernhard Schussek <[email protected]>
1924
*/
2025
class LegacyExecutionContext extends ExecutionContext implements LegacyExecutionContextInterface
2126
{
22-
public function addViolationAt($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
27+
public function __construct($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
2328
{
29+
if (!$validator instanceof LegacyValidatorInterface) {
30+
throw new InvalidArgumentException(
31+
'The validator passed to LegacyExecutionContext must implement '.
32+
'"Symfony\Component\Validator\ValidatorInterface".'
33+
);
34+
}
2435

36+
parent::__construct($root, $validator, $groupManager, $translator, $translationDomain);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function addViolation($message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
43+
{
44+
if (func_num_args() >= 3) {
45+
$this
46+
->buildViolation($message, $parameters)
47+
->setInvalidValue($invalidValue)
48+
->setPluralization($pluralization)
49+
->setCode($code)
50+
->addViolation()
51+
;
52+
53+
return;
54+
}
55+
56+
parent::addViolation($message, $parameters);
57+
}
58+
59+
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
60+
{
61+
if (func_num_args() >= 3) {
62+
$this
63+
->buildViolation($message, $parameters)
64+
->atPath($subPath)
65+
->setInvalidValue($invalidValue)
66+
->setPluralization($pluralization)
67+
->setCode($code)
68+
->addViolation()
69+
;
70+
71+
return;
72+
}
73+
74+
$this
75+
->buildViolation($message, $parameters)
76+
->atPath($subPath)
77+
->addViolation()
78+
;
2579
}
2680

2781
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
2882
{
83+
// TODO handle $traverse and $deep
2984

85+
return $this
86+
->getValidator()
87+
->inContext($this)
88+
->atPath($subPath)
89+
->validateObject($value, $groups)
90+
;
3091
}
3192

3293
public function validateValue($value, $constraints, $subPath = '', $groups = null)
3394
{
34-
95+
return $this
96+
->getValidator()
97+
->inContext($this)
98+
->atPath($subPath)
99+
->validateValue($value, $constraints, $groups)
100+
;
35101
}
36102

37103
public function getMetadataFactory()
38104
{
39-
105+
return $this->getValidator()->getMetadataFactory();
40106
}
41107
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Mapping;
13+
14+
/**
15+
* @since %%NextVersion%%
16+
* @author Bernhard Schussek <[email protected]>
17+
*/
18+
class CascadingStrategy
19+
{
20+
const NONE = 0;
21+
22+
const CASCADE = 1;
23+
24+
private function __construct()
25+
{
26+
}
27+
}

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @author Bernhard Schussek <[email protected]>
2727
* @author Fabien Potencier <[email protected]>
2828
*/
29-
class ClassMetadata extends ElementMetadata implements MetadataInterface, ClassBasedInterface, PropertyMetadataContainerInterface
29+
class ClassMetadata extends ElementMetadata implements MetadataInterface, ClassBasedInterface, PropertyMetadataContainerInterface, ClassMetadataInterface
3030
{
3131
/**
3232
* @var string
@@ -63,6 +63,8 @@ class ClassMetadata extends ElementMetadata implements MetadataInterface, ClassB
6363
*/
6464
public $groupSequenceProvider = false;
6565

66+
public $traversalStrategy = TraversalStrategy::IMPLICIT;
67+
6668
/**
6769
* @var \ReflectionClass
6870
*/
@@ -423,4 +425,19 @@ public function isGroupSequenceProvider()
423425
{
424426
return $this->groupSequenceProvider;
425427
}
428+
429+
/**
430+
* Class nodes are never cascaded.
431+
*
432+
* @return Boolean Always returns false.
433+
*/
434+
public function getCascadingStrategy()
435+
{
436+
return CascadingStrategy::NONE;
437+
}
438+
439+
public function getTraversalStrategy()
440+
{
441+
return $this->traversalStrategy;
442+
}
426443
}

src/Symfony/Component/Validator/Mapping/ElementMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515

16-
abstract class ElementMetadata
16+
abstract class ElementMetadata implements MetadataInterface
1717
{
1818
/**
1919
* @var Constraint[]

0 commit comments

Comments
 (0)