Преглед изворни кода

Improve the ErrorElement class

Thomas Rabaix пре 14 година
родитељ
комит
80a7bd9a40
1 измењених фајлова са 25 додато и 11 уклоњено
  1. 25 11
      Validator/ErrorElement.php

+ 25 - 11
Validator/ErrorElement.php

@@ -29,7 +29,9 @@ class ErrorElement
 
     protected $subject;
 
-    protected $current = '';
+    protected $current;
+
+    protected $basePropertyPath;
 
     public function __construct($subject, ConstraintValidatorFactory $constraintValidatorFactory, ExecutionContext $context, $group)
     {
@@ -37,6 +39,9 @@ class ErrorElement
         $this->context = $context;
         $this->group   = $group;
         $this->constraintValidatorFactory = $constraintValidatorFactory;
+
+        $this->current = '';
+        $this->basePropertyPath = $this->context->getPropertyPath();
     }
 
     public function __call($name, array $arguments = array())
@@ -76,27 +81,36 @@ class ErrorElement
         return $this;
     }
 
-    protected function validate($constraint)
+    protected function validate($constraint, $messageTemplate = null, $messageParameters = array())
     {
         $validator  = $this->constraintValidatorFactory->getInstance($constraint);
         $value      = $this->getValue();
 
         $validator->isValid($value, $constraint);
 
-        $this->context->setPropertyPath($this->getCurrentPropertyPath());
+        $this->context->setPropertyPath($this->getFullPropertyPath());
         $this->context->setGroup($this->group);
 
         $validator->initialize($this->context);
 
         if (!$validator->isValid($value, $constraint)) {
             $this->context->addViolation(
-                $validator->getMessageTemplate(),
-                $validator->getMessageParameters(),
+                $messageTemplate ?: $validator->getMessageTemplate(),
+                array_merge($validator->getMessageParameters(), $messageParameters),
                 $value
             );
         }
     }
 
+    public function getFullPropertyPath()
+    {
+        if ($this->getCurrentPropertyPath()) {
+            return sprintf('%s.%s', $this->basePropertyPath, $this->getCurrentPropertyPath());
+        } else {
+            return $this->basePropertyPath;
+        }
+    }
+
     /**
      * Return the value linked to
      *
@@ -126,24 +140,24 @@ class ErrorElement
     protected function getCurrentPropertyPath()
     {
         if (!isset($this->propertyPaths[$this->current])) {
-            throw new \RunTimeException('You must define a property by using the `with` method');
+            return null; //global error
         }
 
         return $this->propertyPaths[$this->current];
     }
 
-    public function addViolation($error)
+    public function addViolation($message, $parameters = array(), $value = null)
     {
-        $this->context->setPropertyPath($this->getCurrentPropertyPath());
+        $this->context->setPropertyPath($this->getFullPropertyPath());
         $this->context->setGroup($this->group);
 
-        if (!is_array($error)) {
-            $this->context->addViolation($error, array(), null);
+        if (!is_array($message)) {
+            $this->context->addViolation($message, $parameters, $value);
         } else {
             $this->context->addViolation(
                 isset($error[0]) ? $error[0] : 'error',
                 isset($error[1]) ? (array)$error[1] : array(),
-                isset($error[2]) ? $error[2] : null
+                isset($error[2]) ? $error[2] : $value
             );
         }