JBZoo\Utils\Url::buildAll PHP Method

buildAll() public static method

Build a URL. The parts of the second URL will be merged into the first according to the flags argument.
See also: https://github.com/jakeasmith/http_build_url/
Author: Jake Smith ([email protected])
public static buildAll ( mixed $url, mixed $parts = [], integer $flags = self::URL_REPLACE, array &$newUrl = [] ) : string
$url mixed (part(s) of) an URL in form of a string or associative array like parse_url() returns
$parts mixed same as the first argument
$flags integer a bitmask of binary or'ed HTTP_URL constants; HTTP_URL_REPLACE is the default
$newUrl array if set, it will be filled with the parts of the composed url like parse_url() would return
return string
    public static function buildAll($url, $parts = array(), $flags = self::URL_REPLACE, &$newUrl = array())
    {
        is_array($url) || ($url = parse_url($url));
        is_array($parts) || ($parts = parse_url($parts));
        Arr::key('query', $url) && is_string($url['query']) || ($url['query'] = null);
        Arr::key('query', $parts) && is_string($parts['query']) || ($parts['query'] = null);
        $keys = array('user', 'pass', 'port', 'path', 'query', 'fragment');
        // HTTP_URL_STRIP_ALL and HTTP_URL_STRIP_AUTH cover several other flags.
        if ($flags & self::URL_STRIP_ALL) {
            $flags |= self::URL_STRIP_USER | self::URL_STRIP_PASS | self::URL_STRIP_PORT | self::URL_STRIP_PATH | self::URL_STRIP_QUERY | self::URL_STRIP_FRAGMENT;
        } elseif ($flags & self::URL_STRIP_AUTH) {
            $flags |= self::URL_STRIP_USER | self::URL_STRIP_PASS;
        }
        // Schema and host are alwasy replaced
        foreach (array('scheme', 'host') as $part) {
            if (Arr::key($part, $parts)) {
                $url[$part] = $parts[$part];
            }
        }
        if ($flags & self::URL_REPLACE) {
            foreach ($keys as $key) {
                if (Arr::key($key, $parts) && $parts[$key]) {
                    $url[$key] = $parts[$key];
                }
            }
        } else {
            if (Arr::key('path', $parts) && $flags & self::URL_JOIN_PATH) {
                if (Arr::key('path', $url) && substr($parts['path'], 0, 1) !== '/') {
                    $url['path'] = rtrim(str_replace(basename($url['path']), '', $url['path']), '/') . '/' . ltrim($parts['path'], '/');
                } else {
                    $url['path'] = $parts['path'];
                }
            }
            if (Arr::key('query', $parts) && $flags & self::URL_JOIN_QUERY) {
                if (Arr::key('query', $url)) {
                    parse_str($url['query'], $urlQuery);
                    parse_str($parts['query'], $partsQuery);
                    $queryParams = array_replace_recursive($urlQuery, $partsQuery);
                    $url['query'] = self::build($queryParams);
                }
                // see deadcode else condition from utilphp lib
            }
        }
        if (Arr::key('path', $url) && substr($url['path'], 0, 1) !== '/') {
            $url['path'] = '/' . $url['path'];
        }
        foreach ($keys as $key) {
            $strip = 'URL_STRIP_' . strtoupper($key);
            if ($flags & constant(__CLASS__ . '::' . $strip)) {
                unset($url[$key]);
            }
        }
        if (Arr::key('port', $url, true) === self::PORT_HTTPS) {
            $url['scheme'] = 'https';
        } elseif (Arr::key('port', $url, true) === self::PORT_HTTP) {
            $url['scheme'] = 'http';
        }
        $parsedString = '';
        if (Arr::key('scheme', $url)) {
            $parsedString .= $url['scheme'] . '://';
        }
        if (Arr::key('user', $url)) {
            $parsedString .= $url['user'];
            if (Arr::key('pass', $url)) {
                $parsedString .= ':' . $url['pass'];
            }
            $parsedString .= '@';
        }
        if (Arr::key('host', $url)) {
            $parsedString .= $url['host'];
        }
        if (Arr::key('port', $url) && $url['port'] && $url['port'] !== self::PORT_HTTP && $url['port'] !== self::PORT_HTTPS) {
            $parsedString .= ':' . $url['port'];
        }
        if (!empty($url['path'])) {
            $parsedString .= $url['path'];
        } else {
            $parsedString .= '/';
        }
        if (Arr::key('query', $url) && $url['query']) {
            $parsedString .= '?' . $url['query'];
        }
        if (Arr::key('fragment', $url) && $url['fragment']) {
            $parsedString .= '#' . trim($url['fragment'], '#');
        }
        $newUrl = $url;
        return $parsedString;
    }

Usage Example

Example #1
0
 public function testHttpBuildUrl()
 {
     $url = 'http://*****:*****@example.com:8080/path/?query#fragment';
     $expected = 'http://example.com/';
     $actual = Url::buildAll($url, array(), Url::URL_STRIP_ALL);
     is($expected, $actual);
     $expected = 'http://example.com:8080/path/?query#fragment';
     $actual = Url::buildAll($url, array(), Url::URL_STRIP_AUTH);
     is($expected, $actual);
     is('https://dev.example.com/', Url::buildAll('http://example.com/', array('scheme' => 'https', 'host' => 'dev.example.com')));
     is('http://example.com/#hi', Url::buildAll('http://example.com/', array('fragment' => 'hi'), Url::URL_REPLACE));
     is('http://example.com/page', Url::buildAll('http://example.com/', array('path' => 'page'), Url::URL_JOIN_PATH));
     is('http://example.com/page', Url::buildAll('http://example.com', array('path' => 'page'), Url::URL_JOIN_PATH));
     is('http://example.com/?hi=Bro', Url::buildAll('http://example.com/', array('query' => 'hi=Bro'), Url::URL_JOIN_QUERY));
     is('http://example.com/?show=1&hi=Bro', Url::buildAll('http://example.com/?show=1', array('query' => 'hi=Bro'), Url::URL_JOIN_QUERY));
     is('http://[email protected]/', Url::buildAll('http://example.com/', array('user' => 'admin')));
     is('http://*****:*****@example.com/', Url::buildAll('http://example.com/', array('user' => 'admin', 'pass' => '1')));
 }