1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TextFile; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
6
|
|
|
use TextFile\Exception\InvalidReaderException; |
7
|
|
|
use TextFile\Exception\InvalidWalkerException; |
8
|
|
|
use TextFile\Exception\InvalidWriterException; |
9
|
|
|
use TextFile\Exception\OutOfBoundsException; |
10
|
|
|
use TextFile\Factory\ReaderFactory; |
11
|
|
|
use TextFile\Factory\WalkerFactory; |
12
|
|
|
use TextFile\Factory\WriterFactory; |
13
|
|
|
use TextFile\Reader\SimpleReader; |
14
|
|
|
use TextFile\Walker\SimpleWalker; |
15
|
|
|
use TextFile\Writer\PrependingWriter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class TextFile |
19
|
|
|
* |
20
|
|
|
* @package TextFile |
21
|
|
|
*/ |
22
|
|
|
class TextFile |
23
|
|
|
{ |
24
|
|
|
/** @var \SplFileObject */ |
25
|
|
|
protected $splFileObject; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $fileName; |
29
|
|
|
|
30
|
|
|
/** @var Filesystem */ |
31
|
|
|
protected $fileSystem; |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
protected $currentLine = 0; |
35
|
|
|
|
36
|
|
|
/** @var WriterFactory */ |
37
|
|
|
protected $writerFactory; |
38
|
|
|
|
39
|
|
|
/** @var WalkerFactory */ |
40
|
|
|
protected $walkerFactory; |
41
|
|
|
|
42
|
|
|
/** @var ReaderFactory */ |
43
|
|
|
protected $readerFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* TextFile constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $fileName |
49
|
|
|
*/ |
50
|
2 |
|
public function __construct($fileName) |
51
|
|
|
{ |
52
|
2 |
|
$this->fileSystem = new Filesystem(); |
53
|
2 |
|
$this->fileName = $fileName; |
54
|
2 |
|
$this->writerFactory = new WriterFactory(); |
55
|
2 |
|
$this->walkerFactory = new WalkerFactory(); |
56
|
2 |
|
$this->readerFactory = new ReaderFactory(); |
57
|
|
|
|
58
|
2 |
|
$this->open($fileName); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $fileName |
63
|
|
|
*/ |
64
|
2 |
|
public function open($fileName) |
65
|
|
|
{ |
66
|
2 |
|
if (!$this->fileSystem->exists($fileName)) { |
67
|
1 |
|
$this->createEmpty($fileName); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
2 |
|
$this->splFileObject = new \SplFileObject($fileName, 'r+b'); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \SplFileObject |
75
|
|
|
*/ |
76
|
2 |
|
public function getSplFileObject() |
77
|
|
|
{ |
78
|
2 |
|
return $this->splFileObject; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $from |
83
|
|
|
* @param int $to |
84
|
|
|
* @param string $readerClass |
85
|
|
|
* @param string $walkerClass |
86
|
|
|
* |
87
|
|
|
* @return \LimitIterator |
88
|
|
|
* @throws InvalidReaderException |
89
|
|
|
* @throws InvalidWalkerException |
90
|
|
|
* @throws OutOfBoundsException |
91
|
|
|
*/ |
92
|
|
|
public function getLinesRange($from, $to, $readerClass = SimpleReader::class, $walkerClass = SimpleWalker::class) |
93
|
|
|
{ |
94
|
|
|
$walker = $this->walkerFactory->createWalker($walkerClass); |
95
|
|
|
|
96
|
|
|
return $this->readerFactory->createReader($readerClass, $walker)->getLinesRange($this->splFileObject, $from, $to); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $walkerClass |
101
|
|
|
* |
102
|
|
|
* @return int |
103
|
|
|
* @throws InvalidWalkerException |
104
|
|
|
*/ |
105
|
|
|
public function countLines($walkerClass = SimpleWalker::class) |
106
|
|
|
{ |
107
|
|
|
return $this->walkerFactory->createWalker($walkerClass)->countLines($this->splFileObject); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $lineNumber |
112
|
|
|
* @param string $walkerClass |
113
|
|
|
* |
114
|
|
|
* @throws OutOfBoundsException |
115
|
|
|
* @throws InvalidWalkerException |
116
|
|
|
*/ |
117
|
|
|
public function goToLine($lineNumber, $walkerClass = SimpleWalker::class) |
118
|
|
|
{ |
119
|
|
|
$this->walkerFactory->createWalker($walkerClass)->goToLine($this->splFileObject, $lineNumber); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param int $characterNumber |
124
|
|
|
* @param string $walkerClass |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
* @throws OutOfBoundsException |
128
|
|
|
*/ |
129
|
|
|
public function goBeforeCharacter($characterNumber, $walkerClass = SimpleWalker::class) |
130
|
|
|
{ |
131
|
|
|
$this->walkerFactory->createWalker($walkerClass)->goBeforeCharacter($this->splFileObject, $characterNumber); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $readerClass |
136
|
|
|
* @param string $walkerClass |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
* @throws InvalidReaderException |
140
|
|
|
* @throws OutOfBoundsException |
141
|
|
|
*/ |
142
|
|
|
public function getNextLineContent($readerClass = SimpleReader::class, $walkerClass = SimpleWalker::class) |
143
|
|
|
{ |
144
|
|
|
$walker = $this->walkerFactory->createWalker($walkerClass); |
145
|
|
|
|
146
|
|
|
return $this->readerFactory->createReader($readerClass, $walker)->getNextLineContent($this->splFileObject); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $readerClass |
151
|
|
|
* @param string $walkerClass |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
* @throws InvalidReaderException |
155
|
|
|
* @throws OutOfBoundsException |
156
|
|
|
*/ |
157
|
|
|
public function getPreviousLineContent($readerClass = SimpleReader::class, $walkerClass = SimpleWalker::class) |
158
|
|
|
{ |
159
|
|
|
$walker = $this->walkerFactory->createWalker($walkerClass); |
160
|
|
|
|
161
|
|
|
return $this->readerFactory->createReader($readerClass, $walker)->getPreviousLineContent($this->splFileObject); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $readerClass |
166
|
|
|
* @param string $walkerClass |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
* @throws InvalidReaderException |
170
|
|
|
* @throws OutOfBoundsException |
171
|
|
|
*/ |
172
|
|
|
public function getCurrentLineContent($readerClass = SimpleReader::class, $walkerClass = SimpleWalker::class) |
173
|
|
|
{ |
174
|
|
|
$walker = $this->walkerFactory->createWalker($walkerClass); |
175
|
|
|
|
176
|
|
|
return $this->readerFactory->createReader($readerClass, $walker)->getCurrentLineContent($this->splFileObject); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param int $lineNumber |
181
|
|
|
* @param string $readerClass |
182
|
|
|
* @param string $walkerClass |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
* @throws InvalidReaderException |
186
|
|
|
* @throws OutOfBoundsException |
187
|
|
|
*/ |
188
|
|
|
public function getLineContent($lineNumber, $readerClass = SimpleReader::class, $walkerClass = SimpleWalker::class) |
189
|
|
|
{ |
190
|
|
|
$walker = $this->walkerFactory->createWalker($walkerClass); |
191
|
|
|
|
192
|
|
|
return $this->readerFactory->createReader($readerClass, $walker)->getLineContent($this->splFileObject, $lineNumber); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $readerClass |
197
|
|
|
* @param string $walkerClass |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
* @throws InvalidReaderException |
201
|
|
|
* @throws OutOfBoundsException |
202
|
|
|
*/ |
203
|
|
|
public function getNextCharacterContent($readerClass = SimpleReader::class, $walkerClass = SimpleWalker::class) |
204
|
|
|
{ |
205
|
|
|
$walker = $this->walkerFactory->createWalker($walkerClass); |
206
|
|
|
|
207
|
|
|
return $this->readerFactory->createReader($readerClass, $walker)->getNextCharacterContent($this->splFileObject); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $readerClass |
212
|
|
|
* @param string $walkerClass |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
* @throws InvalidReaderException |
216
|
|
|
* @throws OutOfBoundsException |
217
|
|
|
*/ |
218
|
|
|
public function getPreviousCharacterContent($readerClass = SimpleReader::class, $walkerClass = SimpleWalker::class) |
219
|
|
|
{ |
220
|
|
|
$walker = $this->walkerFactory->createWalker($walkerClass); |
221
|
|
|
|
222
|
|
|
return $this->readerFactory->createReader($readerClass, $walker)->getPreviousCharacterContent($this->splFileObject); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param int $characterNumber |
227
|
|
|
* @param string $readerClass |
228
|
|
|
* @param string $walkerClass |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
* @throws InvalidReaderException |
232
|
|
|
* @throws OutOfBoundsException |
233
|
|
|
*/ |
234
|
|
|
public function getCharacterContent($characterNumber, $readerClass = SimpleReader::class, $walkerClass = SimpleWalker::class) |
235
|
|
|
{ |
236
|
|
|
$walker = $this->walkerFactory->createWalker($walkerClass); |
237
|
|
|
|
238
|
|
|
return $this->readerFactory->createReader($readerClass, $walker)->getCharacterContent($this->splFileObject, $characterNumber); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param string $content |
243
|
|
|
* @param bool $newLine |
244
|
|
|
* @param string $writerClass |
245
|
|
|
* |
246
|
|
|
* @throws InvalidWriterException |
247
|
|
|
*/ |
248
|
|
|
public function writeToLine($content, $newLine = false, $writerClass = PrependingWriter::class) |
249
|
|
|
{ |
250
|
|
|
$this->writerFactory->createWriter($writerClass)->write($this->splFileObject, $content, $newLine); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param string $fileName |
255
|
|
|
*/ |
256
|
|
|
protected function createEmpty($fileName) |
257
|
|
|
{ |
258
|
|
|
$this->fileSystem->touch($fileName); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|