Completed
Push — master ( f0b6ba...2d8fe6 )
by ignace nyamagana
04:40
created

Normalize::decodeUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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 normalize URI objects
20
 *
21
 * @package League.uri
22
 * @author  Ignace Nyamagana Butera <[email protected]>
23
 * @since   4.0.0
24
 */
25
class Normalize 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 36
    public function __invoke($uri)
37
    {
38 36
        $modifier = new Pipeline([new HostToAscii(), new KsortQuery()]);
39
40 36
        $path = $uri->getPath();
41 36
        if ('' !== $uri->getScheme().$uri->getAuthority() || (isset($path[0]) && '/' === $path[0])) {
42 36
            $modifier = $modifier->pipe(new RemoveDotSegments());
43 24
        }
44
45 36
        return $this->decodeUri($modifier($uri));
46
    }
47
48 36
    protected function decodeUri($uri)
49
    {
50 36
        foreach (['Path', 'Query', 'Fragment'] as $part) {
51 36
            $uri = $this->decodeUriPart($uri, $part);
52 24
        }
53
54 36
        return $uri;
55
    }
56
57 36
    protected function decodeUriPart($uri, $property)
58
    {
59 36
        $value = preg_replace_callback(
60 36
            ',%('.self::$unreservedCharsEncoded.'),i',
61 36
            function (array $matches) {
62 3
                return rawurldecode($matches[0]);
63 36
            },
64 36
            call_user_func([$uri, 'get'.$property])
65 24
        );
66
67 36
        return call_user_func([$uri, 'with'.$property], $value);
68
    }
69
}
70