Completed
Push — master ( fb47da...fedb3d )
by WEBEWEB
01:20
created

DerDeserializer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 121
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deserializeDocument() 0 28 5
A deserializePage() 0 16 2
A deserializeWord() 0 18 2
A splitHeader() 0 22 4
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2020 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\ThirdParty\OcrLad\Serializer;
13
14
use WBW\Library\Core\ThirdParty\OcrLad\Model\Document;
15
use WBW\Library\Core\ThirdParty\OcrLad\Model\Page;
16
use WBW\Library\Core\ThirdParty\OcrLad\Model\Word;
17
18
/**
19
 * DER deserializer.
20
 *
21
 * @author webeweb <https://github.com/webeweb>
22
 * @package WBW\Library\Core\ThirdParty\OcrLad\Serializer
23
 */
24
class DerDeserializer {
25
26
    /**
27
     * DER delimiter.
28
     *
29
     * @var string
30
     */
31
    const DER_DELIMITER = ";";
32
33
    /**
34
     * Deserialize a document.
35
     *
36
     * @param string $filename The filename.
37
     * @return Document|null Returns the document in case of success, null otherwise.
38
     */
39
    public static function deserializeDocument($filename) {
40
41
        $model = new Document();
42
        $model->setFilename($filename);
43
44
        $stream = fopen($filename, "r");
45
46
        $headers = DerDeserializer::splitHeader(fgets($stream));
47
        foreach ($headers as $current) {
48
49
            $page = DerDeserializer::deserializePage($current);
50
            if (null !== $page) {
51
                $model->addPage($page);
52
            }
53
        }
54
55
        while (true !== feof($stream)) {
56
57
            $word = DerDeserializer::deserializeWord(fgets($stream));
58
            if (null !== $word) {
59
                $model->addWord($word);
60
            }
61
        }
62
63
        fclose($stream);
64
65
        return $model;
66
    }
67
68
    /**
69
     * Deserialize a page.
70
     *
71
     * @param string $rawData The raw data.
72
     * @return Page|null Returns the page in case of success, null otherwise.
73
     */
74
    protected static function deserializePage($rawData) {
75
76
        $data = explode(DerDeserializer::DER_DELIMITER, $rawData);
77
        if (6 !== count($data)) {
78
            return null;
79
        }
80
81
        $model = new Page();
82
        $model->setResolution(intval(preg_replace("/[^0-9]/", "", $data[0])));
83
        $model->setWidth(intval($data[1]));
84
        $model->setHeight(intval($data[2]));
85
        $model->setRotation(intval($data[3]));
86
        $model->setTag(intval($data[4]));
87
88
        return $model;
89
    }
90
91
    /**
92
     * Deserialize a word.
93
     *
94
     * @param string $rawData The raw data.
95
     * @return Word|null Returns the word in case of success, null otherwise.
96
     */
97
    protected static function deserializeWord($rawData) {
98
99
        $data = explode(DerDeserializer::DER_DELIMITER, $rawData);
100
        if (7 !== count($data)) {
101
            return null;
102
        }
103
104
        $model = new Word();
105
        $model->setContent(trim($data[0]));
106
        $model->setType(trim($data[1]));
107
        $model->setOcrConfidence(floatval(str_replace(",", ".", $data[2])));
108
        $model->setX1(floatval($data[3]));
109
        $model->setY1(floatval($data[4]));
110
        $model->setX2(floatval($data[5]));
111
        $model->setY2(floatval($data[6]));
112
113
        return $model;
114
    }
115
116
    /**
117
     * Split an header.
118
     *
119
     * @param string $rawData The raw data.
120
     * @return string[] Returns the headers.
121
     */
122
    protected static function splitHeader($rawData) {
123
124
        $data = explode(DerDeserializer::DER_DELIMITER, $rawData);
125
        if (6 === count($data)) {
126
            return [$rawData];
127
        }
128
129
        $rows   = [];
130
        $buffer = [];
131
132
        foreach ($data as $current) {
133
134
            $buffer[] = $current;
135
            if (6 === count($buffer)) {
136
137
                $rows[] = implode(DerDeserializer::DER_DELIMITER, $buffer);
138
                $buffer = [];
139
            }
140
        }
141
142
        return $rows;
143
    }
144
}