Completed
Pull Request — master (#349)
by
unknown
04:33 queued 02:38
created

Element::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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\ElementBoolean;
35
use Smalot\PdfParser\Element\ElementDate;
36
use Smalot\PdfParser\Element\ElementHexa;
37
use Smalot\PdfParser\Element\ElementName;
38
use Smalot\PdfParser\Element\ElementNull;
39
use Smalot\PdfParser\Element\ElementNumeric;
40
use Smalot\PdfParser\Element\ElementString;
41
use Smalot\PdfParser\Element\ElementStruct;
42
use Smalot\PdfParser\Element\ElementXRef;
43
44
/**
45
 * Class Element
46
 */
47
class Element
48
{
49
    /**
50
     * @var Document
51
     */
52
    protected $document = null;
53
54
    protected $value = null;
55
56
    /**
57
     * @param Document $document
58
     */
59 86
    public function __construct($value, Document $document = null)
60
    {
61 86
        $this->value = $value;
62 86
        $this->document = $document;
63 86
    }
64
65 30
    public function init()
66
    {
67 30
    }
68
69
    /**
70
     * @return bool
71
     */
72 7
    public function equals($value)
73
    {
74 7
        return $value == $this->value;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 11
    public function contains($value)
81
    {
82 11
        if (\is_array($this->value)) {
83
            /** @var Element $val */
84 4
            foreach ($this->value as $val) {
85 4
                if ($val->equals($value)) {
86 4
                    return true;
87
                }
88
            }
89
90 3
            return false;
91
        }
92
93 7
        return $this->equals($value);
94
    }
95
96 57
    public function getContent()
97
    {
98 57
        return $this->value;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 37
    public function __toString()
105
    {
106 37
        return (string) ($this->value);
107
    }
108
109
    /**
110
     * @param string   $content
111
     * @param Document $document
112
     * @param int      $position
113
     *
114
     * @return array
115
     *
116
     * @throws \Exception
117
     */
118 21
    public static function parse($content, Document $document = null, &$position = 0)
119
    {
120 21
        $args = \func_get_args();
121 21
        $only_values = isset($args[3]) ? $args[3] : false;
122 21
        $content = trim($content);
123 21
        $values = [];
124
125
        do {
126 21
            $old_position = $position;
127
128 21
            if (!$only_values) {
129 19
                if (!preg_match('/^\s*(?P<name>\/[A-Z0-9\._]+)(?P<value>.*)/si', substr($content, $position), $match)) {
130 2
                    break;
131
                } else {
132 19
                    $name = ltrim($match['name'], '/');
133 19
                    $value = $match['value'];
134 19
                    $position = strpos($content, $value, $position + \strlen($match['name']));
135
                }
136
            } else {
137 11
                $name = \count($values);
138 11
                $value = substr($content, $position);
139
            }
140
141 21
            if ($element = ElementName::parse($value, $document, $position)) {
142 19
                $values[$name] = $element;
143 15
            } elseif ($element = ElementXRef::parse($value, $document, $position)) {
144 12
                $values[$name] = $element;
145 11
            } elseif ($element = ElementNumeric::parse($value, $document, $position)) {
146 9
                $values[$name] = $element;
147 9
            } elseif ($element = ElementStruct::parse($value, $document, $position)) {
148 7
                $values[$name] = $element;
149 9
            } elseif ($element = ElementBoolean::parse($value, $document, $position)) {
150 2
                $values[$name] = $element;
151 9
            } elseif ($element = ElementNull::parse($value, $document, $position)) {
152 1
                $values[$name] = $element;
153 9
            } elseif ($element = ElementDate::parse($value, $document, $position)) {
154 4
                $values[$name] = $element;
155 9
            } elseif ($element = ElementString::parse($value, $document, $position)) {
156 7
                $values[$name] = $element;
157 8
            } elseif ($element = ElementHexa::parse($value, $document, $position)) {
158 1
                $values[$name] = $element;
159 8
            } elseif ($element = ElementArray::parse($value, $document, $position)) {
160 8
                $values[$name] = $element;
161
            } else {
162 3
                $position = $old_position;
163 3
                break;
164
            }
165 21
        } while ($position < \strlen($content));
166
167 21
        return $values;
168
    }
169
}
170