Completed
Push — master ( ee4164...27a827 )
by ignace nyamagana
03:48
created

TranscoderTrait::decodePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
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.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
    protected static $pathRegexp = "/(?:[^\!\$&'\(\)\*\+,;\=\:\/@%]+|%(?![A-Fa-f0-9]{2}))/S";
24
25
    protected static $queryFragmentRegexp = "/(?:[^\!\$&'\(\)\*\+,;\=\:\/@%\?]+|%(?![A-Fa-f0-9]{2}))/S";
26
27
    protected static $encodedRegexp = ',%(?<encode>[0-9a-fA-F]{2}),';
28
29
    protected static $unreservedRegexp = '/[\w\.~]+/';
30
31
    /**
32
     * Reserved characters list
33
     *
34
     * @var string
35
     */
36
    protected static $reservedCharactersRegex = "\!\$&'\(\)\*\+,;\=\:";
37
38
    /**
39
     * Encode a string according to RFC3986 Rules
40
     *
41
     * @param string $subject
42
     *
43
     * @return string
44
     */
45 699
    protected static function encodeQueryFragment($subject)
46
    {
47 699
        return self::encodeComponent($subject, self::$queryFragmentRegexp);
48
    }
49
50
    /**
51
     * Encode a component string
52
     *
53
     * @param string $subject The string to encode
54
     * @param string $regexp  The component specific regular expression
55
     *
56
     * @return string
57
     */
58 1505
    protected static function encodeComponent($subject, $regexp)
59
    {
60
        $encoder = function (array $matches) {
61 1403
            return rawurlencode($matches[0]);
62 1505
        };
63
64
        $formatter = function (array $matches) {
65 273
            return strtoupper($matches[0]);
66 1505
        };
67
68 1505
        $subject = preg_replace_callback($regexp, $encoder, $subject);
69
70 1505
        return preg_replace_callback(self::$encodedRegexp, $formatter, $subject);
71
    }
72
73
    /**
74
     * Encoding string according to RFC3986
75
     *
76
     * @param string $subject
77
     *
78
     * @return string
79
     */
80 673
    protected static function encode($subject)
81
    {
82 673
        return self::encodeComponent(
83 447
            $subject,
84 673
            '/(?:[^'.static::$reservedCharactersRegex.']+|%(?![A-Fa-f0-9]{2}))/S'
85 447
        );
86
    }
87
88
    /**
89
     * Decode a string according to RFC3986 Rules
90
     *
91
     * @param string $subject
92
     *
93
     * @return string
94
     */
95 696
    protected static function decodeQueryFragment($subject)
96
    {
97
        $decoder = function (array $matches) {
98
99 108
            $decode = chr(hexdec($matches['encode']));
100 108
            if (preg_match(self::$unreservedRegexp, $decode)) {
101 6
                return $matches[0];
102
            }
103
104 102
            if (preg_match('/[\[\]\+\?:]+/', $decode)) {
105 15
                return $decode;
106
            }
107
108 96
            return rawurldecode($matches[0]);
109 696
        };
110
111 696
        return preg_replace_callback(self::$encodedRegexp, $decoder, self::encodeQueryFragment($subject));
112
    }
113
114
    /**
115
     * Encode a path string according to RFC3986
116
     *
117
     * @param string $subject can be a string or an array
118
     *
119
     * @return string The same type as the input parameter
120
     */
121 1133
    protected static function encodePath($subject)
122
    {
123 1133
        return self::encodeComponent($subject, self::$pathRegexp);
124
    }
125
126
    /**
127
     * Decode a path string according to RFC3986
128
     *
129
     * @param string $subject can be a string or an array
130
     *
131
     * @return string The same type as the input parameter
132
     */
133
    protected static function decodePath($subject)
134
    {
135 1113
        $decoder = function (array $matches) {
136 966
            return rawurldecode($matches[0]);
137 1113
        };
138
139 1113
        return preg_replace_callback(self::$pathRegexp, $decoder, $subject);
140
    }
141
}
142