Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
7 / 8 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| EnvFunction | |
87.50% |
7 / 8 |
|
50.00% |
1 / 2 |
4.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toCss | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Css\Value; |
| 6 | |
| 7 | /** |
| 8 | * `env(<env-name> [<integer>]*, <fallback>?)` per CSS Environment |
| 9 | * Variables 1 ยง3. Resolves to a UA-provided named environment |
| 10 | * variable; commonly `safe-area-inset-*`, `titlebar-area-*`, |
| 11 | * `viewport-segment-*`. |
| 12 | * |
| 13 | * Optional positional indices follow the name for indexed env vars |
| 14 | * (e.g. `env(viewport-segment-width 0 1)`). |
| 15 | * |
| 16 | * For a static print render most env() values aren't defined and |
| 17 | * the fallback (if any) wins. The cascade preserves the parsed |
| 18 | * declaration so the renderer can register additional env values |
| 19 | * via a future hook. |
| 20 | * |
| 21 | * padding-top: env(safe-area-inset-top); |
| 22 | * padding-top: env(safe-area-inset-top, 12px); |
| 23 | * width: env(viewport-segment-width 0 1, 100%); |
| 24 | */ |
| 25 | final readonly class EnvFunction extends Value |
| 26 | { |
| 27 | /** |
| 28 | * @param list<int> $indices |
| 29 | */ |
| 30 | public function __construct( |
| 31 | public string $name, |
| 32 | public array $indices = [], |
| 33 | public ?Value $fallback = null, |
| 34 | ) {} |
| 35 | |
| 36 | public function toCss(): string |
| 37 | { |
| 38 | $parts = [$this->name]; |
| 39 | foreach ($this->indices as $i) { |
| 40 | $parts[] = (string) $i; |
| 41 | } |
| 42 | $head = 'env(' . implode(' ', $parts); |
| 43 | return $this->fallback !== null |
| 44 | ? $head . ', ' . $this->fallback->toCss() . ')' |
| 45 | : $head . ')'; |
| 46 | } |
| 47 | } |