UrlUtils::getUrlFileExtension()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebThumbnailer\Utils;
6
7
/**
8
 * Util class for operations on URL strings.
9
 */
10
class UrlUtils
11
{
12
    /**
13
     * Extract the domains from an URL.
14
     *
15
     * @param string $url Given URL.
16
     *
17
     * @return string Extracted domains, lowercase.
18
     */
19
    public static function getDomain(string $url): string
20
    {
21
        if (!parse_url($url, PHP_URL_SCHEME)) {
22
            $url = 'http://' . $url;
23
        }
24
        return strtolower(parse_url($url, PHP_URL_HOST) ?: '');
25
    }
26
27
    /**
28
     * Retrieve the file extension from a URL.
29
     *
30
     * @param string $url given URL.
31
     *
32
     * @return string File extension or false if not found.
33
     */
34
    public static function getUrlFileExtension(string $url): string
35
    {
36
        $path = parse_url($url, PHP_URL_PATH) ?: '';
37
        if (preg_match('/\.(\w+)$/i', $path, $match) > 0) {
38
            return strtolower($match[1]);
39
        }
40
        return '';
41
    }
42
}
43