• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 19859909440

02 Dec 2025 01:16PM UTC coverage: 82.119%. First build
19859909440

Pull #2256

github

web-flow
Merge 10bbce255 into c57e63463
Pull Request #2256: feat: image_from_url Twig function

28 of 33 new or added lines in 2 files covered. (84.85%)

3256 of 3965 relevant lines covered (82.12%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.0
/src/Util/Html.php
1
<?php
2

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
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
    public static function getOpenGraphMetaTags(string $html): array
33
    {
34
        $crawler = new Crawler();
1✔
35
        $crawler->addHtmlContent($html);
1✔
36
        $metaTags = $crawler->filterXPath('//meta[starts-with(@property, "og:")]');
1✔
37

38
        $ogTags = [];
1✔
39
        /** @var \DOMElement $metaTag */
40
        foreach ($metaTags as $metaTag) {
1✔
41
            $property = $metaTag->getAttribute('property');
1✔
42
            $content = $metaTag->getAttribute('content');
1✔
43
            $ogTags[$property] = $content;
1✔
44
        }
45

46
        return $ogTags;
1✔
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
    public static function getTwitterMetaTags(string $html): array
57
    {
58
        $crawler = new Crawler();
1✔
59
        $crawler->addHtmlContent($html);
1✔
60
        $metaTags = $crawler->filterXPath('//meta[starts-with(@name, "twitter:")]');
1✔
61

62
        $twitterTags = [];
1✔
63
        /** @var \DOMElement $metaTag */
64
        foreach ($metaTags as $metaTag) {
1✔
NEW
65
            $name = $metaTag->getAttribute('name');
×
NEW
66
            $content = $metaTag->getAttribute('content');
×
NEW
67
            $twitterTags[$name] = $content;
×
68
        }
69

70
        return $twitterTags;
1✔
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
    public static function getImageFromMetaTags(string $html): ?string
81
    {
82
        $ogTags = self::getOpenGraphMetaTags($html);
1✔
83
        if (isset($ogTags['og:image'])) {
1✔
84
            return $ogTags['og:image'];
1✔
85
        }
86

87
        $twitterTags = self::getTwitterMetaTags($html);
1✔
88
        if (isset($twitterTags['twitter:image'])) {
1✔
NEW
89
            return $twitterTags['twitter:image'];
×
90
        }
91

92
        return null;
1✔
93
    }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc