Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AnchorFunction
100.00% covered (success)
100.00%
9 / 9
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%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Css\Value;
6
7/**
8 * `anchor([<anchor-element>] <anchor-side> [, <fallback>])` per
9 * CSS Anchor Positioning 1 §6. Used as a value for the inset
10 * properties (top / right / bottom / left, plus the logical
11 * equivalents) to position the box relative to a named anchor.
12 *
13 *   top: anchor(--my bottom)
14 *   left: anchor(--my right, 50%)
15 *   top: anchor(bottom)           — implicit anchor reference
16 *
17 * Stored parser-side. The layout engine resolves the anchor
18 * reference + side at positioning time once the anchor's PDF-
19 * space rect is known.
20 */
21final readonly class AnchorFunction extends Value
22{
23    public function __construct(
24        /**
25         * `<dashed-ident>` anchor name, e.g. `--my-anchor`. Null
26         * when the author omitted it (CSS Anchor Positioning 1
27         * §6 — the engine consults `position-anchor` to fill in
28         * the implicit reference).
29         */
30        public ?string $anchorName,
31        /**
32         * The `<anchor-side>` — Keyword (top / bottom / left /
33         * right / start / end / center / self-start / self-end /
34         * inside / outside) or Percentage.
35         */
36        public Value $side,
37        /**
38         * Optional fallback when the anchor reference can't be
39         * resolved at layout time.
40         */
41        public ?Value $fallback = null,
42    ) {}
43
44    public function toCss(): string
45    {
46        $parts = [];
47        if ($this->anchorName !== null) {
48            $parts[] = $this->anchorName;
49        }
50        $parts[] = $this->side->toCss();
51        $head = 'anchor(' . implode(' ', $parts);
52        return $this->fallback !== null
53            ? $head . ', ' . $this->fallback->toCss() . ')'
54            : $head . ')';
55    }
56}