[Validator] Fixed the new validator API under PHP < 5.3.9

This commit is contained in:
Bernhard Schussek 2014-02-20 21:06:05 +01:00
parent 2936d10aa4
commit e8fa15b27c
4 changed files with 22 additions and 144 deletions

View File

@ -16,7 +16,6 @@ use Symfony\Component\Validator\ClassBasedInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Exception\BadMethodCallException;
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
use Symfony\Component\Validator\Group\GroupManagerInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Node\Node;
@ -33,7 +32,7 @@ use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
*
* @see ExecutionContextInterface
*/
class ExecutionContext implements ExecutionContextInterface, LegacyExecutionContextInterface, NodeObserverInterface
class ExecutionContext implements ExecutionContextInterface, NodeObserverInterface
{
/**
* @var ValidatorInterface
@ -121,6 +120,13 @@ class ExecutionContext implements ExecutionContextInterface, LegacyExecutionCont
// The parameters $invalidValue and following are ignored by the new
// API, as they are not present in the new interface anymore.
// You should use buildViolation() instead.
if (func_num_args() > 2) {
throw new BadMethodCallException(
'The parameters $invalidValue, $pluralization and $code are '.
'not supported anymore as of Symfony 2.5. Please use '.
'buildViolation() instead or enable the legacy mode.'
);
}
$this->violations->add(new ConstraintViolation(
$this->translator->trans($message, $parameters, $this->translationDomain),
@ -235,8 +241,8 @@ class ExecutionContext implements ExecutionContextInterface, LegacyExecutionCont
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
{
throw new BadMethodCallException(
'addViolationAt() is not supported anymore in the new API. '.
'Please use buildViolation() or enable the legacy mode.'
'addViolationAt() is not supported anymore as of Symfony 2.5. '.
'Please use buildViolation() instead or enable the legacy mode.'
);
}
@ -246,8 +252,8 @@ class ExecutionContext implements ExecutionContextInterface, LegacyExecutionCont
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
{
throw new BadMethodCallException(
'validate() is not supported anymore in the new API. '.
'Please use getValidator() or enable the legacy mode.'
'validate() is not supported anymore as of Symfony 2.5. '.
'Please use getValidator() instead or enable the legacy mode.'
);
}
@ -257,8 +263,8 @@ class ExecutionContext implements ExecutionContextInterface, LegacyExecutionCont
public function validateValue($value, $constraints, $subPath = '', $groups = null)
{
throw new BadMethodCallException(
'validateValue() is not supported anymore in the new API. '.
'Please use getValidator() or enable the legacy mode.'
'validateValue() is not supported anymore as of Symfony 2.5. '.
'Please use getValidator() instead or enable the legacy mode.'
);
}
@ -268,9 +274,9 @@ class ExecutionContext implements ExecutionContextInterface, LegacyExecutionCont
public function getMetadataFactory()
{
throw new BadMethodCallException(
'getMetadataFactory() is not supported anymore in the new API. '.
'Please use getMetadataFor() or hasMetadataFor() or enable the '.
'legacy mode.'
'getMetadataFactory() is not supported anymore as of Symfony 2.5. '.
'Please use getValidator() in combination with getMetadataFor() '.
'or hasMetadataFor() instead or enable the legacy mode.'
);
}
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Context;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
@ -59,16 +60,8 @@ use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
* @since 2.5
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface ExecutionContextInterface
interface ExecutionContextInterface extends LegacyExecutionContextInterface
{
/**
* Adds a violation at the current node of the validation graph.
*
* @param string $message The error message
* @param array $parameters The parameters substituted in the error message
*/
public function addViolation($message, array $parameters = array());
/**
* Returns a builder for adding a violation with extended information.
*
@ -88,13 +81,6 @@ interface ExecutionContextInterface
*/
public function buildViolation($message, array $parameters = array());
/**
* Returns the violations generated in this context.
*
* @return ConstraintViolationListInterface The constraint violations
*/
public function getViolations();
/**
* Returns the validator.
*
@ -114,108 +100,4 @@ interface ExecutionContextInterface
* @return ValidatorInterface
*/
public function getValidator();
/**
* Returns the root value of the object graph.
*
* The validator, when given an object, traverses the properties and
* related objects and their properties. The root of the validation is the
* object at which the traversal started.
*
* The current value is returned by {@link getValue()}.
*
* @return mixed|null The root value of the validation or null, if no value
* is currently being validated
*/
public function getRoot();
/**
* Returns the value that the validator is currently validating.
*
* If you want to retrieve the object that was originally passed to the
* validator, use {@link getRoot()}.
*
* @return mixed|null The currently validated value or null, if no value is
* currently being validated
*/
public function getValue();
/**
* Returns the metadata for the currently validated value.
*
* With the core implementation, this method returns a
* {@link Mapping\ClassMetadata} instance if the current value is an object,
* a {@link Mapping\PropertyMetadata} instance if the current value is
* the value of a property and a {@link Mapping\GetterMetadata} instance if
* the validated value is the result of a getter method. The metadata can
* also be an {@link Mapping\GenericMetadata} if the current value does not
* belong to any structural element.
*
* @return MetadataInterface|null The metadata of the currently validated
* value or null, if no value is currently
* being validated
*/
public function getMetadata();
/**
* Returns the validation group that is currently being validated.
*
* @return string|GroupSequence|null The current validation group or null,
* if no value is currently being
* validated
*/
public function getGroup();
/**
* Returns the class name of the current node.
*
* If the metadata of the current node does not implement
* {@link ClassBasedInterface}, this method returns null.
*
* @return string|null The class name or null, if no class name could be
* found
*/
public function getClassName();
/**
* Returns the property name of the current node.
*
* If the metadata of the current node does not implement
* {@link PropertyMetadataInterface}, this method returns null.
*
* @return string|null The property name or null, if no property name could
* be found
*/
public function getPropertyName();
/**
* Returns the property path to the value that the validator is currently
* validating.
*
* For example, take the following object graph:
*
* (Person)---($address: Address)---($street: string)
*
* When the <tt>Person</tt> instance is passed to the validator, the
* property path is initially empty. When the <tt>$address</tt> property
* of that person is validated, the property path is "address". When
* the <tt>$street</tt> property of the related <tt>Address</tt> instance
* is validated, the property path is "address.street".
*
* Properties of objects are prefixed with a dot in the property path.
* Indices of arrays or objects implementing the {@link \ArrayAccess}
* interface are enclosed in brackets. For example, if the property in
* the previous example is <tt>$addresses</tt> and contains an array
* of <tt>Address</tt> instances, the property path generated for the
* <tt>$street</tt> property of one of these addresses is for example
* "addresses[0].street".
*
* @param string $subPath Optional. The suffix appended to the current
* property path
*
* @return string The current property path. The result may be an empty
* string if the validator is currently validating the
* root value of the validation graph
*/
public function getPropertyPath($subPath = '');
}

