Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
Link
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
10 / 10
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setXTarget
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setYTarget
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setZTarget
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setPageTarget
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getXTarget
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getYTarget
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getZTarget
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPageTarget
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStream
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Pdf\Document\Page\Annotation;
16
17/**
18 * Pdf page link annotation class
19 *
20 * @category   Pop
21 * @package    Pop\Pdf
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    6.0.0
26 */
27class Link extends AbstractAnnotation
28{
29
30    /**
31     * Internal x-position target to link to on internal page
32     * @var int|float
33     */
34    protected int|float $xTarget = 0;
35
36    /**
37     * Internal y-position target to link to on internal page
38     * @var int|float
39     */
40    protected int|float $yTarget = 0;
41
42    /**
43     * Internal z-position target (zoom) to link to on internal page
44     * @var int|float
45     */
46    protected int|float $zTarget = 0;
47
48    /**
49     * Internal page object index target to link to
50     * @var int|float|null
51     */
52    protected int|float|null $pageTarget = null;
53
54    /**
55     * Constructor
56     *
57     * Instantiate a PDF link annotation object.
58     *
59     * @param  int|float $width
60     * @param  int|float $height
61     * @param  int|float $xTarget
62     * @param  int|float $yTarget
63     */
64    public function __construct(int|float $width, int|float $height, int|float $xTarget, int|float $yTarget)
65    {
66        parent::__construct($width, $height);
67
68        $this->setXTarget($xTarget);
69        $this->setYTarget($yTarget);
70    }
71
72    /**
73     * Set the X target
74     *
75     * @param  int|float $xTarget
76     * @return Link
77     */
78    public function setXTarget(int|float $xTarget): Link
79    {
80        $this->xTarget = $xTarget;
81        return $this;
82    }
83
84    /**
85     * Set the Y target
86     *
87     * @param  int|float $yTarget
88     * @return Link
89     */
90    public function setYTarget(int|float $yTarget): Link
91    {
92        $this->yTarget = $yTarget;
93        return $this;
94    }
95
96    /**
97     * Set the Z (zoom) target
98     *
99     * @param  int|float $zTarget
100     * @return Link
101     */
102    public function setZTarget(int|float $zTarget): Link
103    {
104        $this->zTarget = $zTarget;
105        return $this;
106    }
107
108    /**
109     * Set the page target
110     *
111     * @param  int|float $pageTarget
112     * @return Link
113     */
114    public function setPageTarget(int|float $pageTarget): Link
115    {
116        $this->pageTarget = $pageTarget;
117        return $this;
118    }
119
120    /**
121     * Get the X target
122     *
123     * @return int|float
124     */
125    public function getXTarget(): int|float
126    {
127        return $this->xTarget;
128    }
129
130    /**
131     * Get the Y target
132     *
133     * @return int|float
134     */
135    public function getYTarget(): int|float
136    {
137        return $this->yTarget;
138    }
139
140    /**
141     * Get the Z (zoom) target
142     *
143     * @return int|float
144     */
145    public function getZTarget(): int|float
146    {
147        return $this->zTarget;
148    }
149
150    /**
151     * Get the page target
152     *
153     * @return int|float|null
154     */
155    public function getPageTarget(): int|float|null
156    {
157        return $this->pageTarget;
158    }
159
160    /**
161     * Get the annotation stream
162     *
163     * @param  int       $i
164     * @param  int|float $x
165     * @param  int|float $y
166     * @param  int       $pageIndex
167     * @param  array     $kids
168     * @return string
169     */
170    public function getStream(int $i, int|float $x, int|float $y, int $pageIndex, array $kids): string
171    {
172        // Assemble the border parameters
173        $border = $this->hRadius . ' ' . $this->vRadius . ' ' . $this->borderWidth;
174        if (($this->dashGap != 0) && ($this->dashLength != 0)) {
175            $border .= ' [' . $this->dashGap . ' ' . $this->dashLength . ']';
176        }
177
178        $pageTargetIndex = (($this->pageTarget !== null) && isset($kids[$this->pageTarget - 1])) ?
179            $kids[$this->pageTarget - 1] :
180            $pageIndex;
181
182        return "{$i} 0 obj\n<<\n    /Type /Annot\n    /Subtype /Link\n    /Rect [{$x} {$y} " . ($this->width + $x) .
183            " " . ($this->height + $y) . "]\n    /Border [" . $border .  "]\n    /Dest [" . $pageTargetIndex .
184            " 0 R /XYZ {$this->xTarget} {$this->yTarget} {$this->zTarget}]\n>>\nendobj\n\n";
185    }
186
187}