Passed
Pull Request — master (#2256)
by Arnaud
15:27 queued 09:22
created

Html   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 70
ccs 24
cts 28
cp 0.8571
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOpenGraphMetaTags() 0 15 2
A getTwitterMetaTags() 0 15 2
A getImageFromMetaTags() 0 13 3
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Util;
15
16
use Symfony\Component\DomCrawler\Crawler;
17
18
/**
19
 * HTML utility class.
20
 *
21
 * This class provides utility methods for HTML manipulation.
22
 */
23
class Html
24
{
25
    /**
26
     * Extract Open Graph meta tags from HTML content.
27
     *
28
     * @param string $html The HTML content to parse
29
     *
30
     * @return array An associative array of Open Graph meta tags
31
     */
32 1
    public static function getOpenGraphMetaTags(string $html): array
33
    {
34 1
        $crawler = new Crawler();
35 1
        $crawler->addHtmlContent($html);
36 1
        $metaTags = $crawler->filterXPath('//meta[starts-with(@property, "og:")]');
37
38 1
        $ogTags = [];
39
        /** @var \DOMElement $metaTag */
40 1
        foreach ($metaTags as $metaTag) {
41 1
            $property = $metaTag->getAttribute('property');
42 1
            $content = $metaTag->getAttribute('content');
43 1
            $ogTags[$property] = $content;
44
        }
45
46 1
        return $ogTags;
47
    }
48
49
    /**
50
     * Extract Twitter meta tags from HTML content.
51
     *
52
     * @param string $html The HTML content to parse
53
     *
54
     * @return array An associative array of Twitter meta tags
55
     */
56 1
    public static function getTwitterMetaTags(string $html): array
57
    {
58 1
        $crawler = new Crawler();
59 1
        $crawler->addHtmlContent($html);
60 1
        $metaTags = $crawler->filterXPath('//meta[starts-with(@name, "twitter:")]');
61
62 1
        $twitterTags = [];
63
        /** @var \DOMElement $metaTag */
64 1
        foreach ($metaTags as $metaTag) {
65
            $name = $metaTag->getAttribute('name');
66
            $content = $metaTag->getAttribute('content');
67
            $twitterTags[$name] = $content;
68
        }
69
70 1
        return $twitterTags;
71
    }
72
73
    /**
74
     * Get the image URL from Open Graph or Twitter meta tags.
75
     *
76
     * @param string $html The HTML content to parse
77
     *
78
     * @return string|null The image URL if found, null otherwise
79
     */
80 1
    public static function getImageFromMetaTags(string $html): ?string
81
    {
82 1
        $ogTags = self::getOpenGraphMetaTags($html);
83 1
        if (isset($ogTags['og:image'])) {
84 1
            return $ogTags['og:image'];
85
        }
86
87 1
        $twitterTags = self::getTwitterMetaTags($html);
88 1
        if (isset($twitterTags['twitter:image'])) {
89
            return $twitterTags['twitter:image'];
90
        }
91
92 1
        return null;
93
    }
94
}
95