Passed
Push — master ( 9022c8...7d51a8 )
by Burak
01:37
created

Helpers::GetLinks()   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
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
namespace SEOCheckup;
3
4
class Helpers
5
{
6
    /**
7
     * @var array $data
8
     */
9
    private $data;
10
11
    /**
12
     * @var array $links
13
     */
14
    private $links;
15
16
    /**
17
     * Helpers constructor
18
     * @param array $data
19
     */
20
    public function __construct($data)
21
    {
22
        $this->data = $data;
23
    }
24
25
    /**
26
     * Get links in a page
27
     *
28
     * @param \DOMDocument $dom
29
     * @return $this
30
     */
31
    public function Links($dom)
32
    {
33
        $tags  = $dom->getElementsByTagName('a');
34
        $links = array();
35
36
        if($tags->length)
37
        {
38
            foreach($tags as $item)
39
            {
40
                $link = $item->getAttribute('href');
41
42
                if($link != '' && strpos($link,'#') !== 0 && strpos(strtolower($link),'javascript:') !== 0)
43
                {
44
                    $link = parse_url($link);
45
46
                    if(!isset($link['scheme']))
47
                    {
48
                        $link['scheme'] = $this->data['parsed_url']['scheme'];
49
                    }
50
51
                    if(!isset($link['host']))
52
                    {
53
                        $link['host'] = $this->data['parsed_url']['host'];
54
                    }
55
56
                    if(!isset($link['path']))
57
                    {
58
                        $link['path'] = '';
59
                    } else {
60
                        if(strpos($link['path'],'/')  !== 0)
61
                        {
62
                            $link['path'] = '/'.$link['path'];
63
                        }
64
                    }
65
66
                    if(!isset($link['query']))
67
                    {
68
                        $link['query'] = '';
69
                    } else {
70
                        $link['query'] = '?'.$link['query'];
71
                    }
72
73
                    $links[] = $link['scheme'].'://'.$link['host'].$link['path'].$link['query'];
74
                }
75
            }
76
        }
77
78
        $this->links = array_unique($links);
79
80
        return $this;
81
    }
82
83
    /**
84
     * Return page links
85
     *
86
     * @return array
87
     */
88
    public function GetLinks()
89
    {
90
        return $this->links;
91
    }
92
93
    /**
94
     * Returns page links by host
95
     *
96
     * @param array $links
97
     * @return array
98
     */
99
    public function GetHosts($links = [])
100
    {
101
        if(is_array($links) && count($links) > 0)
102
        {
103
            $this->links = $links;
104
        }
105
106
        $hosts = [];
107
108
        foreach ($this->links as $link)
109
        {
110
            $parse = parse_url($link);
111
112
            if(isset($parse['host']) && !in_array($parse['host'], $hosts))
113
            {
114
                $hosts[] = $parse['host'];
115
            }
116
        }
117
118
        return $hosts;
119
    }
120
121
    /**
122
     * Get link attributes in a page
123
     *
124
     * @param \DOMDocument $dom
125
     * @param string $tag
126
     * @param string $attr
127
     * @return array
128
     */
129
    public function GetAttributes($dom, $tag = 'a', $attr = 'href')
130
    {
131
        $tags  = $dom->getElementsByTagName($tag);
132
        $links = array();
133
134
        if($tags->length)
135
        {
136
            foreach($tags as $item)
137
            {
138
                $links[] = $item->getAttribute($attr);
139
            }
140
        }
141
142
        return array_unique($links);
143
    }
144
145
    /**
146
     * Whitespace cleaner
147
     *
148
     * @param $input
149
     * @return null|string|string[]
150
     */
151
    public function Whitespace($input)
152
    {
153
        return preg_replace('!\s+!', ' ', $input);
154
    }
155
}