1
|
<?php
|
2
|
|
3
|
|
4
|
namespace App\Utils;
|
5
|
|
6
|
use App\Enum\ESurfaceTypes;
|
7
|
|
8
|
class TextParser
|
9
|
{
|
10
|
private $textFile;
|
11
|
private $objectTypes;
|
12
|
private $surfaceTypes;
|
13
|
|
14
|
public function __construct($file)
|
15
|
{
|
16
|
$this->textFile = $file;
|
17
|
}
|
18
|
|
19
|
/** Rozparsuje importovaný text */
|
20
|
public function parseText()
|
21
|
{
|
22
|
$array = explode("\n", file_get_contents($this->textFile));
|
23
|
$array = array_map('trim', $array);
|
24
|
|
25
|
$nextBook = true;
|
26
|
$output = array();
|
27
|
$currentBook = "";
|
28
|
$currentChapter = "";
|
29
|
$currentSurface = $this->transformAbbrev("obverse");
|
30
|
|
31
|
//TODO kontrola a parsovani object types (tablet, envelope)
|
32
|
foreach ($array as $value) {
|
33
|
if (strlen($value) == 0) {
|
34
|
$nextBook = true;
|
35
|
} elseif ($value[0] == '|' && $nextBook) {
|
36
|
$currentBook = substr($value, 1);
|
37
|
$currentChapter = "";
|
38
|
$currentSurface = $this->transformAbbrev("obverse");
|
39
|
$nextBook = false;
|
40
|
} elseif ($value[0] == '|') {
|
41
|
$currentChapter = substr($value, 1);
|
42
|
$currentSurface = $this->transformAbbrev("obverse");
|
43
|
} else {
|
44
|
if (strchr($value, '$')) {
|
45
|
$currentSurface = $this->transformAbbrev(substr(strchr($value, "&"), 1, -1));
|
46
|
$output[$currentBook][$currentChapter][$currentSurface][] = trim(substr($value, 0, stripos($value, "&")));
|
47
|
} else {
|
48
|
$output[$currentBook][$currentChapter][$currentSurface][] = $value;
|
49
|
}
|
50
|
}
|
51
|
}
|
52
|
|
53
|
return $output;
|
54
|
}
|
55
|
|
56
|
/** Nejvíc kontrola souboru, kontroluje zda soubor existuje a zda aspoň začátek souboru vyhovuje formátu */
|
57
|
public function isFileParsable()
|
58
|
{
|
59
|
if (file_exists($this->textFile)) {
|
60
|
$checker = fopen($this->textFile, "r");
|
61
|
if (fgets($checker)[0] == '|') {
|
62
|
return true;
|
63
|
} else {
|
64
|
return false;
|
65
|
}
|
66
|
} else {
|
67
|
return false;
|
68
|
}
|
69
|
}
|
70
|
|
71
|
public function setTypes($objects, $surfaces)
|
72
|
{
|
73
|
$this->objectTypes = $objects;
|
74
|
$this->surfaceTypes = $surfaces;
|
75
|
}
|
76
|
|
77
|
/** Funkce pro transformaci zkratek surfaceType na jejich příslušná idčka v db */
|
78
|
private function transformAbbrev($abbrev)
|
79
|
{
|
80
|
return array_search(ESurfaceTypes::getSurfaceTypeFromAbbrev($abbrev), $this->surfaceTypes);
|
81
|
}
|
82
|
|
83
|
public function getTextFile()
|
84
|
{
|
85
|
return $this->textFile;
|
86
|
}
|
87
|
}
|