/**
  * @param string|PropertyPathInterface $propertyPath  The property path to modify
  *
  * @throws NoSuchIndexException
  */
 public function removeParameter($propertyPath)
 {
     $propertyPathAccessor = PropertyAccess::createPropertyAccessor();
     if (!$propertyPath instanceof PropertyPathInterface) {
         $propertyPath = new PropertyPath($propertyPath);
     }
     if (1 === $propertyPath->getLength()) {
         $buffer =& $this->parameters;
         unset($buffer[$propertyPath->getElement(0)]);
     } else {
         $parentPropertyPath = $propertyPath->getParent();
         $buffer = $propertyPathAccessor->getValue($this->parameters, $parentPropertyPath);
         unset($buffer[$propertyPath->getElement($propertyPath->getLength() - 1)]);
         $propertyPathAccessor->setValue($this->parameters, $parentPropertyPath, $buffer);
     }
 }
 /**
  * @expectedException \OutOfBoundsException
  */
 public function testGetElementDoesNotAcceptNegativeIndices()
 {
     $propertyPath = new PropertyPath('grandpa.parent[child]');
     $propertyPath->getElement(-1);
 }
 /**
  * @dataProvider provideCustomDataErrorTests
  */
 public function testCustomDataErrorMapping($target, $mapFrom, $mapTo, $childName, $childPath, $grandChildName, $grandChildPath, $violationPath)
 {
     $violation = $this->getConstraintViolation($violationPath);
     $parent = $this->getForm('parent', null, null, array($mapFrom => $mapTo));
     $child = $this->getForm($childName, $childPath);
     $grandChild = $this->getForm($grandChildName, $grandChildPath);
     $parent->add($child);
     $child->add($grandChild);
     // Add a field mapped to the first element of $mapFrom
     // to try to distract the algorithm
     // Only add it if we expect the error to come up on a different
     // level than LEVEL_0, because in this case the error would
     // (correctly) be mapped to the distraction field
     if ($target !== self::LEVEL_0) {
         $mapFromPath = new PropertyPath($mapFrom);
         $mapFromPrefix = $mapFromPath->isIndex(0) ? '[' . $mapFromPath->getElement(0) . ']' : $mapFromPath->getElement(0);
         $distraction = $this->getForm('distraction', $mapFromPrefix);
         $parent->add($distraction);
     }
     $this->mapper->mapViolation($violation, $parent);
     if ($target !== self::LEVEL_0) {
         $this->assertCount(0, $distraction->getErrors(), 'distraction should not have an error, but has one');
     }
     if (self::LEVEL_0 === $target) {
         $this->assertEquals(array($this->getFormError($violation)), $parent->getErrors(), $parent->getName() . ' should have an error, but has none');
         $this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
         $this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
     } elseif (self::LEVEL_1 === $target) {
         $this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
         $this->assertEquals(array($this->getFormError($violation)), $child->getErrors(), $childName . ' should have an error, but has none');
         $this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
     } else {
         $this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
         $this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
         $this->assertEquals(array($this->getFormError($violation)), $grandChild->getErrors(), $grandChildName . ' should have an error, but has none');
     }
 }
 /**
  * Replaces a sub-path by a different (sub-) path.
  *
  * @param integer                      $offset     The offset at which to replace.
  * @param integer                      $length     The length of the piece to replace.
  * @param PropertyPathInterface|string $path       The path to insert.
  * @param integer                      $pathOffset The offset where the inserted piece
  *                                                 starts in $path.
  * @param integer                      $pathLength The length of the inserted piece.
  *                                                 If 0, the full path is inserted.
  *
  * @throws OutOfBoundsException If the offset is invalid
  */
 public function replace($offset, $length, $path, $pathOffset = 0, $pathLength = 0)
 {
     if (is_string($path)) {
         $path = new PropertyPath($path);
     }
     if ($offset < 0 && abs($offset) <= $this->getLength()) {
         $offset = $this->getLength() + $offset;
     } elseif (!isset($this->elements[$offset])) {
         throw new OutOfBoundsException('The offset ' . $offset . ' is not within the property path');
     }
     if (0 === $pathLength) {
         $pathLength = $path->getLength() - $pathOffset;
     }
     $this->resize($offset, $length, $pathLength);
     for ($i = 0; $i < $pathLength; ++$i) {
         $this->elements[$offset + $i] = $path->getElement($pathOffset + $i);
         $this->isIndex[$offset + $i] = $path->isIndex($pathOffset + $i);
     }
 }
Exemple #5
0
 /**
  * @param $path
  * @param $haystack
  * @return bool
  */
 private function valueExist($path, array $haystack)
 {
     $propertyPath = new PropertyPath($path);
     $length = $propertyPath->getLength();
     $valueExist = true;
     for ($i = 0; $i < $length; ++$i) {
         $property = $propertyPath->getElement($i);
         $isIndex = $propertyPath->isIndex($i);
         $propertyExist = $this->arrayPropertyExists($property, $haystack);
         if ($isIndex && !$propertyExist) {
             $valueExist = false;
             break;
         }
     }
     unset($propertyPath);
     return $valueExist;
 }