Passed
Push — master ( 19f789...84a0c3 )
by Arthur
02:06
created

UrlUtils::getDomain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WebThumbnailer\Utils;
4
5
/**
6
 * Class UrlUtils
7
 *
8
 * Util class for operations on URL strings.
9
 *
10
 * @package WebThumbnailer\Utils
11
 */
12
class UrlUtils
13
{
14
    /**
15
     * Extract the domains from an URL.
16
     *
17
     * @param string $url Given URL.
18
     *
19
     * @return string Extracted domains, lowercase.
20
     */
21
    public static function getDomain($url)
22
    {
23
        if (! parse_url($url, PHP_URL_SCHEME)) {
24
            $url = 'http://' . $url;
25
        }
26
        return strtolower(parse_url($url, PHP_URL_HOST));
27
    }
28
29
    /**
30
     * Generate a relative URL from absolute local path.
31
     * Example:
32
     *    - /home/website/resources/file.txt
33
     *    ====>
34
     *    - resources/file.txt
35
     *
36
     * @param array  $server $_SERVER array.
37
     * @param string $path   Absolute path to transform.
38
     *
39
     * @return string Relative path.
40
     */
41
    public static function generateRelativeUrlFromPath($server, $path)
42
    {
43
        if (isset($server['SCRIPT_FILENAME']) && preg_match('#/?(.+/)\w+\.php$#', $server['SCRIPT_FILENAME'], $matches) > 0) {
44
            $path = ltrim(substr($path, strlen($matches[1])), '/');
45
        }
46
47
        return $path;
48
    }
49
50
    /**
51
     * Retrieve the file extension from a URL.
52
     *
53
     * @param string $url given URL.
54
     *
55
     * @return string|bool File extension or false if not found.
56
     */
57
    public static function getUrlFileExtension($url)
58
    {
59
        $path = parse_url($url, PHP_URL_PATH);
60
        if (preg_match('/\.(\w+)$/i', $path, $match) > 0) {
61
            return strtolower($match[1]);
62
        }
63
        return '';
64
    }
65
}
66