Completed
Pull Request — master (#39)
by ignace nyamagana
03:06
created

Extension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2013-2015 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.1.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Modifiers;
13
14
use InvalidArgumentException;
15
use League\Uri\Components\HierarchicalPath;
16
17
/**
18
 * Path component extension modifier
19
 *
20
 * @package League.uri
21
 * @author  Ignace Nyamagana Butera <[email protected]>
22
 * @since   4.0.0
23
 */
24
class Extension extends AbstractPathModifier
25
{
26
    /**
27
     * The extension to use for URI modification
28
     *
29
     * @var string
30
     */
31
    protected $extension;
32
33
    /**
34
     * New instance
35
     *
36
     * @param string $extension
37
     */
38 9
    public function __construct($extension)
39
    {
40 9
        $this->extension = $this->filterExtension($extension);
41 6
    }
42
43
    /**
44
     * filter and validate the extension to use
45
     *
46
     * @param string $extension
47
     *
48
     * @throws InvalidArgumentException if the extension is not valid
49
     *
50
     * @return string
51
     */
52 9
    protected function filterExtension($extension)
53
    {
54 9
        if (0 === strpos($extension, '.') || false !== strpos($extension, '/')) {
55 3
            throw new InvalidArgumentException(
56 1
                'extension must be string sequence without a leading `.` and the path separator `/` characters'
57 2
            );
58
        }
59
60 6
        $extension = filter_var($extension, FILTER_SANITIZE_STRING, ['options' => FILTER_FLAG_STRIP_LOW]);
61
62 6
        return trim($extension);
63
    }
64
65
    /**
66
     * Return a new instance with a different extension to use
67
     *
68
     * DEPRECATION WARNING! This method will be removed in the next major point release
69
     *
70
     * @deprecated deprecated since version 4.1
71
     *
72
     * @param string $extension
73
     *
74
     * @return self
75
     */
76
    public function withExtension($extension)
77
    {
78
        $clone = clone $this;
79
        $clone->extension = $this->filterExtension($extension);
80
81
        return $clone;
82
    }
83
84
    /**
85
     * Modify a URI part
86
     *
87
     * @param string $str the URI part string representation
88
     *
89
     * @return string the modified URI part string representation
90
     */
91 6
    protected function modify($str)
92
    {
93 6
        return (string) (new HierarchicalPath($str))->withExtension($this->extension);
94
    }
95
}
96