Passed
Pull Request — master (#318)
by
unknown
04:18
created

ElementArray   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 114
rs 10
ccs 47
cts 47
cp 1
wmc 22

7 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveXRef() 0 9 2
A parse() 0 28 5
A getRawContent() 0 3 1
A __toString() 0 3 1
A getContent() 0 7 2
A __construct() 0 3 1
B getDetails() 0 20 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\Element;
32
33
use Smalot\PdfParser\Document;
34
use Smalot\PdfParser\Element;
35
use Smalot\PdfParser\Header;
36
use Smalot\PdfParser\PDFObject;
37
38
/**
39
 * Class ElementArray
40
 */
41
class ElementArray extends Element
42
{
43
    /**
44
     * @param string   $value
45
     * @param Document $document
46
     */
47 29
    public function __construct($value, Document $document = null)
48
    {
49 29
        parent::__construct($value, $document);
50 29
    }
51
52 24
    public function getContent()
53
    {
54 24
        foreach ($this->value as $name => $element) {
55 24
            $this->resolveXRef($name);
56
        }
57
58 24
        return parent::getContent();
59
    }
60
61
    /**
62
     * @return array
63
     */
64 3
    public function getRawContent()
65
    {
66 3
        return $this->value;
67
    }
68
69
    /**
70
     * @param bool $deep
71
     *
72
     * @return array
73
     */
74 8
    public function getDetails($deep = true)
75
    {
76 8
        $values = [];
77 8
        $elements = $this->getContent();
78
79 8
        foreach ($elements as $key => $element) {
80 8
            if ($element instanceof Header && $deep) {
81 1
                $values[$key] = $element->getDetails($deep);
82 8
            } elseif ($element instanceof PDFObject && $deep) {
83 3
                $values[$key] = $element->getDetails(false);
84 5
            } elseif ($element instanceof self) {
85 1
                if ($deep) {
86 1
                    $values[$key] = $element->getDetails();
87
                }
88 5
            } elseif ($element instanceof Element && !($element instanceof self)) {
89 5
                $values[$key] = $element->getContent();
90
            }
91
        }
92
93 8
        return $values;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 3
    public function __toString()
100
    {
101 3
        return implode(',', $this->value);
102
    }
103
104
    /**
105
     * @param string $name
106
     *
107
     * @return Element|PDFObject
108
     */
109 24
    protected function resolveXRef($name)
110
    {
111 24
        if (($obj = $this->value[$name]) instanceof ElementXRef) {
112
            /** @var PDFObject $obj */
113 22
            $obj = $this->document->getObjectById($obj->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Smalot\PdfParser\PDFObject. Did you maybe mean get()? ( Ignorable by Annotation )

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

113
            $obj = $this->document->getObjectById($obj->/** @scrutinizer ignore-call */ 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...
114 22
            $this->value[$name] = $obj;
115
        }
116
117 24
        return $this->value[$name];
118
    }
119
120
    /**
121
     * @param string   $content
122
     * @param Document $document
123
     * @param int      $offset
124
     *
125
     * @return bool|ElementArray
126
     */
127 11
    public static function parse($content, Document $document = null, &$offset = 0)
128
    {
129 11
        if (preg_match('/^\s*\[(?P<array>.*)/is', $content, $match)) {
130 11
            preg_match_all('/(.*?)(\[|\])/s', trim($content), $matches);
131
132 11
            $level = 0;
133 11
            $sub = '';
134 11
            foreach ($matches[0] as $part) {
135 11
                $sub .= $part;
136 11
                $level += (false !== strpos($part, '[') ? 1 : -1);
137 11
                if ($level <= 0) {
138 11
                    break;
139
                }
140
            }
141
142
            // Removes 1 level [ and ].
143 11
            $sub = substr(trim($sub), 1, -1);
144 11
            $sub_offset = 0;
145 11
            $values = Element::parse($sub, $document, $sub_offset, true);
146
147 11
            $offset += strpos($content, '[') + 1;
148
            // Find next ']' position
149 11
            $offset += \strlen($sub) + 1;
150
151 11
            return new self($values, $document);
0 ignored issues
show
Bug introduced by
$values of type array is incompatible with the type string expected by parameter $value of Smalot\PdfParser\Element...entArray::__construct(). ( Ignorable by Annotation )

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

151
            return new self(/** @scrutinizer ignore-type */ $values, $document);
Loading history...
152
        }
153
154 5
        return false;
155
    }
156
}
157