View File

@ -15,7 +15,6 @@ use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Traverse;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
use Symfony\Component\Validator\Group\GroupManagerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
@ -29,7 +28,7 @@ use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
* @deprecated Implemented for backwards compatibility with Symfony < 2.5. To be
* removed in 3.0.
*/
class LegacyExecutionContext extends ExecutionContext implements LegacyExecutionContextInterface
class LegacyExecutionContext extends ExecutionContext
{
/**
* Creates a new context.
@ -66,7 +65,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution
*/
public function addViolation($message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
{
if (func_num_args() >= 3) {
if (func_num_args() > 2) {
$this
->buildViolation($message, $parameters)
->setInvalidValue($invalidValue)
@ -86,7 +85,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution
*/
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
{
if (func_num_args() >= 3) {
if (func_num_args() > 2) {
$this
->buildViolation($message, $parameters)
->atPath($subPath)

View File

@ -23,15 +23,6 @@ use Symfony\Component\Validator\Validator\Validator;
class Validator2Dot5ApiTest extends Abstract2Dot5ApiTest
{
protected function setUp()
{
if (version_compare(PHP_VERSION, '5.3.9', '<')) {
$this->markTestSkipped('Not supported prior to PHP 5.3.9');
}
parent::setUp();
}
protected function createValidator(MetadataFactoryInterface $metadataFactory)
{
$nodeTraverser = new NodeTraverser($metadataFactory);