Completed
Push — master ( 0c0542...f9131d )
by Felix
06:34
created

Url::setParts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.0078
1
<?php
2
3
namespace Felix\Scraper;
4
5
class Url
6
{
7
    /** @var string */
8
    protected $url;
9
10
    /** @var array */
11
    protected $parts;
12
13
    /** @var string */
14
    protected $hash;
15
16 4
    public function __construct($url)
17
    {
18 4
        $this->setParts($url);
19 4
    }
20
21 2
    public function __toString()
22
    {
23 2
        return $this->url;
24
    }
25
26
    /**
27
     * Parsear URL.
28
     * 
29
     * @param $url string Toda la url.
30
     * 
31
     * @return void
32
     */
33 4
    function setParts($url)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
    {
35 4
        $parts = parse_url($url);
36
37 4
        if ($parts === false) {
38
            throw new Exception($url.' es una URL mal formada y no se puede procesar');
39
        }
40
41 4
        $this->url = $url;
42 4
        $this->parts = $parts;
43 4
        $this->hash = md5($url);
44 4
    }
45
46
    /**
47
     * Obtener una parte de la URL.
48
     *
49
     * @param $part string Parte de la URL (Ej: host).
50
     *
51
     * @return string|null
52
     */
53 3
    public function part($part)
54
    {
55 3
        return array_key_exists($part, $this->parts) ? $this->parts[$part] : null;
56
    }
57
58
    /**
59
     * Url tiene una parte.
60
     *
61
     * @return bool true|false
62
     */
63 2
    public function has($part)
64
    {
65 2
        return $this->part($part) !== null;
66
    }
67
68
    /**
69
     * Decodificación URL.
70
     *
71
     * @return object
72
     */
73 1
    public function decode()
74
    {
75 1
        $this->url = urldecode($this->url);
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * Dada una URL, normaliza esa URL.
82
     *
83
     * @param $schemeAndHost string Esquema y dominio base (Ej. http://example.com)
84
     *
85
     * @return string
86
     */
87 1
    public function normalize($schemeAndHost)
88
    {
89 1
        if (!$this->has('host') || !$this->has('scheme')) {
90 1
            $this->url = $schemeAndHost.$this->url;
91
        }
92
93 1
        return $this;
94
    }
95
}
96