Test Failed
Push — master ( 684014...621eb3 )
by Dev
01:54
created

ExtractResults::getUrlFromGoogleSerpFromat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace rOpenDev\Google;
4
5
class ExtractResults
6
{
7
    /**
8
     * @var array from \rOpenDev\Google\ResultTypes.php
9
     */
10
    protected $types;
11
    /**
12
     * @var object dom
13
     */
14
    protected $html;
15
16
    public function __construct(string $source)
17
    {
18
        $this->html = new \simple_html_dom();
19
        $this->html->load($source);
20
    }
21
22
    public function getOrganicResults()
23
    {
24
        $results = $this->html->find(str_replace(';', ', ', $this->getSelector('organic')));
25
        $result = [];
26
        foreach ($results as $r) {
27
            $title = $r->find('h3, [role=heading]', 0);
28
            if ($title) {
29
                $result[] = [
30
                    'type' => 'organic',
31
                    'title' => $this->normalizeTextFromGoogle($title->innertext),
32
                    'link' => $this->getUrlFromGoogleSerpFromat($r->find('a', 0)->href),
33
                ];
34
            }
35
        }
36
37
        return $result;
38
    }
39
40
    protected function getSelector(string $type)
41
    {
42
        if (null === $this->types) {
43
            $this->types = ResultsTypes::get();
44
        }
45
46
        return $this->types[$type];
47
    }
48
49
    protected static function getUrlFromGoogleSerpFromat($str)
50
    {
51
        preg_match('/\/url\?.*(q|url)=(http.+)&/SiU', $str, $m1);
52
        $str = isset($m1[2]) ? $m1[2] : $str;
53
54
        return $str;
55
    }
56
57
    protected function normalizeTextFromGoogle($text)
58
    {
59
        return htmlspecialchars_decode(html_entity_decode(strip_tags($text)));
60
    }
61
}
62