Passed
Push — master ( ce6aa7...8d948f )
by Konrad
02:45
created

ElementXRef   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 10
ccs 18
cts 20
cp 0.9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A getObject() 0 3 1
A parse() 0 11 2
A __toString() 0 3 1
A equals() 0 23 5
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 38
    public function getId()
45
    {
46 38
        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 18
    public static function parse($content, Document $document = null, &$offset = 0)
98
    {
99 18
        if (preg_match('/^\s*(?P<id>[0-9]+\s+[0-9]+\s+R)/s', $content, $match)) {
100 15
            $id = $match['id'];
101 15
            $offset += strpos($content, $id) + \strlen($id);
102 15
            $id = str_replace(' ', '_', rtrim($id, ' R'));
103
104 15
            return new self($id, $document);
105
        }
106
107 14
        return false;
108
    }
109
}
110