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

ImmutableComponentTrait::getReservedRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
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.1.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
use ReflectionClass;
17
18
/**
19
 * Common methods for Component Value Object
20
 *
21
 * @package League.uri
22
 * @author  Ignace Nyamagana Butera <[email protected]>
23
 * @since   4.0.0
24
 */
25
trait ImmutableComponentTrait
26
{
27
    use ValidatorTrait;
28
29
    /**
30
     * Reserved characters list
31
     *
32
     * @var string
33
     */
34
    protected static $reservedCharactersRegex = "\!\$&'\(\)\*\+,;\=\:";
35
36
    /**
37
     * Invalid characters list
38
     *
39
     * @var string
40
     */
41
    protected static $invalidCharactersRegex;
42
43
    /**
44
     * Asserts the string against RFC3986 rules
45
     *
46
     * @param string $str
47
     *
48
     * @throws InvalidArgumentException If the string is invalid
49
     */
50 1311
    protected function assertValidComponent($str)
51
    {
52 1311
        if (isset(static::$invalidCharactersRegex) && preg_match(static::$invalidCharactersRegex, $str)) {
53 39
            throw new InvalidArgumentException('The component contains invalid characters');
54
        }
55 1272
    }
56
57
    /**
58
     * Returns whether two UriPart objects represent the same value
59
     * The comparison is based on the getUriComponent method
60
     *
61
     * @param UriPart $component
62
     *
63
     * @return bool
64
     */
65 36
    public function sameValueAs(UriPart $component)
66
    {
67 36
        return $component->getUriComponent() === $this->getUriComponent();
68
    }
69
70
    /**
71
     * Returns the instance string representation
72
     * with its optional URI delimiters
73
     *
74
     * @return string
75
     */
76
    abstract public function getUriComponent();
77
78
    /**
79
     * Returns the instance string representation; If the
80
     * instance is not defined an empty string is returned
81
     *
82
     * @return string
83
     */
84
    abstract public function __toString();
85
86
    /**
87
     * Encoding string according to RFC3986
88
     *
89
     * @param string $str
90
     *
91
     * @return string
92
     */
93 726
    protected static function encode($str)
94
    {
95
        $encoder = function (array $matches) {
96 654
            return rawurlencode($matches[0]);
97 726
        };
98
99 726
        $formatter = function (array $matches) {
100 24
            return strtoupper($matches['encode']);
101 726
        };
102
103 726
        $str = preg_replace_callback(
104 726
            '/(?:[^'.static::$reservedCharactersRegex.']+|%(?![A-Fa-f0-9]{2}))/S',
105 484
            $encoder,
106
            $str
107 484
        );
108
109 726
        return preg_replace_callback(',(?<encode>%[0-9a-f]{2}),', $formatter, $str);
110
    }
111
112
    /**
113
     * Returns an instance with the specified string
114
     *
115
     * This method MUST retain the state of the current instance, and return
116
     * an instance that contains the modified data
117
     *
118
     * @param string $value
119
     *
120
     * @return static
121
     */
122 543
    public function modify($value)
123
    {
124 543
        if ($value == $this->__toString()) {
125 186
            return $this;
126
        }
127
128 474
        return (new ReflectionClass(get_called_class()))->newInstance($value);
129
    }
130
}
131