Passed
Push — master ( a4bb6d...8b8a15 )
by Konrad
06:35
created

Header::has()   A

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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
 *
10
 * @license LGPLv3
11
 * @url     <https://github.com/smalot/pdfparser>
12
 *
13
 *  PdfParser is a pdf library written in PHP, extraction oriented.
14
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
15
 *
16
 *  This program is free software: you can redistribute it and/or modify
17
 *  it under the terms of the GNU Lesser General Public License as published by
18
 *  the Free Software Foundation, either version 3 of the License, or
19
 *  (at your option) any later version.
20
 *
21
 *  This program is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU Lesser General Public License for more details.
25
 *
26
 *  You should have received a copy of the GNU Lesser General Public License
27
 *  along with this program.
28
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
29
 */
30
31
namespace Smalot\PdfParser;
32
33
use Smalot\PdfParser\Element\ElementArray;
34
use Smalot\PdfParser\Element\ElementMissing;
35
use Smalot\PdfParser\Element\ElementStruct;
36
use Smalot\PdfParser\Element\ElementXRef;
37
38
/**
39
 * Class Header
40
 */
41
class Header
42
{
43
    /**
44
     * @var Document
45
     */
46
    protected $document = null;
47
48
    /**
49
     * @var Element[]
50
     */
51
    protected $elements = null;
52
53
    /**
54
     * @param Element[] $elements list of elements
55
     * @param Document  $document document
56
     */
57 51
    public function __construct($elements = [], Document $document = null)
58
    {
59 51
        $this->elements = $elements;
60 51
        $this->document = $document;
61 51
    }
62
63 34
    public function init()
64
    {
65 34
        foreach ($this->elements as $element) {
66 34
            if ($element instanceof Element) {
67 34
                $element->init();
68
            }
69
        }
70 34
    }
71
72
    /**
73
     * Returns all elements.
74
     */
75 28
    public function getElements()
76
    {
77 28
        foreach ($this->elements as $name => $element) {
78 28
            $this->resolveXRef($name);
79
        }
80
81 28
        return $this->elements;
82
    }
83
84
    /**
85
     * Used only for debug.
86
     *
87
     * @return array
88
     */
89 1
    public function getElementTypes()
90
    {
91 1
        $types = [];
92
93 1
        foreach ($this->elements as $key => $element) {
94 1
            $types[$key] = \get_class($element);
95
        }
96
97 1
        return $types;
98
    }
99
100
    /**
101
     * @param bool $deep
102
     *
103
     * @return array
104
     */
105 26
    public function getDetails($deep = true)
106
    {
107 26
        $values = [];
108 26
        $elements = $this->getElements();
109
110 26
        foreach ($elements as $key => $element) {
111 26
            if ($element instanceof self && $deep) {
112
                $values[$key] = $element->getDetails($deep);
113 26
            } elseif ($element instanceof PDFObject && $deep) {
114 1
                $values[$key] = $element->getDetails(false);
115 26
            } elseif ($element instanceof ElementArray) {
116 2
                if ($deep) {
117 2
                    $values[$key] = $element->getDetails();
118
                }
119 26
            } elseif ($element instanceof Element) {
120 26
                $values[$key] = (string) $element;
121
            }
122
        }
123
124 26
        return $values;
125
    }
126
127
    /**
128
     * Indicate if an element name is available in header.
129
     *
130
     * @param string $name The name of the element
131
     *
132
     * @return bool
133
     */
134 37
    public function has($name)
135
    {
136 37
        return \array_key_exists($name, $this->elements);
137
    }
138
139
    /**
140
     * @param string $name
141
     *
142
     * @return Element|PDFObject
143
     */
144 38
    public function get($name)
145
    {
146 38
        if (\array_key_exists($name, $this->elements)) {
147 38
            return $this->resolveXRef($name);
148
        }
149
150 34
        return new ElementMissing();
151
    }
152
153
    /**
154
     * Resolve XRef to object.
155
     *
156
     * @param string $name
157
     *
158
     * @return Element|PDFObject
159
     *
160
     * @throws \Exception
161
     */
162 40
    protected function resolveXRef($name)
163
    {
164 40
        if (($obj = $this->elements[$name]) instanceof ElementXRef && null !== $this->document) {
165
            /** @var ElementXRef $obj */
166 29
            $object = $this->document->getObjectById($obj->getId());
167
168 29
            if (null === $object) {
169 2
                return new ElementMissing();
170
            }
171
172
            // Update elements list for future calls.
173 29
            $this->elements[$name] = $object;
174
        }
175
176 40
        return $this->elements[$name];
177
    }
178
179
    /**
180
     * @param string   $content  The content to parse
181
     * @param Document $document The document
182
     * @param int      $position The new position of the cursor after parsing
183
     *
184
     * @return Header
185
     */
186 19
    public static function parse($content, Document $document, &$position = 0)
187
    {
188
        /* @var Header $header */
189 19
        if ('<<' == substr(trim($content), 0, 2)) {
190 18
            $header = ElementStruct::parse($content, $document, $position);
191
        } else {
192 6
            $elements = ElementArray::parse($content, $document, $position);
193 6
            $header = new self([], $document);
194
195 6
            if ($elements) {
196 5
                $header = new self($elements->getRawContent(), null);
197
            }
198
        }
199
200 19
        if ($header) {
201 19
            return $header;
202
        }
203
204
        // Build an empty header.
205
        return new self([], $document);
206
    }
207
}
208