|
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\Validator\TextFileLoaderValidator; |
|
16
|
|
|
use LazyEight\DiTesto\ValueObject\FileContent; |
|
17
|
|
|
use LazyEight\DiTesto\ValueObject\FileLocation; |
|
18
|
|
|
use LazyEight\DiTesto\ValueObject\TextFile\TextFile; |
|
19
|
|
|
|
|
20
|
|
|
class TextFileLoader |
|
21
|
|
|
{ |
|
22
|
|
|
/* |
|
23
|
|
|
* @var FileLocation |
|
24
|
|
|
*/ |
|
25
|
|
|
private $fileLocation; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var FileContent |
|
29
|
|
|
*/ |
|
30
|
|
|
private $rawContent; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var TextFile |
|
34
|
|
|
*/ |
|
35
|
|
|
private $file; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var TextFileLoaderValidator |
|
39
|
|
|
*/ |
|
40
|
|
|
private $validator; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param FileLocation $fileLocation |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function __construct(FileLocation $fileLocation) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->fileLocation = $fileLocation; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Load the file to memory |
|
52
|
|
|
* |
|
53
|
|
|
* @throws InvalidFileLocationException |
|
54
|
|
|
* @throws InvalidFileTypeException |
|
55
|
|
|
* @return TextFile The text file itself |
|
56
|
|
|
*/ |
|
57
|
3 |
|
public function loadFile() |
|
58
|
|
|
{ |
|
59
|
3 |
|
$this->validateFile(); |
|
60
|
1 |
|
$this->rawContent = $this->loadFileFromDisk(); |
|
61
|
1 |
|
$this->file = $this->createFileObject(); |
|
62
|
1 |
|
return clone $this->file; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return FileContent |
|
67
|
|
|
*/ |
|
68
|
1 |
|
protected function loadFileFromDisk() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return new FileContent(new Stringy(file_get_contents($this->fileLocation->getValue()))); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return TextFile |
|
75
|
|
|
*/ |
|
76
|
1 |
|
protected function createFileObject() |
|
77
|
|
|
{ |
|
78
|
1 |
|
$parser = new TextContentParser($this->rawContent); |
|
79
|
1 |
|
return new TextFile($this->fileLocation, $this->rawContent, $parser->parse()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @throws InvalidFileLocationException |
|
84
|
|
|
* @throws InvalidFileTypeException |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
3 |
|
protected function validateFile() |
|
88
|
|
|
{ |
|
89
|
3 |
|
return (new TextFileLoaderValidator($this->fileLocation))->validate(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|