Build path

@param array $parts
@param array $params
@param boolean $optional
@return string
@throws Exception
549373
protected function buildPath(array $parts, array $params, $optional = false) {
        $path = '';
        foreach ($parts as $part) {
            switch ($part[0]) {
                case 'literal':
                    $path .= $part[1];
                    break;
                case 'parameter':
                    if (!isset($params[$part[1]])) {
                        if (!$optional) {
                            throw new Exception(
                                __('Missing parameter "{name}"', [
                                    'name' => $path[1]
                                ])
                            );
                        }
                        return '';
                    }
                    $path .= rawurlencode($params[$part[1]]);
                    break;
                case 'optional':
                    $segment = $this->buildPath($part[1], $params, true);
                    if ($segment !== '') {
                        $path .= $segment;
                    }
                    break;
            }
        }
        return $path;
    }
772393
public function build($params)
    {
        return ((isset($params['scheme'])) ? $params['scheme'] . '://' : '')
            . ((isset($params['user'])) ? $params['user'] . ((isset($params['pass'])) ? ':' . $params['pass'] : '') . '@' : '')
            . ((isset($params['host'])) ? $params['host'] : '')
            . ((isset($params['port'])) ? ':' . $params['port'] : '')
            . ((isset($params['path'])) ? $params['path'] : '')
            . ((isset($params['query'])) ? '?' . $params['query'] : '')
            . ((isset($params['fragment'])) ? '#' . $params['fragment'] : '');
    }
771180
public static function join_paths($part = null)
    {
        $args = func_get_args();
        if (count($args) === 1 && is_array($args[0])) {
            $args = $args[0];
        }

        $parts = array();
        foreach ($args as $arg) {
            $part = trim($arg, ' \\/');
            if ($part) {
                $parts[] = $part;
            }
        }

        return implode('/', $parts);
    }
106357
protected function buildUrl($parts, $params)
    {
        $url = '';

        if (isset($parts['host'])) {
            if ($this->isRelativeDomain) {
                $url .= '//'.$parts['host'];
            } else {
                $url .= $parts['scheme'].'://'.$parts['host'];
            }

            if (isset($parts['port'])) {
                $url .= ':'.$parts['port'];
            }
        }

        $url .= $parts['path'];

        if (count($params)) {
            $url .= '?'.http_build_query($params);
        }

        return $url;
    }
889059
public function url(...$parts)
    {
        $uri = implode('/', $parts);
        return (string)$this->request->uri->make($uri);
    }
676199
protected function _path($id, $parts = false)
    {
        $partsArray = array();
        $root = $this->_options['cache_dir'];
        $prefix = $this->_options['file_name_prefix'];
        if ($this->_options['hashed_directory_level']>0) {
            $hash = hash('adler32', $id);
            for ($i=0 ; $i < $this->_options['hashed_directory_level'] ; $i++) {
                $root = $root . $prefix . '--' . substr($hash, 0, $i + 1) . DIRECTORY_SEPARATOR;
                $partsArray[] = $root;
            }
        }
        if ($parts) {
            return $partsArray;
        } else {
            return $root;
        }
    }
1296733
public static function fromParts(array $parts): UriInterface
    {
        $uri = new self();
        $uri->applyParts($parts);
        $uri->validateState();

        return $uri;
    }
938178
protected static function buildUrlMergeUrlParts($parts)
    {
        $partsDefault = array(
            'scheme'    => null,
            'host'      => null,
            'port'      => null,
            'path'      => '/',
            'query'     => '',
            'fragment'  => null,
        );
        if (!empty($parts['url'])) {
            $urlParts = self::parseUrl(htmlspecialchars_decode($parts['url']));
            if (!$urlParts['scheme']) {
                $urlParts['scheme'] = 'omit';
            }
            if (!empty($parts['path'])) {
                // discard url's query and fragment
                $urlParts['query']      = null;
                $urlParts['params']     = array();
                $urlParts['fragment']   = null;
            }
            if ($urlParts['query'] && !empty($parts['query'])) {
                // merge the queries
                $paramsParts = Str::parse($parts['query']);
                $params = ArrayUtil::mergeDeep($urlParts['params'], $paramsParts);
                $parts['query'] = self::buildQueryString($params);
            }
            $partsDefault = array_merge($partsDefault, $urlParts);
        }
        $parts = array_merge($partsDefault, $parts);
        return $parts;
    }
177573
protected function buildStepsPath(string ...$parts)
    {
        return static::buildPath('repositories', $this->username, $this->repo, 'pipelines', $this->pipeline, 'steps', ...$parts);
    }
81738
protected function buildCommentsPath(string ...$parts)
    {
        return static::buildPath('repositories', $this->username, $this->repo, 'commit', $this->commit, 'comments', ...$parts);
    }
696932
public static function joinPaths(...$parts)
    {
        $combined = null;
        $parts = array_filter($parts);
        array_walk_recursive($parts, function ($part) use (&$combined) {
            // Normalise path
            $part = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $part);
            $combined = $combined
                ? (rtrim($combined, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $part)
                : $part;
        });
        return $combined;
    }
