Passed
Pull Request — master (#318)
by
unknown
04:18
created

Encoding   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 40.54%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 37
c 3
b 1
f 0
dl 0
loc 95
rs 10
ccs 15
cts 37
cp 0.4054
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 49 9
A translateChar() 0 7 2
A getDetails() 0 10 3
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 Smalot\PdfParser\Element\ElementNumeric;
34
35
/**
36
 * Class Encoding
37
 */
38
class Encoding extends PDFObject
39
{
40
    /**
41
     * @var array
42
     */
43
    protected $encoding;
44
45
    /**
46
     * @var array
47
     */
48
    protected $differences;
49
50
    /**
51
     * @var array
52
     */
53
    protected $mapping;
54
55 3
    public function init()
56
    {
57 3
        $this->mapping = [];
58 3
        $this->differences = [];
59 3
        $this->encoding = [];
60
61 3
        if ($this->has('BaseEncoding')) {
62
            // Load reference table charset.
63
            $baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent());
64
            $className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding;
65
66
            if (!class_exists($className)) {
67
                throw new \Exception('Missing encoding data for: "'.$baseEncoding.'".');
68
            }
69
70
            $class = new $className();
71
            $this->encoding = $class->getTranslations();
72
73
            // Build table including differences.
74
            $differences = $this->get('Differences')->getContent();
75
            $code = 0;
76
77
            if (!\is_array($differences)) {
78
                return;
79
            }
80
81
            foreach ($differences as $difference) {
82
                /** @var ElementNumeric $difference */
83
                if ($difference instanceof ElementNumeric) {
84
                    $code = $difference->getContent();
85
                    continue;
86
                }
87
88
                // ElementName
89
                $this->differences[$code] = $difference;
90
                if (\is_object($difference)) {
91
                    $this->differences[$code] = $difference->getContent();
92
                }
93
94
                // For the next char.
95
                ++$code;
96
            }
97
98
            // Build final mapping (custom => standard).
99
            $table = array_flip(array_reverse($this->encoding, true));
100
101
            foreach ($this->differences as $code => $difference) {
102
                /* @var string $difference */
103
                $this->mapping[$code] = (isset($table[$difference]) ? $table[$difference] : Font::MISSING);
104
            }
105
        }
106 3
    }
107
108
    /**
109
     * @return array
110
     */
111 2
    public function getDetails($deep = true)
112
    {
113 2
        $details = [];
114
115 2
        $details['BaseEncoding'] = ($this->has('BaseEncoding') ? (string) $this->get('BaseEncoding') : 'Ansi');
116 2
        $details['Differences'] = ($this->has('Differences') ? (string) $this->get('Differences') : '');
117
118 2
        $details += parent::getDetails($deep);
119
120 2
        return $details;
121
    }
122
123
    /**
124
     * @return int
125
     */
126 2
    public function translateChar($dec)
127
    {
128 2
        if (isset($this->mapping[$dec])) {
129
            $dec = $this->mapping[$dec];
130
        }
131
132 2
        return $dec;
133
    }
134
}
135