8000 [Validator] Refactored the GraphWalker into an implementation of the … · symfony/symfony@efe42cb · GitHub
[go: up one dir, main page]

Skip to content

Commit efe42cb

Browse files
committed
[Validator] Refactored the GraphWalker into an implementation of the Visitor design pattern.
With this refactoring comes a decoupling of the validator from the structure of the underlying metadata. This way it is possible for Drupal to use the validator for validating their Entity API by using their own metadata layer, which is not modeled as classes and properties/getter methods.
1 parent 225e3e5 commit efe42cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2711
-569
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ CHANGELOG
66

77
* added a CardScheme validator
88
* added a Luhn validator
9+
* moved @api-tags from `Validator` to `ValidatorInterface`
10+
* moved @api-tags from `ConstraintViolation` to the new `ConstraintViolationInterface`
11+
* moved @api-tags from `ConstraintViolationList` to the new `ConstraintViolationListInterface`
12+
* moved @api-tags from `ExecutionContext` to the new `ExecutionContextInterface`
13+
* [BC BREAK] `ConstraintValidatorInterface::initialize` is now type hinted against `ExecutionContextInterface` instead of `ExecutionContext`
14+
* [BC BREAK] changed the visibility of the properties in `Validator` from protected to private
15+
* deprecated `ClassMetadataFactoryInterface` in favor of the new `MetadataFactoryInterface`
16+
* deprecated `ClassMetadataFactory::getClassMetadata` in favor of `getMetadataFor`
17+
* created `MetadataInterface`, `PropertyMetadataInterface`, `ClassBasedInterface` and `PropertyMetadataContainerInterface`
18+
* deprecated `GraphWalker` in favor of the new `ValidationVisitorInterface`
19+
* deprecated `ExecutionContext::addViolationAtPath`
20+
* deprecated `ExecutionContext::addViolationAtSubPath` in favor of `ExecutionContextInterface::addViolationAt`
21+
* deprecated `ExecutionContext::getCurrentClass` in favor of `ExecutionContextInterface::getClassName`
22+
* deprecated `ExecutionContext::getCurrentProperty` in favor of `ExecutionContextInterface::getPropertyName`
23+
* deprecated `ExecutionContext::getCurrentValue` in favor of `ExecutionContextInterface::getValue`
24+
* deprecated `ExecutionContext::getGraphWalker` in favor of `ExecutionContextInterface::validate` and `ExecutionContextInterface::validateValue`
25+
* deprecated `ExecutionContext::getMetadataFactory` in favor of `ExecutionContextInterface::getMetadataFor`
26+
* improved `ValidatorInterface::validateValue` to accept arrays of constraints
27+
* changed `ValidatorInterface::getMetadataFactory` to return a `MetadataFactoryInterface` instead of a `ClassMetadataFactoryInterface`
28+
* removed `ClassMetadataFactoryInterface` type hint from `ValidatorBuilderInterface::setMetadataFactory`.
29+
As of Symfony 2.3, this method will be typed against `MetadataFactoryInterface` instead.
930

1031
2.1.0
1132
-----
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 <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+
/**
15+
* An object backed by a PHP class.
16+
*
17+
* @author Bernhard Schussek <bschussek@gmail.com>
18+
*/
19+
interface ClassBasedInterface
20+
{
21+
/**
22+
* Returns the name of the backing PHP class.
23+
*
24+
* @return string The name of the backing class.
25+
*/
26+
public function getClassName();
27+
}

