Passed
Push — fix/encoding-better-tostring-h... ( 2ee0a6 )
by Konrad
03:47
created

Encoding::getEncodingClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 Exception;
34
use Smalot\PdfParser\Element\ElementNumeric;
35
use Smalot\PdfParser\Encoding\PostScriptGlyphs;
36
37
/**
38
 * Class Encoding
39
 */
40
class Encoding extends PDFObject
41
{
42
    /**
43
     * @var array
44
     */
45
    protected $encoding;
46
47
    /**
48
     * @var array
49
     */
50
    protected $differences;
51
52
    /**
53
     * @var array
54
     */
55
    protected $mapping;
56
57 8
    public function init()
58
    {
59 8
        $this->mapping = [];
60 8
        $this->differences = [];
61 8
        $this->encoding = [];
62
63 8
        if ($this->has('BaseEncoding')) {
64 3
            $className = $this->getEncodingClass();
65 2
            $class = new $className();
66 2
            $this->encoding = $class->getTranslations();
67
68
            // Build table including differences.
69 2
            $differences = $this->get('Differences')->getContent();
70 2
            $code = 0;
71
72 2
            if (!\is_array($differences)) {
73 2
                return;
74
            }
75
76
            foreach ($differences as $difference) {
77
                /** @var ElementNumeric $difference */
78
                if ($difference instanceof ElementNumeric) {
79
                    $code = $difference->getContent();
80
                    continue;
81
                }
82
83
                // ElementName
84
                $this->differences[$code] = $difference;
85
                if (\is_object($difference)) {
86
                    $this->differences[$code] = $difference->getContent();
87
                }
88
89
                // For the next char.
90
                ++$code;
91
            }
92
93
            $this->mapping = $this->encoding;
94
            foreach ($this->differences as $code => $difference) {
95
                /* @var string $difference */
96
                $this->mapping[$code] = $difference;
97
            }
98
        }
99 5
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getDetails($deep = true)
105
    {
106
        $details = [];
107
108
        $details['BaseEncoding'] = ($this->has('BaseEncoding') ? (string) $this->get('BaseEncoding') : 'Ansi');
109
        $details['Differences'] = ($this->has('Differences') ? (string) $this->get('Differences') : '');
110
111
        $details += parent::getDetails($deep);
112
113
        return $details;
114
    }
115
116
    /**
117
     * @return int
118
     */
119 3
    public function translateChar($dec)
120
    {
121 3
        if (isset($this->mapping[$dec])) {
122
            $dec = $this->mapping[$dec];
123
        }
124
125 3
        return PostScriptGlyphs::getCodePoint($dec);
126
    }
127
128
    /**
129
     * Returns the name of the encoding class, if available.
130
     *
131
     * @return string
132
     *
133
     * @throws \Exception On PHP 7.4 an exception is thrown if encoding class doesn't exist.
134
     */
135 3
    public function __toString()
136
    {
137
        try {
138 3
            return $this->getEncodingClass();
139 1
        } catch (Exception $e) {
140
            // prior to PHP 7.4 toString has to return an empty string.
141 1
            if (version_compare(PHP_VERSION, '7.4.0', '<')) {
142
                return '';
143
            } else {
144 1
                throw $e;
145
            }
146
        }
147
    }
148
149
    /**
150
     * @return string
151
     *
152
     * @throws \Exception
153
     */
154 4
    protected function getEncodingClass()
155
    {
156
        // Load reference table charset.
157 4
        $baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent());
158 4
        $className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding;
159
160 4
        if (!class_exists($className)) {
161 2
            throw new Exception('Missing encoding data for: "'.$baseEncoding.'".');
162
        }
163
164 2
        return $className;
165
    }
166
}
167