Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
12 / 14
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
View
85.71% covered (warning)
85.71%
12 / 14
66.67% covered (warning)
66.67%
2 / 3
6.10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 viewBox
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
4.07
 preserveAspectRatio
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Svg;
6
7/**
8 * SVG 2 §6.3 — `<view>` element. Defines a named viewport
9 * declaratively so external links can target a specific region
10 * of an SVG document via `file.svg#viewId`. Properties mirror
11 * the root `<svg>`: `viewBox`, `preserveAspectRatio`, optionally
12 * `zoomAndPan` and `viewTarget`.
13 *
14 * Server-side rendering only activates a view when the caller
15 * passes the fragment identifier explicitly. At document level
16 * the element never paints — it's a referenceable definition.
17 */
18final class View extends Element
19{
20    public function __construct()
21    {
22        parent::__construct('view');
23    }
24
25    /**
26     * @return array{0: float, 1: float, 2: float, 3: float}|null
27     */
28    public function viewBox(): ?array
29    {
30        $vb = $this->getAttribute('viewBox');
31        if ($vb === null) {
32            return null;
33        }
34        $parts = preg_split('/[\s,]+/', trim($vb)) ?: [];
35        if (count($parts) !== 4) {
36            return null;
37        }
38        return [
39            (float) $parts[0],
40            (float) $parts[1],
41            (float) $parts[2],
42            (float) $parts[3],
43        ];
44    }
45
46    public function preserveAspectRatio(): ?string
47    {
48        return $this->getAttribute('preserveAspectRatio');
49    }
50}