Completed
Push — master ( f7a79f...951203 )
by Felix
02:08
created

Url::hasHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Felix\Scraper;
4
5
class Url
6
{
7
    protected $url;
8
    protected $parts;
9
10
    public function __construct($url)
11
    {
12
        $this->url = $url;
13
        $this->parts = parse_url($url);
14
    }
15
16
    /**
17
     * Obtener una parte de la URL.
18
     * 
19
     * @param $part string Parte de la URL (Ej: host).
20
     * 
21
     * @return string|null
22
     */
23 5
    public function getPart($part)
24
    {
25 5
        return array_key_exists($part, $this->parts) ? $this->parts[$part] : null;
26
    }
27
28
    /**
29
     * Url tiene un esquema.
30
     * 
31
     * @return boolean true|false
32
     */
33 2
    public function hasScheme()
34
    {
35 2
        return $this->getPart('scheme') !== null;
36
    }
37
38
    /**
39
     * Url tiene un dominio.
40
     * 
41
     * @return boolean true|false
42
     */
43 1
    public function hasHost()
44
    {
45 1
        return $this->getPart('host') !== null;
46
    }
47
48
    /**
49
     * Dada una URL, normaliza esa URL.
50
     * 
51
     * @param $url string Url base (Ej. http://example.com)
52
     * 
53
     * @return string  
54
     */
55 1
    public function normalize($schemeAndHost)
56
    {
57 1
        $url = urldecode($this->url);
58
59 1
        if (! $this->hasScheme()) {
60
            $url = $schemeAndHost.$url;
61
        }
62
63 1
        return $url;
64
    }
65
}