XPath::generateExpression

@param array  $segments
@param string $prefix Specifies the nesting of nodes

@return string XPath expression

@throws InvalidArgumentException if you neither specify tag name nor attributes
356413
private function generateExpression($segments, $prefix = '//')
    {
        $tagName = isset($segments[ 'tag' ]) ? $segments[ 'tag' ] : '*';

        $attributes = [];

        // if the id attribute specified
        if (isset($segments[ 'id' ])) {
            $attributes[] = sprintf('@id="%s"', $segments[ 'id' ]);
        }

        // if the class attribute specified
        if (isset($segments[ 'classes' ])) {
            foreach ($segments[ 'classes' ] as $class) {
                $attributes[] = sprintf('contains(concat(" ", normalize-space(@class), " "), " %s ")', $class);
            }
        }

        // if the attributes specified
        if (isset($segments[ 'attributes' ])) {
            foreach ($segments[ 'attributes' ] as $name => $value) {
                $attributes[] = $this->fetchCssAttributeSelector($name, $value);
            }
        }

        // if the pseudo class specified
        if (isset($segments[ 'pseudo' ])) {
            $expression = isset($segments[ 'expr' ]) ? trim($segments[ 'expr' ]) : '';

            $parameters = explode(',', $expression);

            $attributes[] = $this->fetchCssPseudoSelector($segments[ 'pseudo' ], $parameters, $tagName);
        }

        if (count($attributes) === 0 and ! isset($segments[ 'tag' ])) {
            throw new InvalidArgumentException(
                'The array of segments should contain the name of the tag or at least one attribute'
            );
        }

        $xpath = $prefix . $tagName;

        if ($count = count($attributes)) {
            $xpath .= ($count > 1)
                ? sprintf('[(%s)]', implode(') and (', $attributes))
                : sprintf(
                    '[%s]',
                    $attributes[ 0 ]
                );
        }

        return $xpath;
    }
217768
private function getHypertextXml($routes = [])
    {
        // Check if we have routes
        if (!$routes) {
            return '';
        }

        // Loop through the routes and add them as links to the xml
        $xml = '';
        foreach ($routes as $name => $route) {
            $xml .= "<link rel=\"{$name}\" href=\"{$route}\"/>\n";
        }
        return $xml;
    }
495223
public function generate(array $components)
    {
        $uri = $components['scheme'] . '://'
             . $components['authority']
             . $components['path'];

        if (array_key_exists('query', $components) && strlen($components['query'])) {
            $uri .= '?' . $components['query'];
        }
        if (array_key_exists('fragment', $components)) {
            $uri .= '#' . $components['fragment'];
        }

        return $uri;
    }
287173
private function prepareSegment(string $name, string $segment): string
    {
        if (!empty($segment)) {
            return $this->filterSegment($segment);
        }

        if (!isset($this->constrains[$name])) {
            return self::DEFAULT_SEGMENT;
        }

        if (is_array($this->constrains[$name])) {
            $values = array_map([$this, 'filterSegment'], $this->constrains[$name]);

            return join('|', $values);
        }

        return $this->filterSegment((string)$this->constrains[$name]);
    }
1095682
public function parse(&$segments)
    {
        $path = implode('/', $segments);
        $vars = array();

        $matches = array();
        if (preg_match('/(\d+)-([^\/]+)/', $path, $matches)) {
            $vars['alias'] = $matches[2];
            $path = str_replace($matches[0], $matches[1], $path);
            $segments = array_filter(explode('/', $path));
        }

        $path = implode('/', $segments);

        $matches = array();

        if (preg_match('/@(\w+)/', $path, $matches)) {
            $vars['oid'] = $matches[1];
            if (isset($matches[2])) {
                $vars['view'] = $matches[2];
            }
            $path = ltrim(str_replace($matches[0], '', $path), '/');
            $segments = array_filter(explode('/', $path));
        }

        $vars = array_merge($vars, parent::parse($segments));

        if (isset($vars['id']) && current($segments)) {
            $vars['alias'] = array_shift($segments);
        }

        return $vars;
    }
349437
protected function wrapSegments($segments)
    {
        return collect($segments)->map(function ($segment, $key) use ($segments) {
            return $key == 0 && count($segments) > 1
                            ? $this->wrapTable($segment)
                            : $this->wrapValue($segment);
        })->implode('.');
    }
78143
public static function setLastSegment($segments)
    {
        if (is_array($segments)) {
            $vars = $segments;
        } else {
            $vars = self::segmentUrl($segments);
        }

        return end($vars);
    }
69120
public static function createFromSegments(iterable $segments, int $type = self::IS_RELATIVE): self
    {
        static $type_list = [self::IS_ABSOLUTE => 1, self::IS_RELATIVE => 1];

        if (!isset($type_list[$type])) {
            throw new PathTypeNotFound(sprintf('"%s" is an invalid or unsupported %s type', $type, self::class));
        }

        $pathSegments = [];
        foreach ($segments as $value) {
            if (!is_scalar($value) && !method_exists($value, '__toString')) {
                throw new TypeError('The submitted segments are invalid');
            }
            $pathSegments[] = (string) $value;
        }

        $path = implode(self::SEPARATOR, $pathSegments);
        if (static::IS_ABSOLUTE !== $type) {
            return new self(ltrim($path, self::SEPARATOR));
        }

        if (self::SEPARATOR !== ($path[0] ?? '')) {
            return new self(self::SEPARATOR.$path);
        }

        return new self($path);
    }
