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

Skip to content

Commit a6ed4ca

Browse files
committed
[Validator] Prototype of the traverser implementation
1 parent 25cdc68 commit a6ed4ca

27 files changed

+1851
-0
lines changed

src/Symfony/Component/Validator/Constraints/GroupSequence.php

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

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Traversable;
15+
1416
/**
1517
* Annotation for group sequences
1618
*
@@ -28,6 +30,22 @@ class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable
2830
*/
2931
public $groups;
3032

33+
/**
34+
* The group under which cascaded objects are validated when validating
35+
* this sequence.
36+
*
37+
* By default, cascaded objects are validated in each of the groups of
38+
* the sequence.
39+
*
40+
* If a class has a group sequence attached, that sequence replaces the
41+
* "Default" group. When validating that class in the "Default" group, the
42+
* group sequence is used instead, but still the "Default" group should be
43+
* cascaded to other objects.
44+
*
45+
* @var string|GroupSequence
46+
*/
47+
public $cascadedGroup;
48+
3149
public function __construct(array $groups)
3250
{
3351
// Support for Doctrine annotations
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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\Context;
13+
14+
use Symfony\Component\Validator\ClassBasedInterface;
15+
use Symfony\Component\Validator\ConstraintViolationList;
16+
use Symfony\Component\Validator\Group\GroupManagerInterface;
17+
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
18+
use Symfony\Component\Validator\MetadataFactoryInterface;
19+
use Symfony\Component\Validator\Node\Node;
20+
use Symfony\Component\Validator\Validator\ValidatorInterface;
21+
22+
/**
23+
* @since %%NextVersion%%
24+
* @author Bernhard Schussek <[email protected]>
25+
*/
26+
class ExecutionContext implements ExecutionContextInterface
27+
{
28+
private $root;
29+
30+
private $violations;
31+
32+
/**
33+
* @var Node
34+
*/
35+
private $node;
36+
37+
/**
38+
* @var \SplStack
39+
*/
40+
private $nodeStack;
41+
42+
/**
43+
* @var MetadataFactoryInterface
44+
*/
45+
private $metadataFactory;
46+
47+
/**
48+
* @var ValidatorInterface
49+
*/
50+
private $validator;
51+
52+
/**
53+
* @var GroupManagerInterface
54+
*/
55+
private $groupManager;
56+
57+
public function __construct(MetadataFactoryInterface $metadataFactory, ValidatorInterface $validator, GroupManagerInterface $groupManager)
58+
{
59+
$this->metadataFactory = $metadataFactory;
60+
$this->validator = $validator;
61+
$this->groupManager = $groupManager;
62+
$this->violations = new ConstraintViolationList();
63+
}
64+
65+
public function pushNode(Node $node)
66+
{
67+
if (null === $this->node) {
68+
$this->root = $node->value;
69+
} else {
70+
$this->nodeStack->push($this->node);
71+
}
72+
73+
$this->node = $node;
74+
}
75+
76+
public function popNode()
77+
{
78+
$poppedNode = $this->node;
79+
80+
if (0 === count($this->nodeStack)) {
81+
$this->node = null;
82+
83+
return $poppedNode;
84+
}
85+
86+
if (1 === count($this->nodeStack)) {
87+
$this->nodeStack->pop();
88+
$this->node = null;
89+
90+
return $poppedNode;
91+
}
92+
93+
$this->nodeStack->pop();
94+
$this->node = $this->nodeStack->top();
95+
96+
return $poppedNode;
97+
}
98+
99+
public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
100+
{
101+
}
102+
103+
public function buildViolation($message)
104+
{
105+
106+
}
107+
108+
public function getMetadataFor($object)
109+
{
110+
111+
}
112+
113+
public function getViolations()
114+
{
115+
return $this->violations;
116+
}
117+
118+
public function getRoot()
119+
{
120+
return $this->root;
121+
}
122+
123+
public function getValue()
124+
{
125+
return $this->node ? $this->node->value : null;
126+
}
127+
128+
public function getMetadata()
129+
{
130+
return $this->node ? $this->node->metadata : null;
131+
}
132+
133+
public function getGroup()
134+
{
135+
return $this->groupManager->getCurrentGroup();
136+
}
137+
138+
public function getClassName()
139+
{
140+
$metadata = $this->getMetadata();
141+
142+
return $metadata instanceof ClassBasedInterface ? $metadata->getClassName() : null;
143+
}
144+
145+
public function getPropertyName()
146+
{
147+
$metadata = $this->getMetadata();
148+
149+
return $metadata instanceof PropertyMetadataInterface ? $metadata->getPropertyName() : null;
150+
}
151+
152+
public function getPropertyPath($subPath = '')
153+
{
154+
$propertyPath = $this->node ? $this->node->propertyPath : '';
155+
156+
if (strlen($subPath) > 0) {
157+
if ('[' === $subPath{1}) {
158+
return $propertyPath.$subPath;
159+
}
160+
161+
return $propertyPath ? $propertyPath.'.'.$subPath : $subPath;
162+
}
163+
164+
return $propertyPath;
165+
}
166+
167+
/**
168+
* @return ValidatorInterface
169+
*/
170+
public function getValidator()
171+
{
172+
return $this->validator;
173+
}
174+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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\Context;
13+
14+
use Symfony\Component\Validator\ConstraintViolationListInterface;
15+
use Symfony\Component\Validator\Mapping\MetadataInterface;
16+
use Symfony\Component\Validator\Validator\ValidatorInterface;
17+
18+
/**
19+
* @since %%NextVersion%%
20+
* @author Bernhard Schussek <[email protected]>
21+
*/
22+
interface ExecutionContextInterface
23+
{
24+
/**
25+
* @return ValidatorInterface
26+
*/
27+
public function getValidator();
28+
29+
/**
30+
* Adds a violation at the current node of the validation graph.
31+
*
32+
* @param string $message The error message.
33+
* @param array $params The parameters substituted in the error message.
34+
*
35+
* @api
36+
*/
37+
public function addViolation($message, array $params = array());
38+
39+
public function buildViolation($message);
40+
41+
/**
42+
* Returns the violations generated by the validator so far.
43+
*
44+
* @return ConstraintViolationListInterface The constraint violation list.
45+
*
46+
* @api
47+
*/
48+
public function getViolations();
49+
50+
/**
51+
* Returns the value at which validation was started in the object graph.
52+
*
53+
* The validator, when given an object, traverses the properties and
54+
* related objects and their properties. The root of the validation is the
55+
* object from which the traversal started.
56+
*
57+
* The current value is returned by {@link getValue}.
58+
*
59+
* @return mixed The root value of the validation.
60+
*/
61+
public function getRoot();
62+
63+
/**
64+
* Returns the value that the validator is currently validating.
65+
*
66+
* If you want to retrieve the object that was originally passed to the
67+
* validator, use {@link getRoot}.
68+
*
69+
* @return mixed The currently validated value.
70+
*/
71+
public function getValue();
72+
73+
/**
74+
* Returns the metadata for the currently validated value.
75+
*
76+
* With the core implementation, this method returns a
77+
* {@link Mapping\ClassMetadata} instance if the current value is an object,
78+
* a {@link Mapping\PropertyMetadata} instance if the current value is
79+
* the value of a property and a {@link Mapping\GetterMetadata} instance if
80+
* the validated value is the result of a getter method.
81+
*
82+
* If the validated value is neither of these, for example if the validator
83+
* has been called with a plain value and constraint, this method returns
84+
* null.
85+
*
86+
* @return MetadataInterface|null The metadata of the currently validated
87+
* value.
88+
*/
89+
public function getMetadata();
90+
91+
public function getMetadataFor($object);
92+
93+
/**
94+
* Returns the validation group that is currently being validated.
95+
*
96+
* @return string The current validation group.
97+
*/
98+
public function getGroup();
99+
100+
/**
101+
* Returns the class name of the current node.
102+
*
103+
* If the metadata of the current node does not implement
104+
* {@link ClassBasedInterface} or if no metadata is available for the
105+
* current node, this method returns null.
106+
*
107+
* @return string|null The class name or null, if no class name could be found.
108+
*/
109+
public function getClassName();
110+
111+
/**
112+
* Returns the property name of the current node.
113+
*
114+
* If the metadata of the current node does not implement
115+
* {@link PropertyMetadataInterface} or if no metadata is available for the
116+
* current node, this method returns null.
117+
*
118+
* @return string|null The property name or null, if no property name could be found.
119+
*/
120+
public function getPropertyName();
121+
122+
/**
123+
* Returns the property path to the value that the validator is currently
124+
* validating.
125+
*
126+
* For example, take the following object graph:
127+
*
128+
* <pre>
129+
* (Person)---($address: Address)---($street: string)
130+
* </pre>
131+
*
132+
* When the <tt>Person</tt> instance is passed to the validator, the
133+
* property path is initially empty. When the <tt>$address</tt> property
134+
* of that person is validated, the property path is "address". When
135+
* the <tt>$street</tt> property of the related <tt>Address</tt> instance
136+
* is validated, the property path is "address.street".
137+
*
138+
* Properties of objects are prefixed with a dot in the property path.
139+
* Indices of arrays or objects implementing the {@link \ArrayAccess}
140+
* interface are enclosed in brackets. For example, if the property in
141+
* the previous example is <tt>$addresses</tt> and contains an array
142+
* of <tt>Address</tt> instance, the property path generated for the
143+
* <tt>$street</tt> property of one of these addresses is for example
144+
* "addresses[0].street".
145+
*
146+
* @param string $subPath Optional. The suffix appended to the current
147+
* property path.
148+
*
149+
* @return string The current property path. The result may be an empty
150+
* string if the validator is currently validating the
151+
* root value of the validation graph.
152+
*/
153+
public function getPropertyPath($subPath = '');
154+
}

0 commit comments

Comments
 (0)