Ejemplo n.º 1
0
 protected function updateProperty(&$objectOrArray, PropertyPath $propertyPath)
 {
     if (is_object($objectOrArray) && $propertyPath->isIndex()) {
         if (!$objectOrArray instanceof \ArrayAccess) {
             throw new InvalidPropertyException(sprintf('Index "%s" cannot be modified in object of type "%s" because it doesn\'t implement \\ArrayAccess', $propertyPath->getCurrent(), get_class($objectOrArray)));
         }
         $objectOrArray[$propertyPath->getCurrent()] = $this->getData();
     } else {
         if (is_object($objectOrArray)) {
             $reflClass = new \ReflectionClass($objectOrArray);
             $setter = 'set' . ucfirst($propertyPath->getCurrent());
             $property = $propertyPath->getCurrent();
             if ($reflClass->hasMethod($setter)) {
                 if (!$reflClass->getMethod($setter)->isPublic()) {
                     throw new PropertyAccessDeniedException(sprintf('Method "%s()" is not public in class "%s"', $setter, $reflClass->getName()));
                 }
                 $objectOrArray->{$setter}($this->getData());
             } else {
                 if ($reflClass->hasProperty($property)) {
                     if (!$reflClass->getProperty($property)->isPublic()) {
                         throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?', $property, $reflClass->getName(), ucfirst($property)));
                     }
                     $objectOrArray->{$property} = $this->getData();
                 } else {
                     throw new InvalidPropertyException(sprintf('Neither element "%s" nor method "%s()" exists in class "%s"', $property, $setter, $reflClass->getName()));
                 }
             }
         } else {
             $objectOrArray[$propertyPath->getCurrent()] = $this->getData();
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Binds the form with values and files.
  *
  * This method is final because it is very easy to break a form when
  * overriding this method and adding logic that depends on $taintedFiles.
  * You should override doBind() instead where the uploaded files are
  * already merged into the data array.
  *
  * @param  array $taintedValues  The form data of the $_POST array
  * @param  array $taintedFiles   An array of uploaded files
  * @return boolean               Whether the form is valid
  */
 public final function bind($taintedValues, array $taintedFiles = null)
 {
     if ($taintedFiles === null) {
         if ($this->isMultipart() && $this->getParent() === null) {
             throw new \InvalidArgumentException('You must provide a files array for multipart forms');
         }
         $taintedFiles = array();
     }
     if (null === $taintedValues) {
         $taintedValues = array();
     }
     $this->doBind(self::deepArrayUnion($taintedValues, $taintedFiles));
     if ($this->getParent() === null) {
         if ($violations = $this->validator->validate($this, $this->getValidationGroups())) {
             foreach ($violations as $violation) {
                 $propertyPath = new PropertyPath($violation->getPropertyPath());
                 if ($propertyPath->getCurrent() == 'data') {
                     $type = self::DATA_ERROR;
                     $propertyPath->next();
                     // point at the first data element
                 } else {
                     $type = self::FIELD_ERROR;
                 }
                 $this->addError($violation->getMessage(), $propertyPath, $type);
             }
         }
     }
 }
Ejemplo n.º 3
0
 public function testValidPropertyPath_zero()
 {
     $path = new PropertyPath('0');
     $this->assertEquals('0', $path->getCurrent());
 }
Ejemplo n.º 4
0
 /**
  * {@inheritDoc}
  */
 public function addError($message, PropertyPath $path = null, $type = null)
 {
     if ($path !== null) {
         if ($type === self::FIELD_ERROR && $path->hasNext()) {
             $path->next();
             if ($this->has($path->getCurrent()) && !$this->get($path->getCurrent())->isHidden()) {
                 $this->get($path->getCurrent())->addError($message, $path, $type);
                 return;
             }
         } else {
             if ($type === self::DATA_ERROR) {
                 $iterator = new RecursiveFieldsWithPropertyPathIterator($this);
                 $iterator = new \RecursiveIteratorIterator($iterator);
                 foreach ($iterator as $field) {
                     if (null !== ($fieldPath = $field->getPropertyPath())) {
                         $fieldPath->rewind();
                         if ($fieldPath->getCurrent() === $path->getCurrent() && !$field->isHidden()) {
                             if ($path->hasNext()) {
                                 $path->next();
                             }
                             $field->addError($message, $path, $type);
                             return;
                         }
                     }
                 }
             }
         }
     }
     parent::addError($message);
 }