Issues (82)

src/Smalot/PdfParser/Element.php (1 issue)

Labels
Severity
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;
34
35
use Smalot\PdfParser\Element\ElementArray;
36
use Smalot\PdfParser\Element\ElementBoolean;
37
use Smalot\PdfParser\Element\ElementDate;
38
use Smalot\PdfParser\Element\ElementHexa;
39
use Smalot\PdfParser\Element\ElementName;
40
use Smalot\PdfParser\Element\ElementNull;
41
use Smalot\PdfParser\Element\ElementNumeric;
42
use Smalot\PdfParser\Element\ElementString;
43
use Smalot\PdfParser\Element\ElementStruct;
44
use Smalot\PdfParser\Element\ElementXRef;
45
46
/**
47
 * Class Element
48
 */
49
class Element
50
{
51
    /**
52
     * @var Document|null
53
     */
54
    protected $document;
55
56
    protected $value;
57
58 138
    public function __construct($value, ?Document $document = null)
59
    {
60 138
        $this->value = $value;
61 138
        $this->document = $document;
62
    }
63
64 74
    public function init()
65
    {
66 74
    }
67
68 7
    public function equals($value): bool
69
    {
70 7
        return $value == $this->value;
71
    }
72
73 11
    public function contains($value): bool
74
    {
75 11
        if (\is_array($this->value)) {
76
            /** @var Element $val */
77 4
            foreach ($this->value as $val) {
78 4
                if ($val->equals($value)) {
79 4
                    return true;
80
                }
81
            }
82
83 3
            return false;
84
        }
85
86 7
        return $this->equals($value);
87
    }
88
89 106
    public function getContent()
90
    {
91 106
        return $this->value;
92
    }
93
94 70
    public function __toString(): string
95
    {
96 70
        return (string) $this->value;
97
    }
98
99 32
    public static function parse(string $content, ?Document $document = null, int &$position = 0)
100
    {
101 32
        $args = \func_get_args();
102 32
        $only_values = isset($args[3]) ? $args[3] : false;
103 32
        $content = trim($content);
104 32
        $values = [];
105
106
        do {
107 32
            $old_position = $position;
108
109 32
            if (!$only_values) {
110 30
                if (!preg_match('/\G\s*(?P<name>\/[A-Z#0-9\._]+)(?P<value>.*)/si', $content, $match, 0, $position)) {
111 3
                    break;
112
                } else {
113 30
                    $name = preg_replace_callback(
114 30
                        '/#([0-9a-f]{2})/i',
115 30
                        function ($m) {
116 1
                            return \chr(base_convert($m[1], 16, 10));
0 ignored issues
show
base_convert($m[1], 16, 10) of type string is incompatible with the type integer expected by parameter $codepoint of chr(). ( Ignorable by Annotation )

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

116
                            return \chr(/** @scrutinizer ignore-type */ base_convert($m[1], 16, 10));
Loading history...
117 30
                        },
118 30
                        ltrim($match['name'], '/')
119 30
                    );
120 30
                    $value = $match['value'];
121 30
                    $position = strpos($content, $value, $position + \strlen($match['name']));
122
                }
123
            } else {
124 22
                $name = \count($values);
125 22
                $value = substr($content, $position);
126
            }
127
128 32
            if ($element = ElementName::parse($value, $document, $position)) {
129 30
                $values[$name] = $element;
130 26
            } elseif ($element = ElementXRef::parse($value, $document, $position)) {
131 23
                $values[$name] = $element;
132 23
            } elseif ($element = ElementNumeric::parse($value, $document, $position)) {
133 20
                $values[$name] = $element;
134 21
            } elseif ($element = ElementStruct::parse($value, $document, $position)) {
135 14
                $values[$name] = $element;
136 21
            } elseif ($element = ElementBoolean::parse($value, $document, $position)) {
137 3
                $values[$name] = $element;
138 21
            } elseif ($element = ElementNull::parse($value, $document, $position)) {
139 4
                $values[$name] = $element;
140 21
            } elseif ($element = ElementDate::parse($value, $document, $position)) {
141 13
                $values[$name] = $element;
142 21
            } elseif ($element = ElementString::parse($value, $document, $position)) {
143 18
                $values[$name] = $element;
144 20
            } elseif ($element = ElementHexa::parse($value, $document, $position)) {
145 2
                $values[$name] = $element;
146 20
            } elseif ($element = ElementArray::parse($value, $document, $position)) {
147 19
                $values[$name] = $element;
148
            } else {
149 5
                $position = $old_position;
150 5
                break;
151
            }
152 32
        } while ($position < \strlen($content));
153
154 32
        return $values;
155
    }
156
}
157