Passed
Push — master ( 5d3746...a4bb6d )
by Konrad
02:09
created

Encoding::__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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
     * @return string
130
     *
131
     * @throws \Exception
132
     */
133 2
    public function __toString()
134
    {
135 2
        return $this->getEncodingClass();
136
    }
137
138
    /**
139
     * @return string
140
     *
141
     * @throws \Exception
142
     */
143 3
    protected function getEncodingClass()
144
    {
145
        // Load reference table charset.
146 3
        $baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent());
147 3
        $className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding;
148
149 3
        if (!class_exists($className)) {
150 1
            throw new Exception('Missing encoding data for: "'.$baseEncoding.'".');
151
        }
152
153 2
        return $className;
154
    }
155
}
156