Passed
Pull Request — master (#383)
by Konrad
02:43
created

ElementXRef::__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\Element;
32
33
use Smalot\PdfParser\Document;
34
use Smalot\PdfParser\Element;
35
36
/**
37
 * Class ElementXRef
38
 */
39
class ElementXRef extends Element
40
{
41
    /**
42
     * @return string
43
     */
44 35
    public function getId()
45
    {
46 35
        return $this->getContent();
47
    }
48
49
    public function getObject()
50
    {
51
        return $this->document->getObjectById($this->getId());
52
    }
53
54
    /**
55
     * @return bool
56
     */
57 3
    public function equals($value)
58
    {
59
        /**
60
         * In case $value is a number and $this->value is a string like 5_0
61
         *
62
         * Without this if-clause code like:
63
         *
64
         *      $element = new ElementXRef('5_0');
65
         *      $this->assertTrue($element->equals(5));
66
         *
67
         * would fail (= 5_0 and 5 are not equal in PHP 8.0+).
68
         */
69
        if (
70 3
            true === is_numeric($value)
71 3
            && true === \is_string($this->getContent())
72 3
            && 1 === preg_match('/[0-9]+\_[0-9]+/', $this->getContent(), $matches)
73
        ) {
74 3
            return (float) ($this->getContent()) == $value;
75
        }
76
77 2
        $id = ($value instanceof self) ? $value->getId() : $value;
78
79 2
        return $this->getId() == $id;
80
    }
81
82
    /**
83
     * @return string
84
     */
85 1
    public function __toString()
86
    {
87 1
        return '#Obj#'.$this->getId();
88
    }
89
90
    /**
91
     * @param string   $content
92
     * @param Document $document
93
     * @param int      $offset
94
     *
95
     * @return bool|ElementXRef
96
     */
97 17
    public static function parse($content, Document $document = null, &$offset = 0)
98
    {
99 17
        if (preg_match('/^\s*(?P<id>[0-9]+\s+[0-9]+\s+R)/s', $content, $match)) {
100 14
            $id = $match['id'];
101 14
            $offset += strpos($content, $id) + \strlen($id);
102 14
            $id = str_replace(' ', '_', rtrim($id, ' R'));
103
104 14
            return new self($id, $document);
105
        }
106
107 13
        return false;
108
    }
109
}
110