-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParser.php
57 lines (44 loc) · 1.16 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
class Parser
{
/** @var string line to handle */
protected $line;
/** @var string file to parse */
protected $file;
/** @var FileHandler handler - used to process the data */
protected $handler;
/** @var int lines
* Number of maximum lines to read
* 0 means infinite
*/
protected $maxLines;
public function __construct(string $file, FileHandler $handler, int $maxLines = 0)
{
$this->file = $file;
$this->handler = $handler;
$this->maxLines = $maxLines;
$this->run();
}
/**
* Run the parser by going line by line and giving the handler
* the line to process
*
* @return void
*/
protected function run()
{
$fileHandle = fopen($this->file, 'r');
if(!$fileHandle)
{
exit('Cannot open the file!');
}
$i = 0;
while(($line = fgets($fileHandle)) !== false && ($this->maxLines == 0 || $i < $this->maxLines))
{
$array = $this->handler->processLine($line);
$this->handler->process($array);
$i++;
}
fclose($fileHandle);
}
}