1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Paraunit\Parser; |
4
|
|
|
|
5
|
|
|
use Paraunit\Printer\OutputContainer; |
6
|
|
|
use Paraunit\Process\ProcessResultInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AbstractParser |
10
|
|
|
* @package Paraunit\Parser |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractParser implements ProcessOutputParserChainElementInterface, OutputContainerBearerInterface |
13
|
|
|
{ |
14
|
|
|
/** @var OutputContainer */ |
15
|
|
|
protected $outputContainer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* AbstractParser constructor. |
19
|
|
|
*/ |
20
|
37 |
|
public function __construct() |
21
|
|
|
{ |
22
|
37 |
|
$this->assertConstantExist('TAG'); |
23
|
37 |
|
$this->assertConstantExist('TITLE'); |
24
|
37 |
|
$this->assertConstantExist('PARSING_REGEX'); |
25
|
|
|
|
26
|
37 |
|
$this->outputContainer = new OutputContainer(static::TAG, static::TITLE); |
27
|
37 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return OutputContainer |
31
|
|
|
*/ |
32
|
26 |
|
public function getOutputContainer() |
33
|
|
|
{ |
34
|
26 |
|
return $this->outputContainer; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ProcessResultInterface $process |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
24 |
|
protected function storeParsedBlocks(ProcessResultInterface $process) |
42
|
|
|
{ |
43
|
24 |
|
$parsedBlob = $this->parse($process); |
44
|
|
|
|
45
|
24 |
|
if (isset($parsedBlob[1])) { |
46
|
10 |
|
$parsedBlocks = preg_split('/^\d+\) /m', $parsedBlob[1]); |
47
|
|
|
// il primo è sempre vuoto a causa dello split |
48
|
10 |
|
unset($parsedBlocks[0]); |
49
|
|
|
|
50
|
10 |
|
$this->outputContainer->addToOutputBuffer($parsedBlocks); |
51
|
10 |
|
$this->outputContainer->addFileName($process->getFilename()); |
52
|
|
|
|
53
|
10 |
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
18 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ProcessResultInterface $process |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
18 |
|
protected function parsingFoundSomething(ProcessResultInterface $process) |
64
|
|
|
{ |
65
|
18 |
|
if (count($this->parse($process)) > 0) { |
66
|
4 |
|
$this->outputContainer->addFileName($process->getFilename()); |
67
|
|
|
|
68
|
4 |
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
15 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
37 |
|
private function parse(ProcessResultInterface $process) |
75
|
|
|
{ |
76
|
37 |
|
$parsedBlob = array(); |
77
|
37 |
|
preg_match(static::PARSING_REGEX, $process->getOutput(), $parsedBlob); |
78
|
|
|
|
79
|
37 |
|
return $parsedBlob; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $constantName |
84
|
|
|
* @throws \Exception |
85
|
|
|
*/ |
86
|
37 |
|
private function assertConstantExist($constantName) |
87
|
|
|
{ |
88
|
37 |
|
if ( ! defined('static::' . $constantName)) { |
89
|
|
|
throw new \Exception('Missing ' .$constantName . ' constant in ' . get_class($this)); |
90
|
|
|
} |
91
|
37 |
|
} |
92
|
|
|
} |
93
|
|
|
|