|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LazyEight\DiTesto; |
|
4
|
|
|
|
|
5
|
|
|
use LazyEight\DiTesto\Interfaces\FileInterface; |
|
6
|
|
|
use LazyEight\DiTesto\Interfaces\MeasurableFileInterface; |
|
7
|
|
|
use LazyEight\DiTesto\Interfaces\ReadableFileInterface; |
|
8
|
|
|
use LazyEight\DiTesto\Interfaces\TraversableFileInterface; |
|
9
|
|
|
use LazyEight\DiTesto\Interfaces\TypableFileInterface; |
|
10
|
|
|
use LazyEight\DiTesto\Interfaces\WritableFileInterface; |
|
11
|
|
|
use Traversable; |
|
12
|
|
|
|
|
13
|
|
|
class TextFile implements |
|
14
|
|
|
ReadableFileInterface, |
|
15
|
|
|
WritableFileInterface, |
|
16
|
|
|
MeasurableFileInterface, |
|
17
|
|
|
TypableFileInterface, |
|
18
|
|
|
FileInterface, |
|
19
|
|
|
TraversableFileInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $path; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ArrayObjectLine |
|
28
|
|
|
*/ |
|
29
|
|
|
private $lines; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* TextFile constructor. |
|
33
|
|
|
* @param string $path |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function __construct(string $path) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$this->path = $path; |
|
38
|
1 |
|
$this->lines = new ArrayObjectLine(); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function exists(): bool |
|
45
|
|
|
{ |
|
46
|
2 |
|
return file_exists($this->path); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function isReadable(): bool |
|
53
|
|
|
{ |
|
54
|
2 |
|
return is_readable($this->path); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function isWritable(): bool |
|
61
|
|
|
{ |
|
62
|
2 |
|
return is_writable($this->path); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function read() |
|
66
|
|
|
{ |
|
67
|
1 |
|
$this->lines = new ArrayObjectLine(array_map(function ($line) { |
|
68
|
1 |
|
return new Line($line); |
|
69
|
1 |
|
}, explode(PHP_EOL, file_get_contents($this->path)))); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function write() |
|
73
|
|
|
{ |
|
74
|
1 |
|
file_put_contents($this->path, $this->lines->getRawContent()); |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return int |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function getSize(): int |
|
81
|
|
|
{ |
|
82
|
3 |
|
return filesize($this->path); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function getType(): string |
|
89
|
|
|
{ |
|
90
|
2 |
|
return mime_content_type($this->path); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function getFilename(): string |
|
97
|
|
|
{ |
|
98
|
2 |
|
return basename($this->path); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function getPath(): string |
|
105
|
|
|
{ |
|
106
|
2 |
|
return dirname($this->path); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function getPathName(): string |
|
113
|
|
|
{ |
|
114
|
2 |
|
return $this->path; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return \ArrayIterator |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function getIterator() |
|
121
|
|
|
{ |
|
122
|
1 |
|
return $this->lines->getIterator(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param mixed $offset |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function offsetExists($offset) |
|
130
|
|
|
{ |
|
131
|
2 |
|
return $this->lines->offsetExists($offset); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param mixed $offset |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function offsetGet($offset) |
|
139
|
|
|
{ |
|
140
|
1 |
|
return $this->lines->offsetGet($offset); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param mixed $offset |
|
145
|
|
|
* @param mixed $value |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function offsetSet($offset, $value) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->lines->offsetSet($offset, $value); |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param mixed $offset |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function offsetUnset($offset) |
|
156
|
|
|
{ |
|
157
|
1 |
|
$this->lines->offsetUnset($offset); |
|
158
|
1 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return int |
|
162
|
|
|
*/ |
|
163
|
1 |
|
public function count() |
|
164
|
|
|
{ |
|
165
|
1 |
|
return $this->lines->count(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @inheritDoc |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function __toString() |
|
172
|
|
|
{ |
|
173
|
1 |
|
return $this->lines->getRawContent(); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|