Completed
Pull Request — master (#304)
by Jeremy
05:53 queued 03:07
created

Header::parse()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 20
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.9332
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 36
    public function __construct($elements = [], Document $document = null)
58
    {
59 36
        $this->elements = $elements;
60 36
        $this->document = $document;
61 36
    }
62
63
    /**
64
     * Returns all elements.
65
     */
66 19
    public function getElements()
67
    {
68 19
        foreach ($this->elements as $name => $element) {
69 19
            $this->resolveXRef($name);
70
        }
71
72 19
        return $this->elements;
73
    }
74
75
    /**
76
     * Used only for debug.
77
     *
78
     * @return array
79
     */
80 1
    public function getElementTypes()
81
    {
82 1
        $types = [];
83
84 1
        foreach ($this->elements as $key => $element) {
85 1
            $types[$key] = \get_class($element);
86
        }
87
88 1
        return $types;
89
    }
90
91
    /**
92
     * @param bool $deep
93
     *
94
     * @return array
95
     */
96 17
    public function getDetails($deep = true)
97
    {
98 17
        $values = [];
99 17
        $elements = $this->getElements();
100
101 17
        foreach ($elements as $key => $element) {
102 17
            if ($element instanceof self && $deep) {
103
                $values[$key] = $element->getDetails($deep);
104 17
            } elseif ($element instanceof PDFObject && $deep) {
105 1
                $values[$key] = $element->getDetails(false);
106 17
            } elseif ($element instanceof ElementArray) {
107 2
                if ($deep) {
108 2
                    $values[$key] = $element->getDetails();
109
                }
110 17
            } elseif ($element instanceof Element) {
111 17
                $values[$key] = (string) $element;
112
            }
113
        }
114
115 17
        return $values;
116
    }
117
118
    /**
119
     * Indicate if an element name is available in header.
120
     *
121
     * @param string $name The name of the element
122
     *
123
     * @return bool
124
     */
125 25
    public function has($name)
126
    {
127 25
        return \array_key_exists($name, $this->elements);
128
    }
129
130
    /**
131
     * @param string $name
132
     *
133
     * @return Element|PDFObject
134
     */
135 25
    public function get($name)
136
    {
137 25
        if (\array_key_exists($name, $this->elements)) {
138 25
            return $this->resolveXRef($name);
139
        }
140
141 22
        return new ElementMissing();
142
    }
143
144
    /**
145
     * Resolve XRef to object.
146
     *
147
     * @param string $name
148
     *
149
     * @return Element|PDFObject
150
     *
151
     * @throws \Exception
152
     */
153 27
    protected function resolveXRef($name)
154
    {
155 27
        if (($obj = $this->elements[$name]) instanceof ElementXRef && null !== $this->document) {
156
            /** @var ElementXRef $obj */
157 20
            $object = $this->document->getObjectById($obj->getId());
158
159 20
            if (null === $object) {
160 2
                return new ElementMissing();
161
            }
162
163
            // Update elements list for future calls.
164 20
            $this->elements[$name] = $object;
165
        }
166
167 27
        return $this->elements[$name];
168
    }
169
170
    /**
171
     * @param string   $content  The content to parse
172
     * @param Document $document The document
173
     * @param int      $position The new position of the cursor after parsing
174
     *
175
     * @return Header
176
     */
177 14
    public static function parse($content, Document $document, &$position = 0)
178
    {
179
        /* @var Header $header */
180 14
        if ('<<' == substr(trim($content), 0, 2)) {
181 14
            $header = ElementStruct::parse($content, $document, $position);
182
        } else {
183 3
            $elements = ElementArray::parse($content, $document, $position);
184 3
            $header = new self([], $document);
185
186 3
            if ($elements) {
187 3
                $header = new self($elements->getRawContent(), null);
188
            }
189
        }
190
191 14
        if ($header) {
192 14
            return $header;
193
        }
194
195
        // Build an empty header.
196
        return new self([], $document);
197
    }
198
}
199