1
|
<?php
|
2
|
|
3
|
|
4
|
namespace App\Utils;
|
5
|
|
6
|
class TextParser
|
7
|
{
|
8
|
public $textFile;
|
9
|
private $objectTypes;
|
10
|
private $surfaceTypes;
|
11
|
|
12
|
public function __construct($file)
|
13
|
{
|
14
|
$this->textFile = $file;
|
15
|
}
|
16
|
|
17
|
public function parseText()
|
18
|
{
|
19
|
$array = explode("\n", file_get_contents($this->textFile));
|
20
|
$array = array_map('trim', $array);
|
21
|
|
22
|
$output = array();
|
23
|
$currentBook = "";
|
24
|
$currentChapter = "";
|
25
|
$currentSurface = $this->transformAbbrev("obverse");
|
26
|
|
27
|
//TODO kontrola a parsovani object types (tablet, envelope)
|
28
|
foreach ($array as $value) {
|
29
|
if ($value[0] == '|' && !is_numeric($value[1])) {
|
30
|
$currentBook = substr($value, 1);
|
31
|
$currentChapter = "";
|
32
|
$currentSurface = $this->transformAbbrev("obverse");
|
33
|
}
|
34
|
elseif ($value[0] == '|' && is_numeric($value[1])){
|
35
|
$currentChapter = substr($value, 1);
|
36
|
$currentSurface = $this->transformAbbrev("obverse");
|
37
|
}else{
|
38
|
if (strchr($value, '$')) {
|
39
|
$output[$currentBook][$currentChapter][$currentSurface][] = trim(substr($value,0,stripos($value,"&")));
|
40
|
$currentSurface = $this->transformAbbrev(substr(strchr($value, "&"),1, -1));
|
41
|
}else{
|
42
|
$output[$currentBook][$currentChapter][$currentSurface][] = $value;
|
43
|
}
|
44
|
}
|
45
|
}
|
46
|
|
47
|
return $output;
|
48
|
}
|
49
|
|
50
|
/** Nejvíc kontrola souboru*/
|
51
|
public function isFileParsable()
|
52
|
{
|
53
|
if (file_exists($this->textFile)) {
|
54
|
$checker = fopen($this->textFile, "r");
|
55
|
if (fgets($checker)[0] == '|') {
|
56
|
return true;
|
57
|
} else {
|
58
|
return false;
|
59
|
}
|
60
|
} else {
|
61
|
return false;
|
62
|
}
|
63
|
}
|
64
|
|
65
|
public function setTypes($objects, $surfaces)
|
66
|
{
|
67
|
$this->objectTypes = $objects;
|
68
|
$this->surfaceTypes = $surfaces;
|
69
|
}
|
70
|
|
71
|
private function transformAbbrev($abbrev){
|
72
|
switch ($abbrev){
|
73
|
case "rev.":
|
74
|
return array_search("reverse", $this->surfaceTypes);
|
75
|
case "u.e.":
|
76
|
return array_search("upper edge", $this->surfaceTypes);
|
77
|
case "lo.e.":
|
78
|
return array_search("lower edge", $this->surfaceTypes);
|
79
|
case "le.e.":
|
80
|
return array_search("left edge", $this->surfaceTypes);
|
81
|
// Domnenky, v test souborech se nenachazi -> udelat podle toho export
|
82
|
case "r.e.":
|
83
|
return array_search("right edge", $this->surfaceTypes);
|
84
|
case "s.i.1":
|
85
|
return array_search("seal impression 1", $this->surfaceTypes);
|
86
|
case "s.i.2":
|
87
|
return array_search("seal impression 2", $this->surfaceTypes);
|
88
|
case "s.i.3":
|
89
|
return array_search("seal impression 3", $this->surfaceTypes);
|
90
|
case "s.i.4":
|
91
|
return array_search("seal impression 4", $this->surfaceTypes);
|
92
|
case "s.i.5":
|
93
|
return array_search("seal impression 5", $this->surfaceTypes);
|
94
|
case "s.i.6":
|
95
|
return array_search("seal impression 6", $this->surfaceTypes);
|
96
|
case "s.i.7":
|
97
|
return array_search("seal impression 7", $this->surfaceTypes);
|
98
|
case "s.i.8":
|
99
|
return array_search("seal impression 8", $this->surfaceTypes);
|
100
|
default:
|
101
|
return array_search("obverse", $this->surfaceTypes);
|
102
|
}
|
103
|
}
|
104
|
}
|