Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
Destination
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
10 / 10
12
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
 xyz
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fitH
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fitV
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fitR
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fitB
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fitBH
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fitBV
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toPdf
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Document;
6
7use Phpdftk\Pdf\Core\PdfArray;
8use Phpdftk\Pdf\Core\PdfName;
9use Phpdftk\Pdf\Core\PdfNull;
10use Phpdftk\Pdf\Core\PdfNumber;
11use Phpdftk\Pdf\Core\PdfReference;
12use Phpdftk\Pdf\Core\Serializable;
13
14/**
15 * Explicit destination array (ISO 32000-2 Table 148).
16 *
17 * Represents a destination within a PDF document, specifying a page
18 * and the desired view when navigating to that page.
19 */
20class Destination implements Serializable
21{
22    /**
23     * @param array<int, float|null> $params
24     */
25    private function __construct(
26        private PdfReference $page,
27        private PdfName $type,
28        private array $params,
29    ) {}
30
31    /**
32     * /XYZ left top zoom — display page at given position and zoom.
33     * Null parameters indicate "unchanged".
34     */
35    public static function xyz(PdfReference $page, ?float $left = null, ?float $top = null, ?float $zoom = null): self
36    {
37        return new self($page, new PdfName('XYZ'), [$left, $top, $zoom]);
38    }
39
40    /**
41     * /Fit — fit entire page in window.
42     */
43    public static function fit(PdfReference $page): self
44    {
45        return new self($page, new PdfName('Fit'), []);
46    }
47
48    /**
49     * /FitH top — fit page width, position at top coordinate.
50     */
51    public static function fitH(PdfReference $page, ?float $top = null): self
52    {
53        return new self($page, new PdfName('FitH'), [$top]);
54    }
55
56    /**
57     * /FitV left — fit page height, position at left coordinate.
58     */
59    public static function fitV(PdfReference $page, ?float $left = null): self
60    {
61        return new self($page, new PdfName('FitV'), [$left]);
62    }
63
64    /**
65     * /FitR left bottom right top — fit rectangle in window.
66     */
67    public static function fitR(PdfReference $page, float $left, float $bottom, float $right, float $top): self
68    {
69        return new self($page, new PdfName('FitR'), [$left, $bottom, $right, $top]);
70    }
71
72    /**
73     * /FitB — fit bounding box in window.
74     */
75    public static function fitB(PdfReference $page): self
76    {
77        return new self($page, new PdfName('FitB'), []);
78    }
79
80    /**
81     * /FitBH top — fit bounding box width, position at top.
82     */
83    public static function fitBH(PdfReference $page, ?float $top = null): self
84    {
85        return new self($page, new PdfName('FitBH'), [$top]);
86    }
87
88    /**
89     * /FitBV left — fit bounding box height, position at left.
90     */
91    public static function fitBV(PdfReference $page, ?float $left = null): self
92    {
93        return new self($page, new PdfName('FitBV'), [$left]);
94    }
95
96    public function toPdf(): string
97    {
98        $items = [$this->page, $this->type];
99
100        foreach ($this->params as $param) {
101            if ($param === null) {
102                $items[] = new PdfNull();
103            } else {
104                $items[] = new PdfNumber($param);
105            }
106        }
107
108        return (new PdfArray($items))->toPdf();
109    }
110}