1 |
2d082bc2
|
Petr Lukašík
|
<?php
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
namespace App\Utils;
|
5 |
|
|
|
6 |
42c20ae6
|
Jan Šedivý
|
use App\Enum\ESurfaceTypes;
|
7 |
|
|
|
8 |
2d082bc2
|
Petr Lukašík
|
class TextParser
|
9 |
|
|
{
|
10 |
ce045025
|
Petr Lukašík
|
private $textFile;
|
11 |
2d082bc2
|
Petr Lukašík
|
private $objectTypes;
|
12 |
|
|
private $surfaceTypes;
|
13 |
|
|
|
14 |
|
|
public function __construct($file)
|
15 |
|
|
{
|
16 |
|
|
$this->textFile = $file;
|
17 |
|
|
}
|
18 |
|
|
|
19 |
ce045025
|
Petr Lukašík
|
/** Rozparsuje importovaný text */
|
20 |
2d082bc2
|
Petr Lukašík
|
public function parseText()
|
21 |
|
|
{
|
22 |
|
|
$array = explode("\n", file_get_contents($this->textFile));
|
23 |
|
|
$array = array_map('trim', $array);
|
24 |
|
|
|
25 |
ce045025
|
Petr Lukašík
|
$nextBook = true;
|
26 |
2d082bc2
|
Petr Lukašík
|
$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 |
67ebe0dc
|
Petr Lukašík
|
if (strlen($value) == 0) {
|
34 |
ce045025
|
Petr Lukašík
|
$nextBook = true;
|
35 |
67ebe0dc
|
Petr Lukašík
|
} 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 |
67ebe0dc
|
Petr Lukašík
|
} elseif ($value[0] == '|') {
|
41 |
|
|
$currentChapter = substr($value, 1);
|
42 |
|
|
$currentSurface = $this->transformAbbrev("obverse");
|
43 |
|
|
} else {
|
44 |
2d082bc2
|
Petr Lukašík
|
if (strchr($value, '$')) {
|
45 |
67ebe0dc
|
Petr Lukašík
|
$currentSurface = $this->transformAbbrev(substr(strchr($value, "&"), 1, -1));
|
46 |
|
|
$output[$currentBook][$currentChapter][$currentSurface][] = trim(substr($value, 0, stripos($value, "&")));
|
47 |
|
|
} else {
|
48 |
2d082bc2
|
Petr Lukašík
|
$output[$currentBook][$currentChapter][$currentSurface][] = $value;
|
49 |
|
|
}
|
50 |
|
|
}
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
return $output;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
ce045025
|
Petr Lukašík
|
/** Nejvíc kontrola souboru, kontroluje zda soubor existuje a zda aspoň začátek souboru vyhovuje formátu */
|
57 |
2d082bc2
|
Petr Lukašík
|
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 |
ce045025
|
Petr Lukašík
|
/** Funkce pro transformaci zkratek surfaceType na jejich příslušná idčka v db */
|
78 |
67ebe0dc
|
Petr Lukašík
|
private function transformAbbrev($abbrev)
|
79 |
|
|
{
|
80 |
42c20ae6
|
Jan Šedivý
|
return array_search(ESurfaceTypes::getSurfaceTypeFromAbbrev($abbrev), $this->surfaceTypes);
|
81 |
2d082bc2
|
Petr Lukašík
|
}
|
82 |
ce045025
|
Petr Lukašík
|
|
83 |
67ebe0dc
|
Petr Lukašík
|
public function getTextFile()
|
84 |
|
|
{
|
85 |
ce045025
|
Petr Lukašík
|
return $this->textFile;
|
86 |
|
|
}
|
87 |
2d082bc2
|
Petr Lukašík
|
}
|