Projekt

Obecné

Profil

Stáhnout (4.42 KB) Statistiky
| Větev: | Tag: | Revize:
1 2d082bc2 Petr Lukašík
<?php
2
3
4
namespace App\Utils;
5
6
class TextParser
7
{
8 ce045025 Petr Lukašík
    private $textFile;
9 2d082bc2 Petr Lukašík
    private $objectTypes;
10
    private $surfaceTypes;
11
12
    public function __construct($file)
13
    {
14
        $this->textFile = $file;
15
    }
16
17 ce045025 Petr Lukašík
    /** Rozparsuje importovaný text */
18 2d082bc2 Petr Lukašík
    public function parseText()
19
    {
20
        $array = explode("\n", file_get_contents($this->textFile));
21
        $array = array_map('trim', $array);
22
23 ce045025 Petr Lukašík
        $nextBook = true;
24 2d082bc2 Petr Lukašík
        $output = array();
25
        $currentBook = "";
26
        $currentChapter = "";
27
        $currentSurface = $this->transformAbbrev("obverse");
28
29
        //TODO kontrola a parsovani object types (tablet, envelope)
30
        foreach ($array as $value) {
31 ce045025 Petr Lukašík
            Debugger::barDump($value);
32
            if (strlen($value)==0){
33
                $nextBook = true;
34
            }
35
            elseif ($value[0] == '|' && $nextBook) {
36 2d082bc2 Petr Lukašík
                $currentBook = substr($value, 1);
37
                $currentChapter = "";
38
                $currentSurface = $this->transformAbbrev("obverse");
39 ce045025 Petr Lukašík
                $nextBook = false;
40 2d082bc2 Petr Lukašík
            }
41 ce045025 Petr Lukašík
            elseif ($value[0] == '|'){
42
                // TYPÍIIIIIČO ono to může být i tady... k posrání. Odděleno '-', jakože pro jistotu demence (OBTR => "15 (T.R.4020) - le.e.")
43
                $withRef = explode(" - ", substr($value, 1));
44
                if(isset($withRef[1])){
45
                    $currentChapter = trim(substr($value, 1, strripos($value, "-")-1));
46
                    $currentSurface = $this->transformAbbrev(trim($withRef[1]));
47
                }else{
48
                    $currentChapter = substr($value, 1);
49
                    $currentSurface = $this->transformAbbrev("obverse");
50
                }
51
            }
52
            else{
53 2d082bc2 Petr Lukašík
                if (strchr($value, '$')) {
54
                    $output[$currentBook][$currentChapter][$currentSurface][] = trim(substr($value,0,stripos($value,"&")));
55
                    $currentSurface = $this->transformAbbrev(substr(strchr($value, "&"),1, -1));
56
                }else{
57
                    $output[$currentBook][$currentChapter][$currentSurface][] = $value;
58
                }
59
            }
60
        }
61
62
        return $output;
63
    }
64
65 ce045025 Petr Lukašík
    /** Nejvíc kontrola souboru, kontroluje zda soubor existuje a zda aspoň začátek souboru vyhovuje formátu */
66 2d082bc2 Petr Lukašík
    public function isFileParsable()
67
    {
68
        if (file_exists($this->textFile)) {
69
            $checker = fopen($this->textFile, "r");
70
            if (fgets($checker)[0] == '|') {
71
                return true;
72
            } else {
73
                return false;
74
            }
75
        } else {
76
            return false;
77
        }
78
    }
79
80
    public function setTypes($objects, $surfaces)
81
    {
82
        $this->objectTypes = $objects;
83
        $this->surfaceTypes = $surfaces;
84
    }
85
86 ce045025 Petr Lukašík
    /** Funkce pro transformaci zkratek surfaceType na jejich příslušná idčka v db */
87 2d082bc2 Petr Lukašík
    private function transformAbbrev($abbrev){
88
        switch ($abbrev){
89
            case "rev.":
90
                return array_search("reverse", $this->surfaceTypes);
91
            case "u.e.":
92
                return array_search("upper edge", $this->surfaceTypes);
93
            case "lo.e.":
94
                return array_search("lower edge", $this->surfaceTypes);
95
            case "le.e.":
96
                return array_search("left edge", $this->surfaceTypes);
97
            // Domnenky, v test souborech se nenachazi -> udelat podle toho export
98
            case "r.e.":
99
                return array_search("right edge", $this->surfaceTypes);
100
            case "s.i.1":
101
                return array_search("seal impression 1", $this->surfaceTypes);
102
            case "s.i.2":
103
                return array_search("seal impression 2", $this->surfaceTypes);
104
            case "s.i.3":
105
                return array_search("seal impression 3", $this->surfaceTypes);
106
            case "s.i.4":
107
                return array_search("seal impression 4", $this->surfaceTypes);
108
            case "s.i.5":
109
                return array_search("seal impression 5", $this->surfaceTypes);
110
            case "s.i.6":
111
                return array_search("seal impression 6", $this->surfaceTypes);
112
            case "s.i.7":
113
                return array_search("seal impression 7", $this->surfaceTypes);
114
            case "s.i.8":
115
                return array_search("seal impression 8", $this->surfaceTypes);
116
            default:
117
                return array_search("obverse", $this->surfaceTypes);
118
        }
119
    }
120 ce045025 Petr Lukašík
121
    public function getTextFile(){
122
        return $this->textFile;
123
    }
124 2d082bc2 Petr Lukašík
}