Passed
Push — master ( 7ae6f1...8efc25 )
by Sebastian
02:30
created

URLInfo_Normalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
    public function __construct(URLInfo $info)
29
    {
30
        $this->info = $info;
31
    }
32
    
33
    public function normalize() : string
34
    {
35
        $method = 'normalize_'.$this->info->getType();
36
        
37
        return (string)$this->$method();
38
    }
39
    
40
    protected function normalize_fragment() : string
41
    {
42
        return '#'.$this->info->getFragment();
43
    }
44
    
45
    protected function normalize_phone() : string
46
    {
47
        return 'tel://'.$this->info->getHost();
48
    }
49
    
50
    protected function normalize_email() : string
51
    {
52
        return 'mailto:'.$this->info->getPath();
53
    }
54
    
55
    protected function normalize_url() : string
56
    {
57
        $normalized = $this->info->getScheme().'://'.$this->info->getHost();
58
        
59
        if($this->info->hasPath()) {
60
            $normalized .= $this->info->getPath();
61
        }
62
        
63
        $params = $this->info->getParams();
64
        if(!empty($params)) {
65
            $normalized .= '?'.http_build_query($params);
66
        }
67
        
68
        if($this->info->hasFragment()) {
69
            $normalized .= '#'.$this->info->getFragment();
70
        }
71
        
72
        return $normalized;
73
    }
74
}
75