Projekt

Obecné

Profil

Stáhnout (3.91 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 67ebe0dc Petr Lukašík
            if (strlen($value) == 0) {
32 ce045025 Petr Lukašík
                $nextBook = true;
33 67ebe0dc Petr Lukašík
            } elseif ($value[0] == '|' && $nextBook) {
34 2d082bc2 Petr Lukašík
                $currentBook = substr($value, 1);
35
                $currentChapter = "";
36
                $currentSurface = $this->transformAbbrev("obverse");
37 ce045025 Petr Lukašík
                $nextBook = false;
38 67ebe0dc Petr Lukašík
            } elseif ($value[0] == '|') {
39
                $currentChapter = substr($value, 1);
40
                $currentSurface = $this->transformAbbrev("obverse");
41
            } else {
42 2d082bc2 Petr Lukašík
                if (strchr($value, '$')) {
43 67ebe0dc Petr Lukašík
                    $currentSurface = $this->transformAbbrev(substr(strchr($value, "&"), 1, -1));
44
                    $output[$currentBook][$currentChapter][$currentSurface][] = trim(substr($value, 0, stripos($value, "&")));
45
                } else {
46 2d082bc2 Petr Lukašík
                    $output[$currentBook][$currentChapter][$currentSurface][] = $value;
47
                }
48
            }
49
        }
50
51
        return $output;
52
    }
53
54 ce045025 Petr Lukašík
    /** Nejvíc kontrola souboru, kontroluje zda soubor existuje a zda aspoň začátek souboru vyhovuje formátu */
55 2d082bc2 Petr Lukašík
    public function isFileParsable()
56
    {
57
        if (file_exists($this->textFile)) {
58
            $checker = fopen($this->textFile, "r");
59
            if (fgets($checker)[0] == '|') {
60
                return true;
61
            } else {
62
                return false;
63
            }
64
        } else {
65
            return false;
66
        }
67
    }
68
69
    public function setTypes($objects, $surfaces)
70
    {
71
        $this->objectTypes = $objects;
72
        $this->surfaceTypes = $surfaces;
73
    }
74
75 ce045025 Petr Lukašík
    /** Funkce pro transformaci zkratek surfaceType na jejich příslušná idčka v db */
76 67ebe0dc Petr Lukašík
    private function transformAbbrev($abbrev)
77
    {
78
        switch ($abbrev) {
79 2d082bc2 Petr Lukašík
            case "rev.":
80
                return array_search("reverse", $this->surfaceTypes);
81
            case "u.e.":
82
                return array_search("upper edge", $this->surfaceTypes);
83
            case "lo.e.":
84
                return array_search("lower edge", $this->surfaceTypes);
85
            case "le.e.":
86
                return array_search("left edge", $this->surfaceTypes);
87
            // Domnenky, v test souborech se nenachazi -> udelat podle toho export
88
            case "r.e.":
89
                return array_search("right edge", $this->surfaceTypes);
90
            case "s.i.1":
91
                return array_search("seal impression 1", $this->surfaceTypes);
92
            case "s.i.2":
93
                return array_search("seal impression 2", $this->surfaceTypes);
94
            case "s.i.3":
95
                return array_search("seal impression 3", $this->surfaceTypes);
96
            case "s.i.4":
97
                return array_search("seal impression 4", $this->surfaceTypes);
98
            case "s.i.5":
99
                return array_search("seal impression 5", $this->surfaceTypes);
100
            case "s.i.6":
101
                return array_search("seal impression 6", $this->surfaceTypes);
102
            case "s.i.7":
103
                return array_search("seal impression 7", $this->surfaceTypes);
104
            case "s.i.8":
105
                return array_search("seal impression 8", $this->surfaceTypes);
106
            default:
107
                return array_search("obverse", $this->surfaceTypes);
108
        }
109
    }
110 ce045025 Petr Lukašík
111 67ebe0dc Petr Lukašík
    public function getTextFile()
112
    {
113 ce045025 Petr Lukašík
        return $this->textFile;
114
    }
115 2d082bc2 Petr Lukašík
}