Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
18 / 20 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Image | |
90.00% |
18 / 20 |
|
75.00% |
6 / 8 |
14.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| x | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| y | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| width | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| height | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| href | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| preserveAspectRatio | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| parseOptionalNonNegativeLength | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Svg; |
| 6 | |
| 7 | /** |
| 8 | * SVG `<image>` per SVG 2 §12.6 — references a raster (or SVG) bitmap to |
| 9 | * be drawn at a given position and size. |
| 10 | * |
| 11 | * `href()` deliberately returns the **raw** URL string. The parser never |
| 12 | * opens the URL; the painter in `phpdftk/svg-to-pdf` resolves it through |
| 13 | * the resource-loader gate established for `<img>` in 1L. That gate |
| 14 | * decides what's safe — `data:` URLs, allowlisted local files, etc. — |
| 15 | * keeping the security policy in one place. |
| 16 | */ |
| 17 | final class Image extends Element |
| 18 | { |
| 19 | public function __construct() |
| 20 | { |
| 21 | parent::__construct('image'); |
| 22 | } |
| 23 | |
| 24 | public function x(): float |
| 25 | { |
| 26 | return $this->parseLengthOrZero('x'); |
| 27 | } |
| 28 | |
| 29 | public function y(): float |
| 30 | { |
| 31 | return $this->parseLengthOrZero('y'); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * `width` — non-negative. SVG 2 §12.6 makes both width and height |
| 36 | * optional; we return null when absent so the painter knows to |
| 37 | * fall back to the source bitmap's intrinsic dimensions. |
| 38 | */ |
| 39 | public function width(): ?float |
| 40 | { |
| 41 | return $this->parseOptionalNonNegativeLength('width'); |
| 42 | } |
| 43 | |
| 44 | public function height(): ?float |
| 45 | { |
| 46 | return $this->parseOptionalNonNegativeLength('height'); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Raw URL string from `href` (preferred) or `xlink:href` (legacy). |
| 51 | * Unlike `<use>` we don't strip anything — the painter / resource |
| 52 | * loader needs the full string to decide whether the URL points at |
| 53 | * a `data:` blob, an allowlisted local file, or something to reject. |
| 54 | */ |
| 55 | public function href(): ?string |
| 56 | { |
| 57 | $raw = $this->getAttribute('href') |
| 58 | ?? $this->getAttribute('xlink:href'); |
| 59 | if ($raw === null) { |
| 60 | return null; |
| 61 | } |
| 62 | $trimmed = trim($raw); |
| 63 | return $trimmed === '' ? null : $trimmed; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * `preserveAspectRatio` returned as a raw string. The grammar |
| 68 | * (SVG 2 §7.10) is `<align> [ <meetOrSlice> ]?` — parsing into a |
| 69 | * typed value is a 3O painter concern. |
| 70 | */ |
| 71 | public function preserveAspectRatio(): ?string |
| 72 | { |
| 73 | $raw = $this->getAttribute('preserveAspectRatio'); |
| 74 | return $raw === null ? null : trim($raw); |
| 75 | } |
| 76 | |
| 77 | private function parseOptionalNonNegativeLength(string $attr): ?float |
| 78 | { |
| 79 | $raw = $this->getAttribute($attr); |
| 80 | if ($raw === null) { |
| 81 | return null; |
| 82 | } |
| 83 | if (preg_match('/^\s*([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)/', $raw, $m) !== 1) { |
| 84 | return null; |
| 85 | } |
| 86 | $value = (float) $m[1]; |
| 87 | return $value < 0.0 ? null : $value; |
| 88 | } |
| 89 | } |