1
|
<?php
|
2
|
|
3
|
|
4
|
namespace App\Utils;
|
5
|
|
6
|
class TextParser
|
7
|
{
|
8
|
private $textFile;
|
9
|
private $objectTypes;
|
10
|
private $surfaceTypes;
|
11
|
|
12
|
public function __construct($file)
|
13
|
{
|
14
|
$this->textFile = $file;
|
15
|
}
|
16
|
|
17
|
/** Rozparsuje importovaný text */
|
18
|
public function parseText()
|
19
|
{
|
20
|
$array = explode("\n", file_get_contents($this->textFile));
|
21
|
$array = array_map('trim', $array);
|
22
|
|
23
|
$nextBook = true;
|
24
|
$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
|
if (strlen($value) == 0) {
|
32
|
$nextBook = true;
|
33
|
} elseif ($value[0] == '|' && $nextBook) {
|
34
|
$currentBook = substr($value, 1);
|
35
|
$currentChapter = "";
|
36
|
$currentSurface = $this->transformAbbrev("obverse");
|
37
|
$nextBook = false;
|
38
|
} elseif ($value[0] == '|') {
|
39
|
$currentChapter = substr($value, 1);
|
40
|
$currentSurface = $this->transformAbbrev("obverse");
|
41
|
} else {
|
42
|
if (strchr($value, '$')) {
|
43
|
$currentSurface = $this->transformAbbrev(substr(strchr($value, "&"), 1, -1));
|
44
|
$output[$currentBook][$currentChapter][$currentSurface][] = trim(substr($value, 0, stripos($value, "&")));
|
45
|
} else {
|
46
|
$output[$currentBook][$currentChapter][$currentSurface][] = $value;
|
47
|
}
|
48
|
}
|
49
|
}
|
50
|
|
51
|
return $output;
|
52
|
}
|
53
|
|
54
|
/** Nejvíc kontrola souboru, kontroluje zda soubor existuje a zda aspoň začátek souboru vyhovuje formátu */
|
55
|
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
|
/** Funkce pro transformaci zkratek surfaceType na jejich příslušná idčka v db */
|
76
|
private function transformAbbrev($abbrev)
|
77
|
{
|
78
|
switch ($abbrev) {
|
79
|
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
|
|
111
|
public function getTextFile()
|
112
|
{
|
113
|
return $this->textFile;
|
114
|
}
|
115
|
}
|