Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Url | |
100.00% |
13 / 13 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setUrl | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| encryptWith | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getStream | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | declare(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 | */ |
| 15 | namespace Pop\Pdf\Document\Page\Annotation; |
| 16 | |
| 17 | /** |
| 18 | * Pdf page url 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.2.0 |
| 26 | */ |
| 27 | class Url extends AbstractAnnotation |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * External URL to link to |
| 32 | * @var ?string |
| 33 | */ |
| 34 | protected ?string $url = null; |
| 35 | |
| 36 | /** |
| 37 | * Encrypted, already-escaped URI, set by encryptWith() - used by |
| 38 | * getStream() in place of the plain URL when present. |
| 39 | * @var ?string |
| 40 | */ |
| 41 | protected ?string $encryptedUrl = null; |
| 42 | |
| 43 | /** |
| 44 | * Constructor |
| 45 | * |
| 46 | * Instantiate a PDF URL annotation object. |
| 47 | * |
| 48 | * @param int|float $width |
| 49 | * @param int|float $height |
| 50 | * @param string $url |
| 51 | */ |
| 52 | public function __construct(int|float $width, int|float $height, string $url) |
| 53 | { |
| 54 | parent::__construct($width, $height); |
| 55 | $this->setUrl($url); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the URL |
| 60 | * |
| 61 | * @param string $url |
| 62 | * @return Url |
| 63 | */ |
| 64 | public function setUrl(string $url): Url |
| 65 | { |
| 66 | $this->url = $url; |
| 67 | return $this; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the URL link |
| 72 | * |
| 73 | * @return ?string |
| 74 | */ |
| 75 | public function getUrl(): ?string |
| 76 | { |
| 77 | return $this->url; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Encrypt this annotation's URI target for a compiled, encrypted |
| 82 | * document. Called by Build\Compiler::prepareAnnotations() before |
| 83 | * getStream() - mirrors Build\PdfObject\InfoObject::encryptWith()'s |
| 84 | * shape and CR/LF-escaping rationale exactly (encrypted bytes are |
| 85 | * arbitrary binary, not human-authored text, and routinely contain raw |
| 86 | * bytes a literal-string reader would otherwise silently normalize). |
| 87 | * |
| 88 | * @param callable $encryptor |
| 89 | * @return Url |
| 90 | */ |
| 91 | public function encryptWith(callable $encryptor): Url |
| 92 | { |
| 93 | $this->encryptedUrl = \Pop\Pdf\Document\Page\Text::escape($encryptor($this->url)); |
| 94 | return $this; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the annotation stream |
| 99 | * |
| 100 | * @param int $i |
| 101 | * @param int|float $x |
| 102 | * @param int|float $y |
| 103 | * @return string |
| 104 | */ |
| 105 | public function getStream(int $i, int|float $x, int|float $y): string |
| 106 | { |
| 107 | // Assemble the border parameters |
| 108 | $border = $this->hRadius . ' ' . $this->vRadius . ' ' . $this->borderWidth; |
| 109 | if (($this->dashGap != 0) && ($this->dashLength != 0)) { |
| 110 | $border .= ' [' . $this->dashGap . ' ' . $this->dashLength . ']'; |
| 111 | } |
| 112 | |
| 113 | // Return the stream |
| 114 | return "{$i} 0 obj\n<<\n /Type /Annot\n /Subtype /Link\n /Rect [{$x} {$y} " . ($this->width + $x) . |
| 115 | " " . ($this->height + $y) . "]\n /Border [" . $border . "]\n /A <</S /URI /URI (" . |
| 116 | ($this->encryptedUrl ?? $this->url) . ")>>\n>>\nendobj\n\n"; |
| 117 | } |
| 118 | |
| 119 | } |