1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created with PHP 5.6 generator |
4
|
|
|
* User: Victor Ribeiro <[email protected]> |
5
|
|
|
* PHP 5.6 generator created by Victor MECH - April 2016 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace LazyEight\DiTesto; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use LazyEight\BasicTypes\Stringy; |
12
|
|
|
use LazyEight\DiTesto\Exceptions\InvalidFileLocationException; |
13
|
|
|
use LazyEight\DiTesto\Exceptions\InvalidFileTypeException; |
14
|
|
|
use LazyEight\DiTesto\Parser\TextContentParser; |
15
|
|
|
use LazyEight\DiTesto\ValueObject\FileContent; |
16
|
|
|
use LazyEight\DiTesto\ValueObject\FileLocation; |
17
|
|
|
use LazyEight\DiTesto\ValueObject\TextFile\TextFile; |
18
|
|
|
|
19
|
|
|
class TextFileLoader |
20
|
|
|
{ |
21
|
|
|
/* |
22
|
|
|
* @var FileLocation |
23
|
|
|
*/ |
24
|
|
|
private $fileLocation; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FileContent |
28
|
|
|
*/ |
29
|
|
|
private $rawContent; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var TextFile |
33
|
|
|
*/ |
34
|
|
|
private $file; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var TextFileValidator |
38
|
|
|
*/ |
39
|
|
|
private $validator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param FileLocation $fileLocation |
43
|
|
|
*/ |
44
|
1 |
|
public function __construct(FileLocation $fileLocation) |
45
|
|
|
{ |
46
|
1 |
|
$this->fileLocation = $fileLocation; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Load the file to memory |
51
|
|
|
* |
52
|
|
|
* @throws InvalidFileLocationException |
53
|
|
|
* @throws InvalidFileTypeException |
54
|
|
|
* @return TextFile The text file itself |
55
|
|
|
*/ |
56
|
3 |
|
public function loadFile() |
57
|
|
|
{ |
58
|
3 |
|
$this->validateFile(); |
59
|
1 |
|
$this->rawContent = $this->loadFileFromOS(); |
60
|
1 |
|
$this->file = $this->createFileObject(); |
61
|
1 |
|
return clone $this->file; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return FileContent |
66
|
|
|
*/ |
67
|
1 |
|
protected function loadFileFromOS() |
68
|
|
|
{ |
69
|
1 |
|
return new FileContent(new Stringy(file_get_contents($this->fileLocation->getValue()))); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return TextFile |
74
|
|
|
*/ |
75
|
1 |
|
protected function createFileObject() |
76
|
|
|
{ |
77
|
1 |
|
$parser = new TextContentParser($this->rawContent); |
78
|
1 |
|
return new TextFile($this->fileLocation, $this->rawContent, $parser->parse()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @throws InvalidFileLocationException |
83
|
|
|
* @throws InvalidFileTypeException |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
3 |
|
protected function validateFile() |
87
|
|
|
{ |
88
|
3 |
|
$this->getValidator()->validate(); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return TextFileValidator |
93
|
|
|
*/ |
94
|
|
|
protected function getValidator() |
95
|
|
|
{ |
96
|
|
|
if (!$this->validator) { |
97
|
|
|
$this->validator = new TextFileValidator($this->fileLocation); |
98
|
|
|
} |
99
|
|
|
return $this->validator; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|