|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LazyEight\DiTesto; |
|
4
|
|
|
|
|
5
|
|
|
use LazyEight\DiTesto\Collections\ArrayObjectLine; |
|
6
|
|
|
use LazyEight\DiTesto\Interfaces\TraversableFileInterface; |
|
7
|
|
|
|
|
8
|
|
|
class TextFile extends File implements TraversableFileInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var ArrayObjectLine |
|
12
|
|
|
*/ |
|
13
|
|
|
private $lines; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @inheritDoc |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct($path, $content = '') |
|
19
|
|
|
{ |
|
20
|
|
|
parent::__construct($path, $content); |
|
21
|
|
|
$this->readLines($content); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $content |
|
26
|
|
|
*/ |
|
27
|
|
|
private function readLines(string $content) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->lines = new ArrayObjectLine(array_map(function ($line) { |
|
30
|
|
|
return new Line($line); |
|
31
|
|
|
}, explode(PHP_EOL, $content))); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getRawContent(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->lines->getRawContent(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setRawContent(string $content) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::setRawContent($content); |
|
42
|
|
|
$this->readLines($content); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return \ArrayIterator |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getIterator() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->lines->getIterator(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param mixed $offset |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function offsetExists($offset) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->lines->offsetExists($offset); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param mixed $offset |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function offsetGet($offset) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->lines->offsetGet($offset); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param mixed $offset |
|
73
|
|
|
* @param mixed $value |
|
74
|
|
|
*/ |
|
75
|
|
|
public function offsetSet($offset, $value) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->lines->offsetSet($offset, $value); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param mixed $offset |
|
82
|
|
|
*/ |
|
83
|
|
|
public function offsetUnset($offset) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->lines->offsetUnset($offset); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return int |
|
90
|
|
|
*/ |
|
91
|
|
|
public function count() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->lines->count(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|