Пример #1
0
 /**
  * Reads the path from an object up to a given path index.
  *
  * @param object|array          $objectOrArray The object or array to read from
  * @param PropertyPathInterface $propertyPath  The property path to read
  * @param int                   $lastIndex     The index up to which should be read
  *
  * @return array The values read in the path.
  *
  * @throws UnexpectedTypeException If a value within the path is neither object nor array.
  */
 private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex)
 {
     if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
         throw new UnexpectedTypeException($objectOrArray, 'object or array');
     }
     $propertyValues = array();
     for ($i = 0; $i < $lastIndex; ++$i) {
         $property = $propertyPath->getElement($i);
         $isIndex = $propertyPath->isIndex($i);
         // Create missing nested arrays on demand
         if ($isIndex && ($objectOrArray instanceof \ArrayAccess && !isset($objectOrArray[$property]) || is_array($objectOrArray) && !array_key_exists($property, $objectOrArray))) {
             $objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
         }
         if ($isIndex) {
             $propertyValue =& $this->readIndex($objectOrArray, $property);
         } else {
             $propertyValue =& $this->readProperty($objectOrArray, $property);
         }
         $objectOrArray =& $propertyValue[self::VALUE];
         // the final value of the path must not be validated
         if ($i + 1 < $propertyPath->getLength() && !is_object($objectOrArray) && !is_array($objectOrArray)) {
             throw new UnexpectedTypeException($objectOrArray, 'object or array');
         }
         $propertyValues[] =& $propertyValue;
     }
     return $propertyValues;
 }
Пример #2
0
 /**
  * Reads the path from an object up to a given path index.
  *
  * @param object|array          $objectOrArray The object or array to read from
  * @param PropertyPathInterface $propertyPath  The property path to read
  * @param int                   $lastIndex     The index up to which should be read
  *
  * @return array The values read in the path.
  *
  * @throws UnexpectedTypeException If a value within the path is neither object nor array.
  */
 private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $ignoreInvalidIndices = true)
 {
     $propertyValues = array();
     for ($i = 0; $i < $lastIndex; ++$i) {
         if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
             throw new UnexpectedTypeException($objectOrArray, 'object or array');
         }
         $property = $propertyPath->getElement($i);
         $isIndex = $propertyPath->isIndex($i);
         $isArrayAccess = is_array($objectOrArray) || $objectOrArray instanceof \ArrayAccess;
         // Create missing nested arrays on demand
         if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) {
             if (!$ignoreInvalidIndices) {
                 if (!is_array($objectOrArray)) {
                     if (!$objectOrArray instanceof \Traversable) {
                         throw new NoSuchIndexException(sprintf('Cannot read property "%s".', $property));
                     }
                     $objectOrArray = iterator_to_array($objectOrArray);
                 }
                 throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are "%s"', $property, print_r(array_keys($objectOrArray), true)));
             }
             $objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
         }
         if ($isIndex) {
             $propertyValue =& $this->readIndex($objectOrArray, $property);
         } else {
             $propertyValue =& $this->readProperty($objectOrArray, $property);
         }
         $objectOrArray =& $propertyValue[self::VALUE];
         $propertyValues[] =& $propertyValue;
     }
     return $propertyValues;
 }
 /**
  * Reads the path from an object up to a given path index.
  *
  * @param array                 $zval                 The array containing the object or array to read from
  * @param PropertyPathInterface $propertyPath         The property path to read
  * @param int                   $lastIndex            The index up to which should be read
  * @param bool                  $ignoreInvalidIndices Whether to ignore invalid indices or throw an exception
  *
  * @return array The values read in the path.
  *
  * @throws UnexpectedTypeException If a value within the path is neither object nor array.
  * @throws NoSuchIndexException    If a non-existing index is accessed
  */
 private function readPropertiesUntil($zval, PropertyPathInterface $propertyPath, $lastIndex, $ignoreInvalidIndices = true)
 {
     if (!is_object($zval[self::VALUE]) && !is_array($zval[self::VALUE])) {
         throw new UnexpectedTypeException($zval[self::VALUE], $propertyPath, 0);
     }
     // Add the root object to the list
     $propertyValues = array($zval);
     for ($i = 0; $i < $lastIndex; ++$i) {
         $property = $propertyPath->getElement($i);
         $isIndex = $propertyPath->isIndex($i);
         if ($isIndex) {
             // Create missing nested arrays on demand
             if ($zval[self::VALUE] instanceof \ArrayAccess && !$zval[self::VALUE]->offsetExists($property) || is_array($zval[self::VALUE]) && !isset($zval[self::VALUE][$property]) && !array_key_exists($property, $zval[self::VALUE])) {
                 if (!$ignoreInvalidIndices) {
                     if (!is_array($zval[self::VALUE])) {
                         if (!$zval[self::VALUE] instanceof \Traversable) {
                             throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s".', $property, (string) $propertyPath));
                         }
                         $zval[self::VALUE] = iterator_to_array($zval[self::VALUE]);
                     }
                     throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s". Available indices are "%s".', $property, (string) $propertyPath, print_r(array_keys($zval[self::VALUE]), true)));
                 }
                 if ($i + 1 < $propertyPath->getLength()) {
                     if (isset($zval[self::REF])) {
                         $zval[self::VALUE][$property] = array();
                         $zval[self::REF] = $zval[self::VALUE];
                     } else {
                         $zval[self::VALUE] = array($property => array());
                     }
                 }
             }
             $zval = $this->readIndex($zval, $property);
         } else {
             $zval = $this->readProperty($zval, $property);
         }
         // the final value of the path must not be validated
         if ($i + 1 < $propertyPath->getLength() && !is_object($zval[self::VALUE]) && !is_array($zval[self::VALUE])) {
             throw new UnexpectedTypeException($zval[self::VALUE], $propertyPath, $i + 1);
         }
         if (isset($zval[self::REF]) && (0 === $i || isset($propertyValues[$i - 1][self::IS_REF_CHAINED]))) {
             // Set the IS_REF_CHAINED flag to true if:
             // current property is passed by reference and
             // it is the first element in the property path or
             // the IS_REF_CHAINED flag of its parent element is true
             // Basically, this flag is true only when the reference chain from the top element to current element is not broken
             $zval[self::IS_REF_CHAINED] = true;
         }
         $propertyValues[] = $zval;
     }
     return $propertyValues;
 }
