Test Failed
Pull Request — master (#614)
by
unknown
02:15
created

ElementArray   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 97.87%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 95
ccs 46
cts 47
cp 0.9787
rs 10
wmc 22
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 49
    public function __construct($value, Document $document = null)
46
    {
47 49
        parent::__construct($value, $document);
48 49
    }
49
50 44
    public function getContent()
51
    {
52 44
        foreach ($this->value as $name => $element) {
53 44
            $this->resolveXRef($name);
54
        }
55
56 44
        return parent::getContent();
57
    }
58
59 10
    public function getRawContent(): array
60
    {
61 10
        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 44
    protected function resolveXRef(string $name)
95
    {
96 44
        if (($obj = $this->value[$name]) instanceof ElementXRef) {
97
            /** @var ElementXRef $obj */
98 42
            $obj = $this->document->getObjectById($obj->getId());
99 42
            $this->value[$name] = $obj;
100
        }
101
102 44
        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 20
    public static function parse(string $content, Document $document = null, int &$offset = 0)
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_VARIABLE, expecting T_STRING or T_NAME_QUALIFIED or T_NAME_FULLY_QUALIFIED or T_NAME_RELATIVE on line 110 at column 82
Loading history...
111
    {
112 20
        if (preg_match('/^\s*\[(?P<array>.*)/is', $content, $match)) {
113 18
            preg_match_all('/(.*?)(\[|\])/s', trim($content), $matches);
114
115 18
            $level = 0;
116 18
            $sub = '';
117 18
            foreach ($matches[0] as $part) {
118 18
                $sub .= $part;
119 18
                $level += (false !== strpos($part, '[') ? 1 : -1);
120 18
                if ($level <= 0) {
121 18
                    break;
122
                }
123
            }
124
125
            // Removes 1 level [ and ].
126 18
            $sub = substr(trim($sub), 1, -1);
127 18
            $sub_offset = 0;
128 18
            $values = Element::parse($sub, $document, $sub_offset, true);
129
130 18
            $offset += strpos($content, '[') + 1;
131
            // Find next ']' position
132 18
            $offset += \strlen($sub) + 1;
133
134 18
            return new self($values, $document);
135
        }
136
137 7
        return false;
138
    }
139
}
140