ElementHexa   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 50
ccs 23
cts 23
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 17 3
A decode() 0 26 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
 *
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\Element;
34
35
use Smalot\PdfParser\Document;
36
37
/**
38
 * Class ElementHexa
39
 */
40
class ElementHexa extends ElementString
41
{
42
    /**
43
     * @return bool|ElementHexa|ElementDate
44
     */
45 21
    public static function parse(string $content, ?Document $document = null, int &$offset = 0)
46
    {
47 21
        if (preg_match('/^\s*\<(?P<name>[A-F0-9]+)\>/is', $content, $match)) {
48 3
            $name = $match['name'];
49 3
            $offset += strpos($content, '<'.$name) + \strlen($name) + 2; // 1 for '>'
50
            // repackage string as standard
51 3
            $name = '('.self::decode($name).')';
52 3
            $element = ElementDate::parse($name, $document);
53
54 3
            if (!$element) {
55 3
                $element = ElementString::parse($name, $document);
56
            }
57
58 3
            return $element;
59
        }
60
61 21
        return false;
62
    }
63
64 33
    public static function decode(string $value): string
65
    {
66 33
        $text = '';
67
68
        // Filter $value of non-hexadecimal characters
69 33
        $value = (string) preg_replace('/[^0-9a-f]/i', '', $value);
70
71
        // Check for leading zeros (4-byte hexadecimal indicator), or
72
        // the BE BOM
73 33
        if ('00' === substr($value, 0, 2) || 'feff' === strtolower(substr($value, 0, 4))) {
74 13
            $value = (string) preg_replace('/^feff/i', '', $value);
75 13
            for ($i = 0, $length = \strlen($value); $i < $length; $i += 4) {
76 13
                $hex = substr($value, $i, 4);
77 13
                $text .= '&#'.str_pad(hexdec($hex), 4, '0', \STR_PAD_LEFT).';';
78
            }
79
        } else {
80
            // Otherwise decode this as 2-byte hexadecimal
81 27
            for ($i = 0, $length = \strlen($value); $i < $length; $i += 2) {
82 25
                $hex = substr($value, $i, 2);
83 25
                $text .= \chr(hexdec($hex));
0 ignored issues
show
Bug introduced by
It seems like hexdec($hex) can also be of type double; however, parameter $codepoint of chr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

83
                $text .= \chr(/** @scrutinizer ignore-type */ hexdec($hex));
Loading history...
84
            }
85
        }
86
87 33
        $text = html_entity_decode($text, \ENT_NOQUOTES, 'UTF-8');
88
89 33
        return $text;
90
    }
91
}
92