Passed
Push — php8 ( 0b5f59 )
by Konrad
02:33
created

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