Completed
Push — master ( 11a693...420c23 )
by ignace nyamagana
03:57
created

DecodeUnreservedCharacters::decodeUriPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
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\Modifiers;
13
14
use League\Uri\Interfaces\Uri;
15
use League\Uri\Types\TranscoderTrait;
16
use Psr\Http\Message\UriInterface;
17
18
/**
19
 * A class to Decode URI parts unreserved characters
20
 *
21
 * @package League.uri
22
 * @author  Ignace Nyamagana Butera <[email protected]>
23
 * @since   4.2.0
24
 */
25
class DecodeUnreservedCharacters extends AbstractUriModifier
26
{
27
    use TranscoderTrait;
28
29
    /**
30
     * Return a Uri object modified according to the modifier
31
     *
32
     * @param Uri|UriInterface $uri
33
     *
34
     * @return Uri|UriInterface
35
     */
36 42
    public function __invoke($uri)
37
    {
38 42
        $this->assertUriObject($uri);
39
40 42
        foreach (['Path', 'Query', 'Fragment'] as $part) {
41 42
            $uri = $this->decodeUriPart($uri, $part);
42 28
        }
43
44 42
        return $uri;
45
    }
46
47
    /**
48
     * Decode an URI part
49
     *
50
     * @param Uri|UriInterface $uri
51
     * @param string           $property
52
     *
53
     * @return Uri|UriInterface
54
     */
55
    protected function decodeUriPart($uri, $property)
56
    {
57 42
        $decoder = function (array $matches) {
58 9
            return rawurldecode($matches[0]);
59 42
        };
60
61 42
        $value = preg_replace_callback(
62 42
            ',%('.self::$unreservedCharsEncoded.'),i',
63 28
            $decoder,
64 42
            call_user_func([$uri, 'get'.$property])
65 28
        );
66
67 42
        return call_user_func([$uri, 'with'.$property], $value);
68
    }
69
}
70