Passed
Branch master (f7fac8)
by Sebastien
02:47
created

Page::getXObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * @file
5
 *          This file is part of the PdfParser library.
6
 *
7
 * @author  Sébastien MALOT <[email protected]>
8
 * @date    2017-01-03
9
 * @license LGPLv3
10
 * @url     <https://github.com/smalot/pdfparser>
11
 *
12
 *  PdfParser is a pdf library written in PHP, extraction oriented.
13
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
14
 *
15
 *  This program is free software: you can redistribute it and/or modify
16
 *  it under the terms of the GNU Lesser General Public License as published by
17
 *  the Free Software Foundation, either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  This program is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU Lesser General Public License for more details.
24
 *
25
 *  You should have received a copy of the GNU Lesser General Public License
26
 *  along with this program.
27
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
28
 *
29
 */
30
31
namespace Smalot\PdfParser;
32
33
use Smalot\PdfParser\Element\ElementArray;
34
use Smalot\PdfParser\Element\ElementMissing;
35
use Smalot\PdfParser\Element\ElementXRef;
36
use Smalot\PdfParser\Element\ElementNull;
37
38
/**
39
 * Class Page
40
 *
41
 * @package Smalot\PdfParser
42
 */
43
class Page extends PDFObject
44
{
45
    /**
46
     * @var Font[]
47
     */
48
    protected $fonts = null;
49
50
    /**
51
     * @var PDFObject[]
52
     */
53
    protected $xobjects = null;
54
55
    /**
56
     * @return Font[]
57
     */
58
    public function getFonts()
59
    {
60
        if (!is_null($this->fonts)) {
0 ignored issues
show
introduced by
The condition is_null($this->fonts) is always false.
Loading history...
61
            return $this->fonts;
62
        }
63
64
        $resources = $this->get('Resources');
65
66
        if (method_exists($resources, 'has') && $resources->has('Font')) {
67
68
            if ($resources->get('Font') instanceof Header) {
69
                $fonts = $resources->get('Font')->getElements();
70
            } else {
71
                $fonts = $resources->get('Font')->getHeader()->getElements();
72
            }
73
74
            $table = array();
75
76
            foreach ($fonts as $id => $font) {
77
                if ($font instanceof Font) {
78
                    $table[$id] = $font;
79
80
                    // Store too on cleaned id value (only numeric)
81
                    $id = preg_replace('/[^0-9\.\-_]/', '', $id);
82
                    if ($id != '') {
83
                        $table[$id] = $font;
84
                    }
85
                }
86
            }
87
88
            return ($this->fonts = $table);
89
        } else {
90
            return array();
91
        }
92
    }
93
94
    /**
95
     * @param string $id
96
     *
97
     * @return Font
98
     */
99
    public function getFont($id)
100
    {
101
        $fonts = $this->getFonts();
102
103
        if (isset($fonts[$id])) {
104
            return $fonts[$id];
105
        } else {
106
            $id = preg_replace('/[^0-9\.\-_]/', '', $id);
107
108
            if (isset($fonts[$id])) {
109
                return $fonts[$id];
110
            } else {
111
                return null;
112
            }
113
        }
114
    }
115
116
    /**
117
     * Support for XObject
118
     *
119
     * @return PDFObject[]
120
     */
121
    public function getXObjects()
122
    {
123
        if (!is_null($this->xobjects)) {
0 ignored issues
show
introduced by
The condition is_null($this->xobjects) is always false.
Loading history...
124
            return $this->xobjects;
125
        }
126
127
        $resources = $this->get('Resources');
128
129
        if (method_exists($resources, 'has') && $resources->has('XObject')) {
130
131
            if ($resources->get('XObject') instanceof Header) {
132
                $xobjects = $resources->get('XObject')->getElements();
133
            } else {
134
                $xobjects = $resources->get('XObject')->getHeader()->getElements();
135
            }
136
137
            $table = array();
138
139
            foreach ($xobjects as $id => $xobject) {
140
                $table[$id] = $xobject;
141
142
                // Store too on cleaned id value (only numeric)
143
                $id = preg_replace('/[^0-9\.\-_]/', '', $id);
144
                if ($id != '') {
145
                    $table[$id] = $xobject;
146
                }
147
            }
148
149
            return ($this->xobjects = $table);
150
        } else {
151
            return array();
152
        }
153
    }
154
155
    /**
156
     * @param string $id
157
     *
158
     * @return PDFObject
159
     */
160
    public function getXObject($id)
161
    {
162
        $xobjects = $this->getXObjects();
163
164
        if (isset($xobjects[$id])) {
165
            return $xobjects[$id];
166
        } else {
167
            return null;
168
            /*$id = preg_replace('/[^0-9\.\-_]/', '', $id);
169
170
            if (isset($xobjects[$id])) {
171
                return $xobjects[$id];
172
            } else {
173
                return null;
174
            }*/
175
        }
176
    }
177
178
    /**
179
     * @param Page
180
     *
181
     * @return string
182
     */
183
    public function getText(Page $page = null)
184
    {
185
        if ($contents = $this->get('Contents')) {
186
187
            if ($contents instanceof ElementMissing) {
188
                return '';
189
			} elseif ($contents instanceof ElementNull) {
190
				return '';
191
            } elseif ($contents instanceof PDFObject) {
0 ignored issues
show
introduced by
$contents is never a sub-type of Smalot\PdfParser\PDFObject.
Loading history...
192
                $elements = $contents->getHeader()->getElements();
193
194
                if (is_numeric(key($elements))) {
195
                    $new_content = '';
196
197
                    foreach ($elements as $element) {
198
                        if ($element instanceof ElementXRef) {
199
                            $new_content .= $element->getObject()->getContent();
200
                        } else {
201
                            $new_content .= $element->getContent();
202
                        }
203
                    }
204
205
                    $header   = new Header(array(), $this->document);
206
                    $contents = new PDFObject($this->document, $header, $new_content);
207
                }
208
            } elseif ($contents instanceof ElementArray) {
209
                // Create a virtual global content.
210
                $new_content = '';
211
212
                foreach ($contents->getContent() as $content) {
213
                    $new_content .= $content->getContent() . "\n";
214
                }
215
216
                $header   = new Header(array(), $this->document);
217
                $contents = new PDFObject($this->document, $header, $new_content);
218
            }
219
220
            return $contents->getText($this);
0 ignored issues
show
Bug introduced by
The method getText() does not exist on Smalot\PdfParser\Element. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
            return $contents->/** @scrutinizer ignore-call */ getText($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
221
        }
222
223
        return '';
224
    }
225
226
	/**
227
	 * @param Page
228
	 *
229
	 * @return array
230
	 */
231
	public function getTextArray(Page $page = null)
232
	{
233
		if ($contents = $this->get('Contents')) {
234
235
			if ($contents instanceof ElementMissing) {
236
				return array();
237
			} elseif ($contents instanceof ElementNull) {
238
				return array();
239
			} elseif ($contents instanceof PDFObject) {
0 ignored issues
show
introduced by
$contents is never a sub-type of Smalot\PdfParser\PDFObject.
Loading history...
240
				$elements = $contents->getHeader()->getElements();
241
242
				if (is_numeric(key($elements))) {
243
					$new_content = '';
244
245
					/** @var PDFObject $element */
246
					foreach ($elements as $element) {
247
						if ($element instanceof ElementXRef) {
248
							$new_content .= $element->getObject()->getContent();
249
						} else {
250
							$new_content .= $element->getContent();
251
						}
252
					}
253
254
					$header   = new Header(array(), $this->document);
255
					$contents = new PDFObject($this->document, $header, $new_content);
256
				}
257
			} elseif ($contents instanceof ElementArray) {
258
				// Create a virtual global content.
259
				$new_content = '';
260
261
				/** @var PDFObject $content */
262
          foreach ($contents->getContent() as $content) {
263
					$new_content .= $content->getContent() . "\n";
264
				}
265
266
				$header   = new Header(array(), $this->document);
267
				$contents = new PDFObject($this->document, $header, $new_content);
268
			}
269
270
			return $contents->getTextArray($this);
0 ignored issues
show
Bug introduced by
The method getTextArray() does not exist on Smalot\PdfParser\Element. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

270
			return $contents->/** @scrutinizer ignore-call */ getTextArray($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
271
		}
272
273
		return array();
274
	}
275
}
276