Completed
Push — master ( 4b40ba...4de594 )
by ignace nyamagana
03:31
created

TranscoderTrait::encodeComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
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.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Types;
13
14
/**
15
 * Uri Parameter validation
16
 *
17
 * @package League.uri
18
 * @author  Ignace Nyamagana Butera <[email protected]>
19
 * @since   4.2.0
20
 */
21
trait TranscoderTrait
22
{
23
    /**
24
     * Encoded Characters regular expression pattern
25
     *
26
     * @see http://tools.ietf.org/html/rfc3986#section-2.1
27
     *
28
     * @var string
29
     */
30
    protected static $encodedChars = '[A-Fa-f0-9]{2}';
31
32
    /**
33
     * RFC3986 Sub delimiter characters regular expression pattern
34
     *
35
     * @see http://tools.ietf.org/html/rfc3986#section-2.2
36
     *
37
     * @var string
38
     */
39
    protected static $subdelimChars = "\!\$&'\(\)\*\+,;\=%";
40
41
    /**
42
     * RFC3986 unreserved characters regular expression pattern
43
     *
44
     * @see http://tools.ietf.org/html/rfc3986#section-2.3
45
     *
46
     * @var string
47
     */
48
    protected static $unreservedChars = 'A-Za-z0-9_\-\.~';
49
50
    /**
51
     * RFC3986 unreserved characters encoded regular expression pattern
52
     *
53
     * @see http://tools.ietf.org/html/rfc3986#section-2.3
54
     *
55
     * @var string
56
     */
57
    protected static $unreservedCharsEncoded = '2[D|E]|3[0-9]|4[1-9|A-F]|5[0-9|A|F]|6[1-9|A-F]|7[0-9|E]';
58
59
    /**
60
     * Encode a component string
61
     *
62
     * @param string $str     The string to encode
63
     * @param string $pattern a regular expression pattern
64
     *
65
     * @return string
66
     */
67 1527
    protected static function encode($str, $pattern)
68
    {
69 1527
        $regexp = '/(?:[^'.self::$unreservedChars.$pattern.']+|%(?!'.self::$encodedChars.'))/';
70
        $encoder = function (array $matches) {
71 228
            return rawurlencode($matches[0]);
72 1527
        };
73
74 1527
        $str = preg_replace_callback($regexp, $encoder, $str);
75
        $formatter = function (array $matches) {
76 294
            return strtoupper($matches[0]);
77 1527
        };
78
79 1527
        return preg_replace_callback(',%'.self::$encodedChars.',', $formatter, $str);
80
    }
81
82
    /**
83
     * Encode a user string according to RFC3986 Rules
84
     *
85
     * @param string $str
86
     *
87
     * @return string
88
     */
89 798
    protected static function encodeUser($str)
90
    {
91 798
        return self::encode($str, self::$subdelimChars);
92
    }
93
94
    /**
95
     * Encode a user string according to RFC3986 Rules
96
     *
97
     * @param string $str
98
     *
99
     * @return string
100
     */
101 405
    protected static function encodePass($str)
102
    {
103 405
        return self::encode($str, self::$subdelimChars.'\:');
104
    }
105
106
    /**
107
     * Encode a path string according to RFC3986
108
     *
109
     * @param string $str can be a string or an array
110
     *
111
     * @return string The same type as the input parameter
112
     */
113 1244
    protected static function encodePath($str)
114
    {
115 1244
        return self::encode($str, self::$subdelimChars.'\:\/@');
116
    }
117
118
    /**
119
     * Encode a string according to RFC3986 Rules
120
     *
121
     * @param string $str
122
     *
123
     * @return string
124
     */
125 702
    protected static function encodeQueryFragment($str)
126
    {
127 702
        return self::encode($str, self::$subdelimChars.'\:\/@\?');
128
    }
129
130
    /**
131
     * Decode a component string
132
     *
133
     * @param string $str     The string to decode
134
     * @param string $pattern a regular expression pattern
135
     *
136
     * @return string
137
     */
138 1594
    protected static function decode($str, $pattern)
139
    {
140 1594
        $regexp = ',%'.$pattern.',i';
141 1594
        $decoder = function (array $matches) use ($regexp) {
142
143 102
            if (preg_match($regexp, $matches[0])) {
144 27
                return strtoupper($matches[0]);
145
            }
146
147 78
            return rawurldecode($matches[0]);
148 1594
        };
149
150 1594
        return preg_replace_callback(',%'.self::$encodedChars.',', $decoder, $str);
151
    }
152
153
    /**
154
     * Decode a component according to RFC3986
155
     *
156
     * @param string $str
157
     *
158
     * @return string
159
     */
160 963
    protected static function decodeComponent($str)
161
    {
162 963
        return self::decode($str, self::$unreservedCharsEncoded);
163
    }
164
165
    /**
166
     * Decode a path component according to RFC3986
167
     *
168
     * @param string $str
169
     *
170
     * @return string
171
     */
172 1224
    protected static function decodePath($str)
173
    {
174 1224
        return self::decode($str, self::$unreservedCharsEncoded.'|2F');
175
    }
176
}
177