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

Normalize   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 45
ccs 21
cts 21
cp 1
rs 10
wmc 7
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 4
A decodeUri() 0 8 2
A decodeUriPart() 0 12 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\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