Given a path and an index, reads a Segment from the path and updates
the index.

@param string $path
@param int $index
@return Segment
@throws ValidationException
5036
private static function parseSegmentFromPath($path, &$index)
    {
        if ($index >= strlen($path)) {
            // A trailing '/' has caused the index to exceed the bounds
            // of the string - provide a helpful error message.
            throw self::parseError($path, strlen($path) - 1, "invalid trailing '/'");
        }
        if ($path[$index] === '{') {
            // Validate that the { has a matching }
            $closingBraceIndex = strpos($path, '}', $index);
            if ($closingBraceIndex === false) {
                throw self::parseError(
                    $path,
                    strlen($path),
                    "Expected '}' to match '{' at index $index, got end of string"
                );
            }

            $segmentStringLengthWithoutBraces = $closingBraceIndex - $index - 1;
            $segmentStringWithoutBraces = substr($path, $index + 1, $segmentStringLengthWithoutBraces);
            $index = $closingBraceIndex + 1;
            return self::parseVariableSegment($segmentStringWithoutBraces);
        } else {
            $nextSlash = strpos($path, '/', $index);
            if ($nextSlash === false) {
                $nextSlash = strlen($path);
            }
            $segmentString = substr($path, $index, $nextSlash - $index);
            $index = $nextSlash;
            return self::parse($segmentString, $path, $index);
        }
    }
395072
final protected function setIndex($index)
    {
        $this->model->leafIndex = $index;
        $this->model->updatePath();
        // Signal to all or any sub leaves that need to recompute their own path now.
        $this->leafPathChanged();
    }
370626
public function getUriSegment($index)
    {
        $segments = explode('/', $this->server->get('REQUEST_URI'));
        if (isset($segments[$index])) {
            return $segments[$index];
        }
        return false;
    }
1146404
public function addIndex($index)
    {
        if ($index instanceof Index) {
            $index = $index->getName();
        }

        if (!\is_scalar($index)) {
            throw new InvalidException('Invalid param type');
        }

        $this->_indices[] = (string) $index;

        return $this;
    }
766656
public function check($path)
    {
        if ($this->isExpanded === false) {
            $this->value = $this->_expand($this->value);
        }

        return Hash::check($this->value, $path);
    }
811666
private function validatePath($path)
    {
        $last = strlen($path)-1;
        if ($path[$last] == '/') {
            $path = substr($path, 0, -1);
        }
        return $path;
    }
1269839
public function setIndex($index)
	{
		if ( ! is_numeric($index) || ! is_int(+$index) || $index < 0) {
			throw new \InvalidArgumentException(
				__METHOD__."() expects parameter one, index, to be a positive "
				 ."integer or zero"
			);
		}
		
		$this->index = $index;
		
		return $this;
	}
267456
public function setPath($path)
    {
        if ($this->validatePath($path) === false) {
            require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception("Path \"$path\" is not a valid HTTP path");
        }

        $oldPath     = $this->_path;
        $this->_path = $path;

        return $oldPath;
    }
1215909
private function pathToNode(RoutePath $path, Route $route, int $index): TreeNode
    {
        if ($path instanceof AttributePath) {
            return $this->createAttributeNode($path);
        }

        if ($path instanceof StaticPath) {
            return $this->createStaticNode($path);
        }

        if ($path instanceof OptionalPath) {
            return $this->createOptionalNode();
        }

        return $this->createLeafNode($route, $index);
    }
167550
public function withPath($path)
    {
        if (!is_string($path) || strpos($path, '#') !== false || strpos($path, '?') !== false) {
            throw new \InvalidArgumentException('Path must be a string and cannot contain a fragment or query string');
        }

        if (!empty($path) && '/' !== substr($path, 0, 1)) {
            $path = '/' . $path;
        }

        return $this->_clone('path', $this->filterPath($path));
    }
706775
public function segment($delimiter, $index) {
        $parts = $this->split($delimiter);
        $returnValue = (isset($parts[$index - 1])) ? $parts[$index - 1] : '';

        return $this->createClone($returnValue);
    }
508272
public function segment($index) {
		if (isset($this->segments[$index])) {
			return $this->segments[$index];
		}

		return null;
	}
402848
public function segments(int $index = null)
    {
        $segments = collect(explode('/', $this->path()))
            ->filter(function ($value) {
                return $value !== '';
            })
            ->values()
            ->toArray();

        if (! is_null($index)) {
            return $this->segment($index);
        }

        return $segments;
    }
200870
protected function validateIndex($index, $indexType) {		
		if (is_null($this->resultMap)) {
			//find index column on given result
			if (!array_key_exists($index, $this->columnTypes)) {
				if (is_numeric($index) && array_key_exists(intval($index), $this->columnTypes))
					$index = intval($index);
				else
					throw new \UnexpectedValueException("Index column '$index' not found");
			}
				
			//get index type
			$indexColumn = $index;
			$indexType = is_null($indexType) ? $this->columnTypes[$index] : $indexType;
				
			//obtain index type handler
			$indexTypeHandler = $this->typeManager->getTypeHandler($indexType);
				
			if ($indexTypeHandler === false)
				throw new \UnexpectedValueException("Unknown type '$indexType' defined for index '$index'");
		}
		else {
			//find index property
			if (!array_key_exists($index, $this->properties))
				throw new \UnexpectedValueException("Index property '$index' was not found in result map");
				
			//obtain index column and type handler
			$indexColumn = $this->properties[$index]->getColumn();
			$indexTypeHandler = $this->typeHandlers[$index];
		}
		
		return [$indexColumn, $indexTypeHandler];
	}
656841
public function delete ($path) {
		list ($first, $rest) = self::getFirstAndRestPathElements($path);

		# If we are going still deeper...
		if (!is_null($rest)) {
			if (array_key_exists($first, $this -> data)) {
				if ($this -> data[$first] instanceof MutableTreeInterface) {
					/** @var MutableTreeInterface $tree */
					$tree = $this -> data[$first];
					$tree -> delete($rest);
				} else {
					throw new InvalidKeyException("Value at key `" . $first . "` is a scalar; cannot step further down the requested path `" . $rest . "`");
				}
			} else {
				throw new NonExistentKeyException("Key `" . $first . "` does not exist.");
			}
		} else {
			unset($this -> data[$first]);
		}

		return $this;
	}
1232856
public function seek($index)
    {
        $this->rewind();
        $position = 0;

        while ($position < $index && $this->valid()) {
            $this->next();
            $position++;
        }

        if (!$this->valid()) {
            throw new CollectionException('Invalid seek position');
        }
    }
568117
public function segment($index, $default = null)
    {
        return array_key_exists($index, $this->segments()) ? $this->segments()[$index] : $default;
    }