ElementArray::resolveXRef()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 1
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * @file
5
 *          This file is part of the PdfParser library.
6
 *
7
 * @author  Sébastien MALOT <[email protected]>
8
 *
9
 * @date    2017-01-03
10
 *
11
 * @license LGPLv3
12
 *
13
 * @url     <https://github.com/smalot/pdfparser>
14
 *
15
 *  PdfParser is a pdf library written in PHP, extraction oriented.
16
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
17
 *
18
 *  This program is free software: you can redistribute it and/or modify
19
 *  it under the terms of the GNU Lesser General Public License as published by
20
 *  the Free Software Foundation, either version 3 of the License, or
21
 *  (at your option) any later version.
22
 *
23
 *  This program is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU Lesser General Public License for more details.
27
 *
28
 *  You should have received a copy of the GNU Lesser General Public License
29
 *  along with this program.
30
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
31
 */
32
33
namespace Smalot\PdfParser\Element;
34
35
use Smalot\PdfParser\Document;
36
use Smalot\PdfParser\Element;
37
use Smalot\PdfParser\Header;
38
use Smalot\PdfParser\PDFObject;
39
40
/**
41
 * Class ElementArray
42
 */
43
class ElementArray extends Element
44
{
45 75
    public function __construct($value, ?Document $document = null)
46
    {
47 75
        parent::__construct($value, $document);
48
    }
49
50 70
    public function getContent()
51
    {
52 70
        foreach ($this->value as $name => $element) {
53 70
            $this->resolveXRef($name);
54
        }
55
56 70
        return parent::getContent();
57
    }
58
59 14
    public function getRawContent(): array
60
    {
61 14
        return $this->value;
62
    }
63
64 3
    public function getDetails(bool $deep = true): array
65
    {
66 3
        $values = [];
67 3
        $elements = $this->getContent();
68
69 3
        foreach ($elements as $key => $element) {
70 3
            if ($element instanceof Header && $deep) {
71 1
                $values[$key] = $element->getDetails($deep);
72 3
            } elseif ($element instanceof PDFObject && $deep) {
73
                $values[$key] = $element->getDetails(false);
74 3
            } elseif ($element instanceof self) {
75 1
                if ($deep) {
76 1
                    $values[$key] = $element->getDetails();
77
                }
78 3
            } elseif ($element instanceof Element && !($element instanceof self)) {
79 3
                $values[$key] = $element->getContent();
80
            }
81
        }
82
83 3
        return $values;
84
    }
85
86 1
    public function __toString(): string
87
    {
88 1
        return implode(',', $this->value);
89
    }
90
91
    /**
92
     * @return Element|PDFObject
93
     */
94 70
    protected function resolveXRef(string $name)
95
    {
96 70
        if (($obj = $this->value[$name]) instanceof ElementXRef) {
97
            /** @var ElementXRef $obj */
98 67
            $obj = $this->document->getObjectById($obj->getId());
0 ignored issues
show
Bug introduced by
The method getObjectById() does not exist on null. ( Ignorable by Annotation )

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

98
            /** @scrutinizer ignore-call */ 
99
            $obj = $this->document->getObjectById($obj->getId());

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...
99 67
            $this->value[$name] = $obj;
100
        }
101
102 70
        return $this->value[$name];
103
    }
104
105
    /**
106
     * @todo: These methods return mixed and mismatched types throughout the hierarchy
107
     *
108
     * @return bool|ElementArray
109
     */
110 24
    public static function parse(string $content, ?Document $document = null, int &$offset = 0)
111
    {
112 24
        if (preg_match('/^\s*\[(?P<array>.*)/is', $content, $match)) {
113 22
            preg_match_all('/(.*?)(\[|\])/s', trim($content), $matches);
114
115 22
            $level = 0;
116 22
            $sub = '';
117 22
            foreach ($matches[0] as $part) {
118 22
                $sub .= $part;
119 22
                $level += (false !== strpos($part, '[') ? 1 : -1);
120 22
                if ($level <= 0) {
121 22
                    break;
122
                }
123
            }
124
125
            // Removes 1 level [ and ].
126 22
            $sub = substr(trim($sub), 1, -1);
127 22
            $sub_offset = 0;
128 22
            $values = Element::parse($sub, $document, $sub_offset, true);
129
130 22
            $offset += strpos($content, '[') + 1;
131
            // Find next ']' position
132 22
            $offset += \strlen($sub) + 1;
133
134 22
            return new self($values, $document);
135
        }
136
137 9
        return false;
138
    }
139
}
140