Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Youtube
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 __toString
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
 raw
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 json
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Value;
6
7use Override;
8
9use function Cosray\escape;
10
11class Youtube extends Value
12{
13    #[Override]
14    public function __toString(): string
15    {
16        $x = (float) ($this->meta('aspectRatioX', 16) ?: 16);
17        $y = (float) ($this->meta('aspectRatioY', 9) ?: 9);
18        $percent = number_format(($y / $x) * 100, 2, '.', '');
19        $iframeStyle = 'position: absolute; top: 0; left: 0; width: 100%; height: 100%';
20
21        return (
22            '<div class="youtube-container">'
23                . '<div style="position: relative; padding-top: '
24                . $percent
25                . '%">'
26                . '<iframe class="youtube" style="'
27                . $iframeStyle
28                . '" '
29                . 'src="https://www.youtube.com/embed/'
30                . escape((string) $this->raw())
31                . '" allowfullscreen></iframe>'
32                . '</div></div>'
33        );
34    }
35
36    #[Override]
37    public function raw(): mixed
38    {
39        return $this->value();
40    }
41
42    /** @return ?array{id: string, ratio: string} */
43    #[Override]
44    public function json(): ?array
45    {
46        if (!$this->isset()) {
47            return null;
48        }
49
50        $x = $this->meta('aspectRatioX', 16) ?: 16;
51        $y = $this->meta('aspectRatioY', 9) ?: 9;
52
53        return ['id' => (string) $this->raw(), 'ratio' => "{$x}/{$y}"];
54    }
55
56    #[Override]
57    public function isset(): bool
58    {
59        return is_string($this->raw()) && $this->raw() !== '';
60    }
61}