src/Symfony/Component/Validator/ConstraintValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
abstract class ConstraintValidator implements ConstraintValidatorInterface
2424
{
2525
/**
26-
* @var ExecutionContext
26+
* @var ExecutionContextInterface
2727
*/
2828
protected $context;
2929

@@ -44,7 +44,7 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
4444
/**
4545
* {@inheritDoc}
4646
*/
47-
public function initialize(ExecutionContext $context)
47+
public function initialize(ExecutionContextInterface $context)
4848
{
4949
$this->context = $context;
5050
$this->messageTemplate = '';

src/Symfony/Component/Validator/ConstraintValidatorInterface.php

Lines changed: 2 additions & 2 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ interface ConstraintValidatorInterface
2121
/**
2222
* Initializes the constraint validator.
2323
*
24-
* @param ExecutionContext $context The current validation context
24+
* @param ExecutionContextInterface $context The current validation context
2525
*/
26-
public function initialize(ExecutionContext $context);
26+
public function initialize(ExecutionContextInterface $context);
2727

2828
/**
2929
* Checks if the passed value is valid.

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,64 @@
1212
namespace Symfony\Component\Validator;
1313

1414
/**
15-
* Represents a single violation of a constraint.
15+
* Default implementation of {@ConstraintViolationInterface}.
1616
*
17-
* @api
17+
* @author Bernhard Schussek <bschussek@gmail.com>
1818
*/
19-
class ConstraintViolation
19+
class ConstraintViolation implements ConstraintViolationInterface
2020
{
21-
protected $messageTemplate;
22-
protected $messageParameters;
23-
protected $messagePluralization;
24-
protected $root;
25-
protected $propertyPath;
26-
protected $invalidValue;
27-
protected $code;
21+
/**
22+
* @var string
23+
*/
24+
private $messageTemplate;
25+
26+
/**
27+
* @var array
28+
*/
29+
private $messageParameters;
30+
31+
/**
32+
* @var integer|null
33+
*/
34+
private $messagePluralization;
35+
36+
/**
37+
* @var mixed
38+
*/
39+
private $root;
40+
41+
/**
42+
* @var string
43+
*/
44+
private $propertyPath;
45+
46+
/**
47+
* @var mixed
48+
*/
49+
private $invalidValue;
50+
51+
/**
52+
* @var mixed
53+
*/
54+
private $code;
2855

56+
/**
57+
* Creates a new constraint violation.
58+
*
59+
* @param string $messageTemplate The raw violation message.
60+
* @param array $messageParameters The parameters to substitute
61+
* in the raw message.
62+
* @param mixed $root The value originally passed
63+
* to the validator.
64+
* @param string $propertyPath The property path from the
65+
* root value to the invalid
66+
* value.
67+
* @param mixed $invalidValue The invalid value causing the
68+
* violation.
69+
* @param integer|null $messagePluralization The pluralization parameter.
70+
* @param mixed $code The error code of the
71+
* violation, if any.
72+
*/
2973
public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null, $code = null)
3074
{
3175
$this->messageTemplate = $messageTemplate;
@@ -38,7 +82,9 @@ public function __construct($messageTemplate, array $messageParameters, $root, $
3882
}
3983

4084
/**
41-
* @return string
85+
* Converts the violation into a string for debugging purposes.
86+
*
87+
* @return string The violation as string.
4288
*/
4389
public function __toString()
4490
{
@@ -58,39 +104,31 @@ public function __toString()
58104
}
59105

60106
/**
61-
* @return string
62-
*
63-
* @api
107+
* {@inheritDoc}
64108
*/
65109
public function getMessageTemplate()
66110
{
67111
return $this->messageTemplate;
68112
}
69113

70114
/**
71-
* @return array
72-
*
73-
* @api
115+
* {@inheritDoc}
74116
*/
75117
public function getMessageParameters()
76118
{
77119
return $this->messageParameters;
78120
}
79121

80122
/**
81-
* @return integer|null
123+
* {@inheritDoc}
82124
*/
83125
public function getMessagePluralization()
84126
{
85127
return $this->messagePluralization;
86128
}
87129

88130
/**
89-
* Returns the violation message.
90-
*
91-
* @return string
92-
*
93-
* @api
131+
* {@inheritDoc}
94132
*/
95133
public function getMessage()
96134
{
@@ -105,21 +143,33 @@ public function getMessage()
105143
return strtr($this->messageTemplate, $parameters);
106144
}
107145

146+
/**
147+
* {@inheritDoc}
148+
*/
108149
public function getRoot()
109150
{
110151
return $this->root;
111152
}
112153

154+
/**
155+
* {@inheritDoc}
156+
*/
113157
public function getPropertyPath()
114158
{
115159
return $this->propertyPath;
116160
}
117161

162+
/**
163+
* {@inheritDoc}
164+
*/
118165
public function getInvalidValue()
119166
{
120167
return $this->invalidValue;
121168
}
122169

170+
/**
171+
* {@inheritDoc}
172+
*/
123173
public function getCode()
124174
{
125175
return $this->code;
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
/**
15+
* A violation of a constraint that happened during validation.
16+
*
17+
* For each constraint that fails during validation one or more violations are
18+
* created. The violations store the violation message, the path to the failing
19+
* element in the validation graph and the root element t D7AE hat was originally
20+
* passed to the validator. For example, take the following graph:
21+
*
22+
* <pre>
23+
* (Person)---(firstName: string)
24+
* \
25+
* (address: Address)---(street: string)
26+
* </pre>
27+
*
28+
* If the <tt>Person</tt> object is validated and validation fails for the
29+
* "firstName" property, the generated violation has the <tt>Person</tt>
30+
* instance as root and the property path "firstName". If validation fails
31+
* for the "street" property of the related <tt>Address</tt> instance, the root
32+
* element is still the person, but the property path is "address.street".
33+
*
34+
* @author Bernhard Schussek <bschussek@gmail.com>
35+
*
36+
* @api
37+
*/
38+
interface ConstraintViolationInterface
39+
{
40+
/**
41+
* Returns the violation message.
42+
*
43+
* @return string The violation message.
44+
*
45+
* @api
46+
*/
47+
public function getMessage();
48+
49+
/**
50+
* Returns the raw violation message.
51+
*
52+
* The raw violation message contains placeholders for the parameters
53+
* returned by {@link getMessageParameters}. Typically you'll pass the
54+
* message template and parameters to a translation engine.
55+
*
56+
* @return string The raw violation message.
57+
*
58+ * @api
59+
*/
60+
public function getMessageTemplate();
61+
62+
/**
63+
* Returns the parameters to be inserted into the raw violation message.
64+
*
65+
* @return array A possibly empty list of parameters indexed by the names
66+
* that appear in the message template.
67+
*
68+
* @see getMessageTemplate
69+
*
70+
* @api
71+
*/
72+
public function getMessageParameters();
73+
74+
/**
75+
* Returns a number for pluralizing the violation message.
76+
*
77+
* For example, the message template could have different translation based
78+
* on a parameter "choices":
79+
*
80+
* <ul>
81+
* <li>Please select exactly one entry. (choices=1)</li>
82+
* <li>Please select two entries. (choices=2)</li>
83+
* </ul>
84+
*
85+
* This method returns the value of the parameter for choosing the right
86+
* pluralization form (in this case "choices").
87+
*
88+
* @return integer|null The number to use to pluralize of the message.
89+
*/
90+
public function getMessagePluralization();
91+
92+
/**
93+
* Returns the root element of the validation.
94+
*
95+
* @return mixed The value that was passed originally to the validator when
96+
* the validation was started. Because the validator traverses
97+
* the object graph, the value at which the violation occurs
98+
* is not necessarily the value that was originally validated.
99+
*
100+
* @api
101+
*/
102+
public function getRoot();
103+
104+
/**
105+
* Returns the property path from the root element to the violation.
106+
*
107+
* @return string The property path indicates how the validator reached
108+
* the invalid value from the root element. If the root
109+
* element is a <tt>Person</tt> instance with a property
110+
* "address" that contains an <tt>Address</tt> instance
111+
* with an invalid property "street", the generated property
112+
* path is "address.street". Property access is denoted by
113+
* dots, while array access is denoted by square brackets,
114+
* for example "addresses[1].street".
115+
*
116+
* @api
117+
*/
118+
public function getPropertyPath();
119+
120+
/**
121+
* Returns the value that caused the violation.
122+
*
123+
* @return mixed The invalid value that caused the validated constraint to
124+
* fail.
125+
*
126+
* @api
127+
*/
128+
public function getInvalidValue();
129+
130+
/**
131+
* Returns a machine-digestible error code for the violation.
132+
*
133+
* @return mixed The error code.
134+
*/
135+
public function getCode();
136+
}

0 commit comments

Comments
 (0)
0