Completed
Push — master ( 2d1013...8de2d7 )
by Andreas
18:56
created

org_openpsa_httplib_helpers::get_meta_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
1
<?php
2
/**
3
 * @package org.openpsa.httplib
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Symfony\Component\DomCrawler\Crawler;
10
11
/**
12
 * Helpers for HTTP content fetching and handling
13
 *
14
 * @package org.openpsa.httplib
15
 */
16
class org_openpsa_httplib_helpers extends midcom_baseclasses_components_purecode
17
{
18
    /**
19
     * Get value of a meta tag in HTML page.
20
     *
21
     * @param string $html HTML to parse
22
     * @param string $name Name of the meta tag to fetch
23
     * @return ?string
24
     */
25 1
    public static function get_meta_value($html, $name)
26
    {
27 1
        $crawler = new Crawler($html);
28 1
        if ($crawler->filter('head meta[name="' . $name . '"]')->count() == 0) {
29
            return null;
30
        }
31 1
        return (string) $crawler->filter('head meta[name="' . $name . '"]')->attr('content');
32
    }
33
34
    /**
35
     * Get value(s) of a link tag(s) in HTML page.
36
     *
37
     * @param string $html HTML to parse
38
     * @param string $relation Relation (rel) or reverse relation (rev) of the link tag to fetch
39
     * @return array Links matching given criteria as arrays containing keys title, href and optionally hreflang
40
     */
41 1
    public static function get_link_values($html, $relation) : array
42
    {
43 1
        $crawler = new Crawler($html);
44 1
        $nodes = $crawler->filter('head link[rel="' . $relation . '"]');
45
46
        return $nodes->each(function(Crawler $node, $i) {
47 1
            $tag = ['title' => false, 'href' => false, 'hreflang' => false];
48 1
            foreach ($tag as $property => &$value) {
49 1
                if ($node->attr($property) !== null) {
50 1
                    $value = $node->attr($property);
51
                }
52
            }
53 1
            return $tag;
54 1
        });
55
    }
56
57
    /**
58
     * Get value(s) of an anchor tag(s) in HTML page.
59
     *
60
     * @param string $html HTML to parse
61
     * @param string $relation Relation (rel) of the anchor to fetch
62
     * @return array Links matching given criteria as arrays containing keys title, href and value
63
     */
64 2
    public static function get_anchor_values($html, $relation) : array
65
    {
66 2
        $crawler = new Crawler($html);
67 2
        $nodes = $crawler->filter('a[rel="' . $relation . '"]');
68
69
        return $nodes->each(function(Crawler $node, $i) {
70
            return [
71 1
                'title' => ($node->attr('title') !== null) ? $node->attr('title') : false,
72 1
                'href' => ($node->attr('href') !== null) ? $node->attr('href') : false,
73 1
                'value' => ($node->text() !== null) ? $node->text() : false,
0 ignored issues
show
introduced by
The condition $node->text() !== null is always true.
Loading history...
74
            ];
75 2
        });
76
    }
77
}
78