563578
public static function cssToXpath($selector, $prefix = '//')
    {
        $paths = [];

        while ($selector !== '') {
            list($xpath, $selector) = static::parseAndConvertSelector($selector, $prefix);

            if (substr($selector, 0, 1) === ',') {
                $selector = trim($selector, ', ');
            }

            $paths[] = $xpath;
        }

        return implode('|', $paths);
    }
957775
private function prefixPaths (array $paths, string $prefix) : array
    {
        $result = [];

        foreach ($paths as $namespace => $path)
        {
            $result[$namespace] = "{$prefix}/" . trim($path, "/");
        }

        return $result;
    }
1269413
private function generateOverviewsOfSegment($segmentId)
    {
        /** @var AnalyticsSegmentRepository $segmentRepository */
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
        $segment = $segmentRepository->find($segmentId);

        if (!$segment) {
            throw new \InvalidArgumentException('Unknown segment ID');
        }

        // init the segment
        $segmentRepository->initSegment($segment);
    }
140499
protected function loadStructureTags($path, $xpath)
    {
        $result = [];

        foreach ($xpath->query($path) as $node) {
            $tag = [
                'name' => null,
                'attributes' => [],
            ];

            foreach ($node->attributes as $key => $attr) {
                if (in_array($key, ['name'])) {
                    $tag[$key] = $attr->value;
                } else {
                    $tag['attributes'][$key] = $attr->value;
                }
            }

            if (!isset($tag['name'])) {
                // this should not happen because of the XSD validation
                throw new \InvalidArgumentException('Tag does not have a name in template definition');
            }

            $result[] = $tag;
        }

        return $result;
    }
981027
public function compileExpression(QueryBuilder $queryBuilder, Expr $expr, bool $subSelectAlias = false)
    {
        switch (true) {
            case $expr instanceof Expression\Count:
                return $this->doctrinePlatform->getCountExpression('*');

            case $expr instanceof Expression\SimpleAggregate:
                return $this->compileSimpleAggregate($queryBuilder, $expr);

            case $expr instanceof Expression\BinOp:
                return (string)$this->compileBinOp($queryBuilder, $expr);

            case $expr instanceof Expression\ColumnExpr:
                return ''
                . $this->doctrinePlatform->quoteSingleIdentifier($expr->getTable())
                . '.'
                . $this->doctrinePlatform->quoteSingleIdentifier($expr->getName());

            case $expr instanceof Expression\Parameter:
                return $queryBuilder->createPositionalParameter($this->platform->mapValueToDbFormat($expr->getType(), $expr->getValue()));

            case $expr instanceof Expression\Tuple:
                return $this->compileExpressions($queryBuilder, $expr->getExpressions());

            case $expr instanceof Expression\UnaryOp:
                return $this->compileUnaryOp($queryBuilder, $expr);

            case $expr instanceof Expression\SubSelect:
                return $this->compileSubSelect($queryBuilder, $expr, $subSelectAlias);
        }

        throw InvalidArgumentException::format('Unknown expression type: ' . get_class($expr));
    }
201496
public static function appendSegment($uri, string $segment)
    {
        $uri = self::filterUri($uri);

        return self::normalizePath($uri, (new HierarchicalPath($uri->getPath()))->append($segment));
    }
1108007
private function loadValues(\DOMXPath $xpath, \DOMNode $node, $keys, $prefix = '@')
    {
        $result = [];

        foreach ($keys as $key) {
            $result[$key] = $this->getValueFromXPath($prefix . $key, $xpath, $node);
        }

        return $result;
    }
202955
private function traverse(array $segments, &$result = array(), $node = null)
    {
        $path = array();

        if (null !== $node) {
            $path = explode('/', substr($node->getPath(), 1));
        }

        do {
            list($element, $bitmask) = array_shift($segments);

            if ($bitmask & SelectorParser::T_STATIC) {
                $path[] = $element;

                if ($bitmask & SelectorParser::T_LAST) {
                    if ($node = $this->getNode($path)) {
                        $result[] = $node;
                        break;
                    }
                }
            }

            if ($bitmask & SelectorParser::T_PATTERN) {
                if (null === $parentNode = $this->getNode($path)) {
                    return;
                }

                $children = $this->getChildren($parentNode, $element);

                foreach ($children as $child) {
                    if ($bitmask & SelectorParser::T_LAST) {
                        $result[] = $child;
                    } else {
                        $this->traverse($segments, $result, $child);
                    }
                }

                return;
            }
        } while ($segments);
    }
1082425
public function parse(&$segments)
    {
        $query = array();
        $path = implode('/', $segments);
        if ($path == 'new') {
            array_pop($segments);
            $query = array_merge(parent::parse($segments), array('new' => true));
        } else {
            return parent::parse($segments);
        }

        return $query;
    }