Пример #4
0
 /**
  * Reads the path from an object up to a given path index.
  *
  * @param object|array          $objectOrArray        The object or array to read from
  * @param PropertyPathInterface $propertyPath         The property path to read
  * @param int                   $lastIndex            The index up to which should be read
  * @param bool                  $ignoreInvalidIndices Whether to ignore invalid indices
  *                                                    or throw an exception
  *
  * @return array The values read in the path.
  *
  * @throws UnexpectedTypeException If a value within the path is neither object nor array.
  * @throws NoSuchIndexException    If a non-existing index is accessed
  */
 private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $ignoreInvalidIndices = true)
 {
     if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
         throw new UnexpectedTypeException($objectOrArray, $propertyPath, 0);
     }
     $propertyValues = array();
     for ($i = 0; $i < $lastIndex; ++$i) {
         $property = $propertyPath->getElement($i);
         $isIndex = $propertyPath->isIndex($i);
         // Create missing nested arrays on demand
         if ($isIndex && ($objectOrArray instanceof \ArrayAccess && !isset($objectOrArray[$property]) || is_array($objectOrArray) && !array_key_exists($property, $objectOrArray))) {
             if (!$ignoreInvalidIndices) {
                 if (!is_array($objectOrArray)) {
                     if (!$objectOrArray instanceof \Traversable) {
                         throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s".', $property, (string) $propertyPath));
                     }
                     $objectOrArray = iterator_to_array($objectOrArray);
                 }
                 throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s". Available indices are "%s".', $property, (string) $propertyPath, print_r(array_keys($objectOrArray), true)));
             }
             $objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
         }
         if ($isIndex) {
             $propertyValue =& $this->readIndex($objectOrArray, $property);
         } else {
             $propertyValue =& $this->readProperty($objectOrArray, $property);
         }
         $objectOrArray =& $propertyValue[self::VALUE];
         // the final value of the path must not be validated
         if ($i + 1 < $propertyPath->getLength() && !is_object($objectOrArray) && !is_array($objectOrArray)) {
             throw new UnexpectedTypeException($objectOrArray, $propertyPath, $i + 1);
         }
         $propertyValues[] =& $propertyValue;
     }
     return $propertyValues;
 }
    /**
     * 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 $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, PropertyPathInterface $path, $pathOffset = 0, $pathLength = 0)
    {
        if (!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);
        }
    }
 /**
  * @param \Symfony\Component\PropertyAccess\PropertyPathInterface $propertyPath
  * @return string
  */
 private function getMostNestedProperty(PropertyPathInterface $propertyPath)
 {
     return $propertyPath->getElement($propertyPath->getLength() - 1);
 }