Passed
Push — master ( 12a81e...e685ae )
by Sebastian
02:29
created

URLInfo_Normalizer::normalize_email()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 1
c 3
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
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
   /**
36
    * Enables the authentication information in the URL,
37
    * if a username and password are present.
38
    * 
39
    * @param bool $enable Whether to turn it on or off.
40
    * @return URLInfo_Normalizer
41
    */
42
    public function enableAuth(bool $enable=true) : URLInfo_Normalizer
43
    {
44
        $this->auth = $enable;
45
        return $this;
46
    }
47
    
48
   /**
49
    * Retrieves the normalized URL.
50
    * @return string
51
    */
52
    public function normalize() : string
53
    {
54
        $method = 'normalize_'.$this->info->getType();
55
        
56
        return (string)$this->$method();
57
    }
58
    
59
    protected function normalize_fragment() : string
60
    {
61
        return '#'.$this->info->getFragment();
62
    }
63
    
64
    protected function normalize_phone() : string
65
    {
66
        return 'tel://'.$this->info->getHost();
67
    }
68
    
69
    protected function normalize_email() : string
70
    {
71
        return 'mailto:'.$this->info->getPath();
72
    }
73
    
74
    protected function normalize_url() : string
75
    {
76
        $normalized = $this->info->getScheme().'://';
77
        $normalized = $this->renderAuth($normalized);
78
        $normalized .= $this->info->getHost();
79
        $normalized = $this->renderPort($normalized);        
80
        $normalized = $this->renderPath($normalized);
81
        $normalized = $this->renderParams($normalized);
82
        $normalized = $this->renderFragment($normalized);
83
        
84
        return $normalized;
85
    }
86
    
87
    protected function renderAuth(string $normalized) : string
88
    {
89
        if(!$this->info->hasUsername() || !$this->auth) {
90
            return $normalized;
91
        }
92
         
93
        return $normalized . urlencode($this->info->getUsername()).':'.urlencode($this->info->getPassword()).'@';
94
    }
95
    
96
    protected function renderPort(string $normalized) : string
97
    {
98
        if(!$this->info->hasPort()) {
99
            return $normalized;
100
        }
101
        
102
        return $normalized . ':'.$this->info->getPort();
103
    }
104
    
105
    protected function renderPath(string $normalized) : string
106
    {
107
        if(!$this->info->hasPath()) {
108
            return $normalized; 
109
        }
110
        
111
        return $normalized . $this->info->getPath();
112
    }
113
    
114
    protected function renderParams(string $normalized) : string
115
    {
116
        $params = $this->info->getParams();
117
        
118
        if(empty($params)) {
119
            return $normalized;
120
        }
121
        
122
        return $normalized . '?'.http_build_query($params);
123
    }
124
    
125
    protected function renderFragment(string $normalized) : string
126
    {
127
        if(!$this->info->hasFragment()) {
128
            return $normalized;
129
        }
130
        
131
        return $normalized . '#'.$this->info->getFragment();
132
    }
133
}
134