Passed
Push — master ( 4a79ed...7ae6f1 )
by Sebastian
02:30
created

URLInfo_Highlighter::highlight_url()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 21
rs 9.8333
1
<?php
2
/**
3
 * File containing the {@see AppUtils\URLInfo_Highlighter} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage URLInfo
7
 * @see AppUtils\URLInfo_Highlighter
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Handles highlighting a previously parsed URL. 
16
 *
17
 * @package Application Utils
18
 * @subpackage URLInfo
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class URLInfo_Highlighter
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 highlight() : string
34
    {
35
        $method = 'highlight_'.$this->info->getType();
36
        
37
        return 
38
        '<span class="link">'.
39
            $this->$method().
40
        '</span>';
41
    }
42
    
43
    protected function highlight_email() : string
44
    {
45
        return sprintf(
46
            '<span class="link-scheme scheme-mailto">mailto:</span>'.
47
            '<span class="link-host">%s</span>',
48
            $this->info->getPath()
49
        );
50
    }
51
    
52
    protected function highlight_fragment() : string
53
    {
54
        return sprintf(
55
            '<span class="link-fragment-sign">#</span>'.
56
            '<span class="link-fragment-value">%s</span>',
57
            $this->info->getFragment()
58
        );
59
    }
60
    
61
    protected function highlight_phone() : string
62
    {
63
        return $this->highlight_url();
64
    }
65
    
66
    protected function highlight_url() : string
67
    {
68
        $result = [];
69
        
70
        $parts = array(
71
            'scheme',
72
            'username',
73
            'host',
74
            'port',
75
            'path',
76
            'params',
77
            'fragment'
78
        );
79
        
80
        foreach($parts as $part) 
81
        {
82
            $method = 'render_'.$part;
83
            $result[] = (string)$this->$method();
84
        }
85
        
86
        return implode('', $result);
87
    }
88
    
89
    protected function render_scheme() : string
90
    {
91
        if(!$this->info->hasScheme()) {
92
            return '';
93
        }
94
        
95
        return sprintf(
96
            '<span class="link-scheme scheme-%1$s">'.
97
                '%1$s:'.
98
            '</span>'.
99
            '<span class="link-component double-slashes">//</span>',
100
            $this->info->getScheme()
101
        );
102
    }
103
    
104
    protected function render_username() : string
105
    {
106
        if(!$this->info->hasUsername()) {
107
            return '';
108
        }
109
        
110
        return sprintf(
111
            '<span class="link-credentials">%s</span>'.
112
            '<span class="link-component credentials-separator">:</span>'.
113
            '<span class="link-credentials">%s</span>'.
114
            '<span class="link-component credentials-at">@</span>',
115
            $this->info->getUsername(),
116
            $this->info->getPassword()
117
        );
118
    }
119
    
120
    protected function render_host() : string
121
    {
122
        if(!$this->info->hasHost()) {
123
            return '';
124
        }
125
        
126
        return sprintf(
127
            '<span class="link-host">%s</span><wbr>',
128
            $this->info->getHost()
129
        );
130
    }
131
    
132
    protected function render_port() : string
133
    {
134
        if(!$this->info->hasPort()) {
135
            return '';
136
        }
137
        
138
        return sprintf(
139
            '<span class="link-component port-separator">:</span>'.
140
            '<span class="link-port">%s</span>',
141
            $this->info->getPort()
142
        );
143
    }
144
       
145
    protected function render_path() : string
146
    {
147
        if(!$this->info->hasPath()) {
148
            return '';
149
        }
150
        
151
        $path = str_replace(array(';', '='), array(';<wbr>', '=<wbr>'), $this->info->getPath());
152
        $tokens = explode('/', $path);
153
        $path = implode('<span class="link-component path-separator">/</span><wbr>', $tokens);
154
        
155
        return sprintf(
156
            '<span class="link-path">%s</span><wbr>',
157
            $path
158
        );
159
    }
160
    
161
    protected function render_params() : string
162
    {
163
        $params = $this->info->getParams();
164
        
165
        if(empty($params)) {
166
            return '';
167
        }
168
        
169
        $tokens = array();
170
        $excluded = array();
171
        
172
        if($this->info->isParamExclusionEnabled())
173
        {
174
            $excluded = $this->info->getExcludedParams();
175
        }
176
        
177
        foreach($params as $param => $value)
178
        {
179
            $parts = sprintf(
180
                '<span class="link-param-name">%s</span>'.
181
                '<span class="link-component param-equals">=</span>'.
182
                '<span class="link-param-value">%s</span>'.
183
                '<wbr>',
184
                $param,
185
                str_replace(
186
                    array(':', '.', '-', '_'),
187
                    array(':<wbr>', '.<wbr>', '-<wbr>', '_<wbr>'),
188
                    $value
189
                )
190
            );
191
            
192
            $tag = '';
193
            
194
            // is parameter exclusion enabled, and is this an excluded parameter?
195
            if(isset($excluded[$param]))            
196
            {
197
                // display the excluded parameter, but highlight it
198
                if($this->info->isHighlightExcludeEnabled())
199
                {
200
                    $tooltip = $excluded[$param];
201
                    
202
                    $tag = sprintf(
203
                        '<span class="link-param excluded-param" title="%s" data-toggle="tooltip">%s</span>',
204
                        $tooltip,
205
                        $parts
206
                    );
207
                }
208
                else
209
                {
210
                    continue;
211
                }
212
            }
213
            else
214
            {
215
                $tag = sprintf(
216
                    '<span class="link-param">%s</span>',
217
                    $parts
218
                );
219
            }
220
            
221
            $tokens[] = $tag;
222
        }
223
        
224
        return
225
        '<span class="link-component query-sign">?</span>'.
226
        implode('<span class="link-component param-separator">&amp;</span>', $tokens);
227
    }
228
     
229
    protected function render_fragment() : string
230
    {
231
        if(!$this->info->hasFragment()) {
232
            return '';
233
        }
234
        
235
        return sprintf(
236
            '<span class="link-fragment-sign">#</span>'.
237
            '<span class="link-fragment">%s</span>',
238
            $this->info->getFragment()
239
        );
240
    }
241
}
242