Browse Source

deprecated Validator.

Mlanawo Mbechezi 10 years ago
parent
commit
2c1bc8f475
3 changed files with 15 additions and 328 deletions
  1. 5 64
      Validator/Constraints/InlineConstraint.php
  2. 5 220
      Validator/ErrorElement.php
  3. 5 44
      Validator/InlineValidator.php

+ 5 - 64
Validator/Constraints/InlineConstraint.php

@@ -10,70 +10,11 @@
 
 namespace Sonata\AdminBundle\Validator\Constraints;
 
-use Symfony\Component\Validator\Constraint;
+use Sonata\CoreBundle\Validator\Constraints\InlineConstraint as BaseInlineConstraint;
 
-class InlineConstraint extends Constraint
+/**
+ * @deprecated
+ */
+class InlineConstraint extends BaseInlineConstraint
 {
-    protected $service;
-
-    protected $method;
-
-    /**
-     * {@inheritDoc}
-     */
-    public function validatedBy()
-    {
-        return 'sonata.admin.validator.inline';
-    }
-
-    /**
-     * @return bool
-     */
-    public function isClosure()
-    {
-        return $this->method instanceof \Closure;
-    }
-
-    /**
-     * @return mixed
-     */
-    public function getClosure()
-    {
-        return $this->method;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getTargets()
-    {
-        return self::CLASS_CONSTRAINT;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getRequiredOptions()
-    {
-        return array(
-            'service',
-            'method'
-        );
-    }
-
-    /**
-     * @return string
-     */
-    public function getMethod()
-    {
-        return $this->method;
-    }
-
-    /**
-     * @return mixed
-     */
-    public function getService()
-    {
-        return $this->service;
-    }
 }

+ 5 - 220
Validator/ErrorElement.php

@@ -10,227 +10,12 @@
 
 namespace Sonata\AdminBundle\Validator;
 
-use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
-use Symfony\Component\Validator\ExecutionContextInterface;
-use Symfony\Component\PropertyAccess\PropertyAccess;
-use Symfony\Component\PropertyAccess\PropertyPath;
-use Symfony\Component\Validator\Constraint;
+use Sonata\CoreBundle\Validator\ErrorElement as BaseErrorElement;
 
-class ErrorElement
+/**
+ * @deprecated
+ */
+class ErrorElement extends BaseErrorElement
 {
-    /**
-     * @var ExecutionContextInterface
-     */
-    protected $context;
-
-    /**
-     * @var string
-     */
-    protected $group;
-
-    /**
-     * @var ConstraintValidationFactoryInterface
-     */
-    protected $constraintValidatorFactory;
-
-    protected $stack = array();
-
-    protected $propertyPaths = array();
-
-    protected $subject;
-
-    protected $current;
-
-    protected $basePropertyPath;
-
-    protected $errors = array();
-
-    /**
-     * @param mixed                               $subject
-     * @param ConstraintValidatorFactoryInterface $constraintValidatorFactory
-     * @param ExecutionContextInterface           $context
-     * @param string                              $group
-     */
-    public function __construct($subject, ConstraintValidatorFactoryInterface $constraintValidatorFactory, ExecutionContextInterface $context, $group)
-    {
-        $this->subject                    = $subject;
-        $this->context                    = $context;
-        $this->group                      = $group;
-        $this->constraintValidatorFactory = $constraintValidatorFactory;
-
-        $this->current          = '';
-        $this->basePropertyPath = $this->context->getPropertyPath();
-    }
-
-    /**
-     * @throws \RunTimeException
-     *
-     * @param string $name
-     * @param array  $arguments
-     *
-     * @return ErrorElement
-     */
-    public function __call($name, array $arguments = array())
-    {
-        if (substr($name, 0, 6) == 'assert') {
-            $this->validate($this->newConstraint(substr($name, 6), isset($arguments[0]) ? $arguments[0] : array()));
-        } else {
-            throw new \RunTimeException('Unable to recognize the command');
-        }
-
-        return $this;
-    }
-
-    /**
-     * @param Constraint $constraint
-     *
-     * @return ErrorElement
-     */
-    public function addConstraint(Constraint $constraint)
-    {
-        $this->validate($constraint);
-
-        return $this;
-    }
-
-    /**
-     * @param string $name
-     * @param bool   $key
-     *
-     * @return ErrorElement
-     */
-    public function with($name, $key = false)
-    {
-        $key           = $key ? $name . '.' . $key : $name;
-        $this->stack[] = $key;
-
-        $this->current = implode('.', $this->stack);
-
-        if (!isset($this->propertyPaths[$this->current])) {
-            $this->propertyPaths[$this->current] = new PropertyPath($this->current);
-        }
-
-        return $this;
-    }
-
-    /**
-     * @return ErrorElement
-     */
-    public function end()
-    {
-        array_pop($this->stack);
-
-        $this->current = implode('.', $this->stack);
-
-        return $this;
-    }
-
-    /**
-     * @param \Symfony\Component\Validator\Constraint $constraint
-     *
-     * @return void
-     */
-    protected function validate(Constraint $constraint)
-    {
-        $subPath = (string) $this->getCurrentPropertyPath();
-
-        $this->context->validateValue($this->getValue(), $constraint, $subPath, $this->group);
-    }
-
-    /**
-     * @return string
-     */
-    public function getFullPropertyPath()
-    {
-        if ($this->getCurrentPropertyPath()) {
-            return sprintf('%s.%s', $this->basePropertyPath, $this->getCurrentPropertyPath());
-        } else {
-            return $this->basePropertyPath;
-        }
-    }
-
-    /**
-     * Return the value linked to
-     *
-     * @return mixed
-     */
-    protected function getValue()
-    {
-        if ($this->current == '') {
-            return $this->subject;
-        }
-
-        $propertyAccessor = PropertyAccess::createPropertyAccessor();
-
-        return $propertyAccessor->getValue($this->subject, $this->getCurrentPropertyPath());
-    }
-
-    /**
-     * @return mixed
-     */
-    public function getSubject()
-    {
-        return $this->subject;
-    }
-
-    /**
-     * @param string $name
-     * @param array  $options
-     *
-     * @return
-     */
-    protected function newConstraint($name, array $options = array())
-    {
-        if (strpos($name, '\\') !== false && class_exists($name)) {
-            $className = (string) $name;
-        } else {
-            $className = 'Symfony\\Component\\Validator\\Constraints\\' . $name;
-        }
-
-        return new $className($options);
-    }
-
-    /**
-     * @return null|PropertyPath
-     */
-    protected function getCurrentPropertyPath()
-    {
-        if (!isset($this->propertyPaths[$this->current])) {
-            return null; //global error
-        }
-
-        return $this->propertyPaths[$this->current];
-    }
-
-    /**
-     * @param string|array $message
-     * @param array        $parameters
-     * @param null         $value
-     *
-     * @return ErrorElement
-     */
-    public function addViolation($message, $parameters = array(), $value = null)
-    {
-        if (is_array($message)) {
-            $value      = isset($message[2]) ? $message[2] : $value;
-            $parameters = isset($message[1]) ? (array) $message[1] : array();
-            $message    = isset($message[0]) ? $message[0] : 'error';
-        }
-
-        $subPath = (string) $this->getCurrentPropertyPath();
-
-        $this->context->addViolationAt($subPath, $message, $parameters, $value);
-
-        $this->errors[] = array($message, $parameters, $value);
-
-        return $this;
-    }
 
-    /**
-     * @return array
-     */
-    public function getErrors()
-    {
-        return $this->errors;
-    }
 }

+ 5 - 44
Validator/InlineValidator.php

@@ -10,51 +10,12 @@
 
 namespace Sonata\AdminBundle\Validator;
 
-use Symfony\Component\Validator\ConstraintValidator;
-use Symfony\Component\Validator\Constraint;
-use Symfony\Component\DependencyInjection\ContainerInterface;
-use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
+use Sonata\CoreBundle\Validator\InlineValidator as BaseInlineValidator;
 
-class InlineValidator extends ConstraintValidator
+/**
+ * @deprecated
+ */
+class InlineValidator extends BaseInlineValidator
 {
-    protected $container;
-
-    protected $constraintValidatorFactory;
-
-    /**
-     * @param \Symfony\Component\DependencyInjection\ContainerInterface            $container
-     * @param \Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory $constraintValidatorFactory
-     */
-    public function __construct(ContainerInterface $container, ConstraintValidatorFactoryInterface $constraintValidatorFactory)
-    {
-        $this->container                  = $container;
-        $this->constraintValidatorFactory = $constraintValidatorFactory;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function validate($value, Constraint $constraint)
-    {
-        $errorElement = new ErrorElement(
-            $value,
-            $this->constraintValidatorFactory,
-            $this->context,
-            $this->context->getGroup()
-        );
-
-        if ($constraint->isClosure()) {
-            $function = $constraint->getClosure();
-        } else {
-            if (is_string($constraint->getService())) {
-                $service = $this->container->get($constraint->getService());
-            } else {
-                $service = $constraint->getService();
-            }
-
-            $function = array($service, $constraint->getMethod());
-        }
 
-        call_user_func($function, $errorElement, $value);
-    }
 }