Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
LinearGradient
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toCss
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Css\Value;
6
7/**
8 * `linear-gradient(<angle> | to <side-or-corner>, <stops>)`. The angle is
9 * stored in degrees normalised to [0, 360). A `to <side>` (e.g. `to right`)
10 * converts to a fixed equivalent angle, but a `to <corner>` also records
11 * the {@see GradientCorner}: a corner's angle depends on the gradient box's
12 * aspect ratio, so it can only be finalised at paint time. `$angleDeg` then
13 * holds the square-box angle as a fallback.
14 */
15final readonly class LinearGradient extends Gradient
16{
17    /** @param list<GradientStop> $stops */
18    public function __construct(
19        public float $angleDeg,
20        public array $stops,
21        public bool $repeating = false,
22        public ?ColorSpace $interpolationSpace = null,
23        public ?HueInterpolation $hueInterpolation = null,
24        public ?GradientCorner $corner = null,
25    ) {}
26
27    public function toCss(): string
28    {
29        $prefix = $this->repeating ? 'repeating-linear-gradient' : 'linear-gradient';
30        $stops = implode(', ', array_map(static fn(GradientStop $s): string => $s->toCss(), $this->stops));
31        $angle = (fmod($this->angleDeg, 1.0) === 0.0 ? (int) $this->angleDeg : $this->angleDeg) . 'deg';
32        $head = $angle . InterpolationMethodCss::serialise($this->interpolationSpace, $this->hueInterpolation);
33        return sprintf('%s(%s, %s)', $prefix, $head, $stops);
34    }
35}