Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
17 / 17
CRAP
100.00% covered (success)
100.00%
1 / 1
Permissions
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
17 / 17
25
100.00% covered (success)
100.00%
1 / 1
 allowPrinting
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isPrintingAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowHighResPrinting
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isHighResPrintingAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowModifying
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isModifyingAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowCopying
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isCopyingAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowAnnotating
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isAnnotatingAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowFillingForms
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isFillingFormsAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowExtractingForAccessibility
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isExtractingForAccessibilityAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowAssembling
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isAssemblingAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toPValue
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
9
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;
16
17/**
18 * Pdf permissions 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 */
27class Permissions
28{
29
30    /**
31     * Allow printing
32     * @var bool
33     */
34    protected bool $printing = true;
35
36    /**
37     * Allow high (full) resolution printing
38     * @var bool
39     */
40    protected bool $highResPrinting = true;
41
42    /**
43     * Allow modifying the document
44     * @var bool
45     */
46    protected bool $modifying = true;
47
48    /**
49     * Allow copying/extracting text and graphics
50     * @var bool
51     */
52    protected bool $copying = true;
53
54    /**
55     * Allow adding or modifying text annotations
56     * @var bool
57     */
58    protected bool $annotating = true;
59
60    /**
61     * Allow filling in existing interactive form fields
62     * @var bool
63     */
64    protected bool $fillingForms = true;
65
66    /**
67     * Allow extracting text/graphics for accessibility purposes
68     * @var bool
69     */
70    protected bool $extractingForAccessibility = true;
71
72    /**
73     * Allow document assembly (insert/delete/rotate pages, bookmarks)
74     * @var bool
75     */
76    protected bool $assembling = true;
77
78    /**
79     * Set whether printing is allowed
80     *
81     * @param  bool $allow
82     * @return Permissions
83     */
84    public function allowPrinting(bool $allow = true): Permissions
85    {
86        $this->printing = $allow;
87        return $this;
88    }
89
90    /**
91     * Determine whether printing is allowed
92     *
93     * @return bool
94     */
95    public function isPrintingAllowed(): bool
96    {
97        return $this->printing;
98    }
99
100    /**
101     * Set whether high (full) resolution printing is allowed
102     *
103     * @param  bool $allow
104     * @return Permissions
105     */
106    public function allowHighResPrinting(bool $allow = true): Permissions
107    {
108        $this->highResPrinting = $allow;
109        return $this;
110    }
111
112    /**
113     * Determine whether high (full) resolution printing is allowed
114     *
115     * @return bool
116     */
117    public function isHighResPrintingAllowed(): bool
118    {
119        return $this->highResPrinting;
120    }
121
122    /**
123     * Set whether modifying the document is allowed
124     *
125     * @param  bool $allow
126     * @return Permissions
127     */
128    public function allowModifying(bool $allow = true): Permissions
129    {
130        $this->modifying = $allow;
131        return $this;
132    }
133
134    /**
135     * Determine whether modifying the document is allowed
136     *
137     * @return bool
138     */
139    public function isModifyingAllowed(): bool
140    {
141        return $this->modifying;
142    }
143
144    /**
145     * Set whether copying/extracting text and graphics is allowed
146     *
147     * @param  bool $allow
148     * @return Permissions
149     */
150    public function allowCopying(bool $allow = true): Permissions
151    {
152        $this->copying = $allow;
153        return $this;
154    }
155
156    /**
157     * Determine whether copying/extracting text and graphics is allowed
158     *
159     * @return bool
160     */
161    public function isCopyingAllowed(): bool
162    {
163        return $this->copying;
164    }
165
166    /**
167     * Set whether adding or modifying text annotations is allowed
168     *
169     * @param  bool $allow
170     * @return Permissions
171     */
172    public function allowAnnotating(bool $allow = true): Permissions
173    {
174        $this->annotating = $allow;
175        return $this;
176    }
177
178    /**
179     * Determine whether adding or modifying text annotations is allowed
180     *
181     * @return bool
182     */
183    public function isAnnotatingAllowed(): bool
184    {
185        return $this->annotating;
186    }
187
188    /**
189     * Set whether filling in existing interactive form fields is allowed
190     *
191     * @param  bool $allow
192     * @return Permissions
193     */
194    public function allowFillingForms(bool $allow = true): Permissions
195    {
196        $this->fillingForms = $allow;
197        return $this;
198    }
199
200    /**
201     * Determine whether filling in existing interactive form fields is allowed
202     *
203     * @return bool
204     */
205    public function isFillingFormsAllowed(): bool
206    {
207        return $this->fillingForms;
208    }
209
210    /**
211     * Set whether extracting text/graphics for accessibility purposes is allowed
212     *
213     * @param  bool $allow
214     * @return Permissions
215     */
216    public function allowExtractingForAccessibility(bool $allow = true): Permissions
217    {
218        $this->extractingForAccessibility = $allow;
219        return $this;
220    }
221
222    /**
223     * Determine whether extracting text/graphics for accessibility purposes is allowed
224     *
225     * @return bool
226     */
227    public function isExtractingForAccessibilityAllowed(): bool
228    {
229        return $this->extractingForAccessibility;
230    }
231
232    /**
233     * Set whether document assembly (insert/delete/rotate pages, bookmarks) is allowed
234     *
235     * @param  bool $allow
236     * @return Permissions
237     */
238    public function allowAssembling(bool $allow = true): Permissions
239    {
240        $this->assembling = $allow;
241        return $this;
242    }
243
244    /**
245     * Determine whether document assembly (insert/delete/rotate pages, bookmarks) is allowed
246     *
247     * @return bool
248     */
249    public function isAssemblingAllowed(): bool
250    {
251        return $this->assembling;
252    }
253
254    /**
255     * Pack the permission flags into ISO 32000-1 Table 22's signed 32-bit
256     * /P value. Every bit from 7 upward that Table 22 does not assign a
257     * meaning is reserved and must read as 1, so this starts from all bits
258     * set and clears only the specific bit for each permission that is
259     * denied - it never sets bits from 0 upward, which would risk leaving a
260     * reserved bit incorrectly cleared.
261     *
262     * Bits 1 and 2 (combined value 3) are the exception: Table 22 reserves
263     * them explicitly as "must be 0", so they are cleared up front.
264     *
265     * @return int
266     */
267    public function toPValue(): int
268    {
269        $p = -1 & ~3;
270
271        if (!$this->printing) {
272            $p &= ~4;
273        }
274        if (!$this->modifying) {
275            $p &= ~8;
276        }
277        if (!$this->copying) {
278            $p &= ~16;
279        }
280        if (!$this->annotating) {
281            $p &= ~32;
282        }
283        if (!$this->fillingForms) {
284            $p &= ~256;
285        }
286        if (!$this->extractingForAccessibility) {
287            $p &= ~512;
288        }
289        if (!$this->assembling) {
290            $p &= ~1024;
291        }
292        if (!$this->highResPrinting) {
293            $p &= ~2048;
294        }
295
296        return $p;
297    }
298}