Bookmarks::resetArrayIndizes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * PDFtk wrapper
4
 *
5
 * @copyright 2014-2019 Institute of Legal Medicine, Medical University of Innsbruck
6
 * @author Martin Pircher <[email protected]>
7
 * @author Andreas Erhard <[email protected]>
8
 * @license LGPL-3.0-only
9
 * @link http://www.gerichtsmedizin.at/
10
 *
11
 * @package pdftk
12
 */
13
14
namespace Gmi\Toolkit\Pdftk;
15
16
use Gmi\Toolkit\Pdftk\Exception\PdfException;
17
18
/**
19
 * Apply bookmarks to PDF.
20
 */
21
class Bookmarks
22
{
23
    /**
24
     * Bookmarks.
25
     *
26
     * @var Bookmark[]
27
     */
28
    private $bookmarks = [];
29
30
    /**
31
     * Maximum page number.
32
     *
33
     * @var int
34
     */
35
    private $maxpage = -1;
36
37
    /**
38
     * @var PdftkWrapper
39
     */
40
    private $wrapper;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param PdftkWrapper $wrapper
46
     */
47 16
    public function __construct(PdftkWrapper $wrapper = null)
48
    {
49 16
        $this->wrapper = $wrapper ?: new PdftkWrapper();
50 16
    }
51
52
    /**
53
     * Add bookmark to page.
54
     *
55
     * @param Bookmark $bookmark
56
     *
57
     * @return self
58
     */
59 11
    public function add(Bookmark $bookmark)
60
    {
61 11
        if (!is_int($bookmark->getPageNumber()) || $bookmark->getPageNumber() < 1) {
0 ignored issues
show
introduced by
The condition is_int($bookmark->getPageNumber()) is always true.
Loading history...
62 2
            throw new PdfException(sprintf('Invalid page number: %s', $bookmark->getPageNumber()));
63 9
        } elseif ($this->maxpage > 0 && $bookmark->getPageNumber() > $this->maxpage) {
64 1
            throw new PdfException('Page number out of range!');
65
        }
66
67 8
        $this->bookmarks[] = $bookmark;
68
69 8
        return $this;
70
    }
71
72
    /**
73
     * Returns all bookmarks.
74
     *
75
     * @return Bookmark[]
76
     */
77 8
    public function all()
78
    {
79 8
        return $this->bookmarks;
80
    }
81
82
    /**
83
     * Deletes a bookmark.
84
     *
85
     * @param Bookmark $bookmark
86
     *
87
     * @return self
88
     */
89 1
    public function remove(Bookmark $bookmark)
90
    {
91 1
        foreach ($this->bookmarks as $key => $currentBookmark) {
92
            // weak comparison - value objects with equal values are considered equal
93 1
            if ($currentBookmark == $bookmark) {
94 1
                unset($this->bookmarks[$key]);
95
            }
96
        }
97
98 1
        $this->resetArrayIndizes();
99
100 1
        return $this;
101
    }
102
103
    /**
104
     * Delete bookmark from page.
105
     *
106
     * @param int $pageNumber
107
     *
108
     * @return self
109
     */
110 1
    public function removeByPageNumber($pageNumber)
111
    {
112 1
        foreach ($this->bookmarks as $key => $currentBookmark) {
113 1
            if ($currentBookmark->getPageNumber() === $pageNumber) {
114 1
                unset($this->bookmarks[$key]);
115
            }
116
        }
117
118 1
        $this->resetArrayIndizes();
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * Remove all bookmarks.
125
     *
126
     * @return self
127
     */
128 3
    public function clear()
129
    {
130 3
        $this->bookmarks = [];
131
132 3
        return $this;
133
    }
134
135
    /**
136
     * Apply bookmarks to PDF file.
137
     *
138
     * @param string $infile
139
     * @param string $outfile
140
     *
141
     * @return self
142
     */
143 3
    public function apply($infile, $outfile = null)
144
    {
145 3
        $this->wrapper->updatePdfDataFromDump($infile, $this->buildBookmarksBlock(), $outfile);
146
147 2
        return $this;
148
    }
149
150
    /**
151
     * Imports bookmarks from a PDF file.
152
     *
153
     * @param string $infile
154
     *
155
     * @return self
156
     */
157 3
    public function import($infile)
158
    {
159 3
        $dump = $this->wrapper->getPdfDataDump($infile);
160 2
        $this->importFromDump($dump);
161
162 2
        return $this;
163
    }
164
165
    /**
166
     * Imports bookmarks from a pdftk dump.
167
     *
168
     * @param string $dump
169
     *
170
     * @return self
171
     */
172 4
    public function importFromDump($dump)
173
    {
174 4
        $matches = [];
175
        $regex = '/BookmarkBegin\nBookmarkTitle: (?<title>.+)\n' .
176 4
                 'BookmarkLevel: (?<level>[0-9]+)\nBookmarkPageNumber: (?<page>[0-9]+)/';
177 4
        preg_match_all($regex, $dump, $matches, PREG_SET_ORDER);
178
179 4
        foreach ($matches as $bm) {
180 4
            $bookmark = new Bookmark();
181
            $bookmark
182 4
                ->setTitle($bm['title'])
183 4
                ->setPageNumber((int) $bm['page'])
184 4
                ->setLevel((int) $bm['level'])
185
            ;
186
187 4
            $this->add($bookmark);
188
        }
189
190 4
        return $this;
191
    }
192
193
    /**
194
     * Sets the maximum page number of the PDF to validate page numbers.
195
     *
196
     * @internal
197
     *
198
     * @param int $maxpage
199
     *
200
     * @return self
201
     */
202 3
    public function setMaxpage($maxpage)
203
    {
204 3
        $this->maxpage = $maxpage;
205
206 3
        return $this;
207
    }
208
209
    /**
210
     * Builds an Bookmark string for all bookmarks.
211
     *
212
     * @return string
213
     */
214 3
    private function buildBookmarksBlock()
215
    {
216 3
        $result = '';
217
218 3
        foreach ($this->bookmarks as $bookmark) {
219 2
            $result .= $this->buildBookmarkBlock($bookmark);
220
        }
221
222 3
        return $result;
223
    }
224
225
    /**
226
     * Builds an Bookmark string for a single bookmark.
227
     *
228
     * @param Bookmark $bookmark
229
     *
230
     * @return string
231
     */
232 2
    private function buildBookmarkBlock(Bookmark $bookmark)
233
    {
234
        return
235 2
            'BookmarkBegin' . PHP_EOL .
236 2
            'BookmarkTitle: ' . $bookmark->getTitle() . PHP_EOL .
237 2
            'BookmarkLevel: ' . $bookmark->getLevel() . PHP_EOL .
238 2
            'BookmarkPageNumber: ' . $bookmark->getPageNumber() . PHP_EOL;
239
    }
240
241
    /**
242
     * Resets the bookmark array indizes (e.g. after removal of a bookmark).
243
     */
244 2
    private function resetArrayIndizes()
245
    {
246 2
        $this->bookmarks = array_values($this->bookmarks);
247 2
    }
248
}
249