Helpers::Whitespace()   A
last analyzed

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 1
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
     * Get link attributes in a page
95
     *
96
     * @param \DOMDocument $dom
97
     * @param string $tag
98
     * @param string $attr
99
     * @return array
100
     */
101
    public function GetAttributes($dom, $tag = 'a', $attr = 'href')
102
    {
103
        $tags  = $dom->getElementsByTagName($tag);
104
        $links = array();
105
106
        if($tags->length)
107
        {
108
            foreach($tags as $item)
109
            {
110
                $links[] = $item->getAttribute($attr);
111
            }
112
        }
113
114
        return array_unique($links);
115
    }
116
117
    /**
118
     * Whitespace cleaner
119
     *
120
     * @param $input
121
     * @return null|string|string[]
122
     */
123
    public function Whitespace($input)
124
    {
125
        return preg_replace('!\s+!', ' ', $input);
126
    }
127
}