Test Failed
Push — master ( 147d4d...670e11 )
by Alain
03:28
created

URIHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 58
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasExtension() 0 11 2
A containsExtension() 0 12 2
A getFilename() 0 4 1
1
<?php declare(strict_types=1);
2
/**
3
 * Bright Nucleus View Component.
4
 *
5
 * @package   BrightNucleus\View
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016-2017 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\View\Support;
13
14
/**
15
 * Class URIHelper.
16
 *
17
 * @since   0.1.1
18
 *
19
 * @package BrightNucleus\View\Support
20
 * @author  Alain Schlesser <[email protected]>
21
 */
22
class URIHelper
23
{
24
25
    /**
26
     * Check whether a given URI has a specific extension.
27
     *
28
     * @since 0.1.3
29
     *
30
     * @param string $uri       URI to check the extension of.
31
     * @param string $extension Extension to check for.
32
     *
33
     * @return bool
34
     */
35 44
    public static function hasExtension(string $uri, string $extension): bool
36
    {
37 44
        $extension       = '.' . ltrim($extension, '.');
38 44
        $uriLength       = mb_strlen($uri);
39 44
        $extensionLength = mb_strlen($extension);
40 1
        if ($extensionLength > $uriLength) {
41
            return false;
42
        }
43 43
44
        return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
45
    }
46
47
    /**
48
     * Check whether a given URI contains an extension and return it.
49
     *
50
     * @param string $uri URI to check.
51
     * @return string Extension that was found, empty string if none found.
52
     */
53
    public static function containsExtension(string $uri): string
54
    {
55 44
        $pathParts     = explode('/', $uri);
56
        $filename      = array_pop($pathParts);
57 44
        $filenameParts = explode('.', $filename);
58
59
        if (count($filenameParts) > 1) {
60
            return array_pop($filenameParts);
61
        }
62
63
        return '';
64
    }
65
66
    /**
67
     * Get the filename for an URI.
68
     *
69
     * @since 0.1.3
70
     *
71
     * @param string $uri URI to get the filename from.
72
     *
73
     * @return string Filename without path.
74
     */
75
    public static function getFilename(string $uri): String
76
    {
77
        return basename($uri);
78
    }
79
}
80