Passed
Push — master ( 32692a...12a81e )
by Sebastian
02:54
created

URLInfo_Normalizer::normalize_url()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 28
rs 8.8333
cc 7
nc 32
nop 0
1
<?php
2
/**
3
 * File containing the {@see AppUtils\URLInfo_Normalizer} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage URLInfo
7
 * @see AppUtils\URLInfo_Normalizer
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Handles normalizing an URL.
16
 *
17
 * @package Application Utils
18
 * @subpackage URLInfo
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class URLInfo_Normalizer
22
{
23
    /**
24
     * @var URLInfo
25
     */
26
    protected $info;
27
    
28
    protected $auth = true;
29
    
30
    public function __construct(URLInfo $info)
31
    {
32
        $this->info = $info;
33
    }
34
    
35
    public function disableAuth() : URLInfo_Normalizer
36
    {
37
        $this->auth = false;
38
        return $this;
39
    }
40
    
41
    public function normalize() : string
42
    {
43
        $method = 'normalize_'.$this->info->getType();
44
        
45
        return (string)$this->$method();
46
    }
47
    
48
    protected function normalize_fragment() : string
49
    {
50
        return '#'.$this->info->getFragment();
51
    }
52
    
53
    protected function normalize_phone() : string
54
    {
55
        return 'tel://'.$this->info->getHost();
56
    }
57
    
58
    protected function normalize_email() : string
59
    {
60
        return 'mailto:'.$this->info->getPath();
61
    }
62
    
63
    protected function normalize_url() : string
64
    {
65
        $normalized = $this->info->getScheme().'://';
66
        
67
        if($this->info->hasUsername() && $this->auth) {
68
            $normalized .= urlencode($this->info->getUsername()).':'.urlencode($this->info->getPassword()).'@';
69
        }
70
        
71
        $normalized .= $this->info->getHost();
72
        
73
        if($this->info->hasPort()) {
74
            $normalized .= ':'.$this->info->getPort();
75
        }
76
        
77
        if($this->info->hasPath()) {
78
            $normalized .= $this->info->getPath();
79
        }
80
        
81
        $params = $this->info->getParams();
82
        if(!empty($params)) {
83
            $normalized .= '?'.http_build_query($params);
84
        }
85
        
86
        if($this->info->hasFragment()) {
87
            $normalized .= '#'.$this->info->getFragment();
88
        }
89
        
90
        return $normalized;
91
    }
92
}
93