Completed
Pull Request — master (#58)
by ignace nyamagana
03:51
created

ImmutableComponentTrait::modify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Types;
13
14
use InvalidArgumentException;
15
use League\Uri\Interfaces\UriPart;
16
17
/**
18
 * Common methods for Component Value Object
19
 *
20
 * @package League.uri
21
 * @author  Ignace Nyamagana Butera <[email protected]>
22
 * @since   4.0.0
23
 */
24
trait ImmutableComponentTrait
25
{
26
    use ValidatorTrait;
27
28
    /**
29
     * Reserved characters list
30
     *
31
     * @var string
32
     */
33
    protected static $reservedCharactersRegex = "\!\$&'\(\)\*\+,;\=\:";
34
35
    /**
36
     * Invalid characters list
37
     *
38
     * @var string
39
     */
40
    protected static $invalidCharactersRegex;
41
42
    /**
43
     * Asserts the string against RFC3986 rules
44
     *
45
     * @param string $str
46
     *
47
     * @throws InvalidArgumentException If the string is invalid
48
     */
49 1345
    protected function assertValidComponent($str)
50
    {
51 1345
        if (isset(static::$invalidCharactersRegex) && preg_match(static::$invalidCharactersRegex, $str)) {
52 39
            throw new InvalidArgumentException('The component contains invalid characters');
53
        }
54 1306
    }
55
56
    /**
57
     * Returns whether two UriPart objects represent the same value
58
     * The comparison is based on the getUriComponent method
59
     *
60
     * @param UriPart $component
61
     *
62
     * @return bool
63
     */
64 36
    public function sameValueAs(UriPart $component)
65
    {
66 36
        return $component->getUriComponent() === $this->getUriComponent();
67
    }
68
69
    /**
70
     * Returns the instance string representation
71
     * with its optional URI delimiters
72
     *
73
     * @return string
74
     */
75
    abstract public function getUriComponent();
76
77
    /**
78
     * Returns the instance string representation; If the
79
     * instance is not defined an empty string is returned
80
     *
81
     * @return string
82
     */
83
    abstract public function __toString();
84
85
    /**
86
     * Encoding string according to RFC3986
87
     *
88
     * @param string $str
89
     *
90
     * @return string
91
     */
92 699
    protected static function encode($str)
93
    {
94
        $encoder = function (array $matches) {
95 681
            return rawurlencode($matches[0]);
96 699
        };
97
98 699
        $formatter = function (array $matches) {
99 24
            return strtoupper($matches['encode']);
100 699
        };
101
102 699
        $str = preg_replace_callback(
103 699
            '/(?:[^'.static::$reservedCharactersRegex.']+|%(?![A-Fa-f0-9]{2}))/S',
104 464
            $encoder,
105
            $str
106 464
        );
107
108 699
        return preg_replace_callback(',(?<encode>%[0-9a-f]{2}),', $formatter, $str);
109
    }
110
}
111