Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.96% covered (warning)
86.96%
20 / 23
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Stop
86.96% covered (warning)
86.96%
20 / 23
50.00% covered (danger)
50.00%
2 / 4
11.27
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
 offset
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
5.12
 stopColor
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 stopOpacity
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Svg\Gradient;
6
7use Phpdftk\Color\ColorInterface;
8use Phpdftk\Svg\Element;
9use Phpdftk\Svg\Value\Color;
10
11/**
12 * SVG `<stop>` per SVG 2 ยง13.2.3 โ€” one colour stop inside a gradient.
13 *
14 * `offset()` is clamped to `[0, 1]` (numeric in that range or a percentage
15 * in `0%`โ€“`100%`). Out-of-range values clamp per spec.
16 *
17 * `stopColor()` returns the parsed colour or null. The `currentColor`
18 * keyword resolves to null here โ€” the painter checks the raw attribute
19 * separately when it needs that distinction.
20 */
21final class Stop extends Element
22{
23    public function __construct()
24    {
25        parent::__construct('stop');
26    }
27
28    public function offset(): float
29    {
30        $raw = $this->getAttribute('offset');
31        if ($raw === null) {
32            return 0.0;
33        }
34        $value = trim($raw);
35        if (str_ends_with($value, '%')) {
36            $n = substr($value, 0, -1);
37            if (!is_numeric($n)) {
38                return 0.0;
39            }
40            return max(0.0, min(1.0, ((float) $n) / 100.0));
41        }
42        if (!is_numeric($value)) {
43            return 0.0;
44        }
45        return max(0.0, min(1.0, (float) $value));
46    }
47
48    public function stopColor(): ?ColorInterface
49    {
50        $raw = $this->getAttribute('stop-color');
51        if ($raw === null) {
52            return null;
53        }
54        return Color::parse($raw);
55    }
56
57    public function stopOpacity(): ?float
58    {
59        $raw = $this->getAttribute('stop-opacity');
60        if ($raw === null) {
61            return null;
62        }
63        if (!is_numeric(trim($raw))) {
64            return null;
65        }
66        return max(0.0, min(1.0, (float) $raw));
67    }
68}