357116
public function makeLayoutPartial($partial, $params = [])
    {
        if (!File::isLocalPath($partial) && !File::isPathSymbol($partial)) {
            $folder = strpos($partial, '/') !== false ? dirname($partial) . '/' : '';
            $partial = $folder . '_' . strtolower(basename($partial));
        }

        return $this->makeLayout($partial, $params);
    }
653947
protected function _buildStringFromParsed(array $parts, $params = [])
    {
        if ($params instanceof \Traversable)
            $params = StdTravers::of($params)->toArray();

        if (! is_array($params) )
            throw new \InvalidArgumentException(sprintf(
                'Parameters must be an array or Traversable; given: (%s).'
                , \Poirot\Std\flatten($params)
            ));


        // regard to recursive function call
        $isOptional = false;
        $args = func_get_args();
        if ($args && isset($args[2]))
            $isOptional = $args[2];

        # Check For Presented Values in Optional Segment
        // consider this "/[@:username{{\w+}}][-:userid{{\w+}}]"
        // if username not presented as params injected then first part include @ as literal
        // not rendered.
        // optional part only render when all parameter is present
        if ($isOptional) {
            foreach ($parts as $parsed) {
                if (! isset($parsed['_parameter_']) )
                    continue;

                ## need parameter
                $neededParameterName = $parsed['_parameter_'];
                if (! (isset($params[$neededParameterName]) && $params[$neededParameterName] !== '') )
                    return '';
            }
        }


        $return    = '';
        // [0 => ['_literal_' => 'localhost'], 1=>['_optional' => ..] ..]
        foreach ($parts as $parsed) {
            $definition_name  = key($parsed);
            $definition_value = $parsed[$definition_name];
            // $parsed can also have extra parsed data options
            // _parameter_ String(3) => tld \
            // tld String(4)         => .com
            switch ($definition_name)
            {
                case '_literal_':
                    $return .= $definition_value;
                    break;

                case '_parameter_':
                    if (! isset($params[$definition_value]) ) {
                        if ($isOptional)  return '';

                        throw new \InvalidArgumentException(sprintf(
                            'Missing parameter (%s).'
                            , $definition_value
                        ));
                    }

                    if (! \Poirot\Std\isStringify($params[$definition_value]) )
                        throw new \Exception(sprintf(
                            'Parameter %s is not stringify; given: (%s).'
                            , $definition_value
                            , \Poirot\Std\flatten($params[$definition_value])
                        ));

                    $return .= $params[$definition_value];
                    break;

                case '_optional_':
                    $optionalPart = $this->_buildStringFromParsed($definition_value, $params, true);
                    if ($optionalPart !== '')
                        $return .= $optionalPart;

                    break;
            }
        }

        return $return;
    }
953297
public static function pathSub($path, $part, $separator = DIRECTORY_SEPARATOR)
    {
        if (empty($path)) {
            throw new ArgumentNullException("path");
        }

        if (empty($part)) {
            throw new ArgumentNullException("part");
        }

        $pattern = '@[/\\\\]+@';
        $path = preg_replace($pattern, DIRECTORY_SEPARATOR, $path);
        $part = preg_replace($pattern, DIRECTORY_SEPARATOR, $part.DIRECTORY_SEPARATOR);
        $part = preg_quote($part, '@');
        $result = preg_replace('@^'.$part.'@', DIRECTORY_SEPARATOR, $path);
        if ($separator != DIRECTORY_SEPARATOR) {
            $result = str_replace(DIRECTORY_SEPARATOR, $separator, $result);
        }

        return $result;
    }
272187
public static function build($component, array $options = [])
    {
        $ret = [];
        foreach ($component as $frag) {
            $ret[] = $frag::build($frag);
        }

        return implode($ret, ', ');
    }
94610
private function createUrl($path, $options = [])
    {
        if (!is_string($path)) {
            $url = implode('/', array_map(function ($a) {
                return urlencode(is_array($a) ? implode(',', $a) : $a);
            }, $path));
            if (!empty($options)) {
                $url .= '?' . http_build_query($options);
            }
        } else {
            $url = $path;
            if (!empty($options)) {
                $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($options);
            }
        }

        return $url;
    }
70514
public static function BuildPathOptions(array $options = [])
    {
        // Size flag
        $sizeFlag = sprintf( '-%sx%s', $options['width'], $options['height'] );

        // Resize flag
        $resizeFlag = $options[ 'resize' ] ? '-resize' : '';

        // focus point flag
        $focusPointFlag = '';
        if (!$options['resize'] && ( $options[ 'focusX' ] || $options['focusY'] )) {
            $focusPointFlag = sprintf( '-[%sx%s]', $options['focusX'], $options['focusY'] );
        }

        return sprintf( '%s%s%s', $sizeFlag, $focusPointFlag, $resizeFlag );
    }