-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFileSystemFile.php
224 lines (179 loc) · 5.72 KB
/
FileSystemFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 12/15/14
* Time: 8:49 PM
*/
namespace Giftcards\FixedWidth;
class FileSystemFile extends AbstractFile
{
protected $fileObject;
protected $lineSeparatorLength;
protected $realLineWidth;
protected $lineSeparator;
protected $width;
public function __construct(
$width,
\SplFileObject $fileObject,
$lineSeparator = "\r\n"
) {
$this->width = $width;
$this->fileObject = $fileObject;
$this->lineSeparator = $lineSeparator;
$this->lineSeparatorLength = strlen($this->lineSeparator);
$this->realLineWidth = $this->width + $this->lineSeparatorLength;
$lineRemainder = $this->getSize() % $this->realLineWidth;
if ($lineRemainder != 0 && $lineRemainder != $this->width) {
throw new \InvalidArgumentException(sprintf(
'It looks like the file supplied does not have all lines of the width %d.
make sure you have the correct line ending being passed since it is used in the comparison to figure this out.',
$this->width
));
}
$this->hasTrailingLineSeparator = $lineRemainder == 0;
}
/**
* be CAREFUL using this function since if the file is large
* loading he whole file into memory could immediately cause
* you to run out of memory. This has been made available to the developer
* if they want to use it but it seems it would usually not be a good idea.
* If you want to go over each line and do something then using iteration
* would probably be better since only one or 2 lines are usually in memory
* at a time
*
* @return string
*/
public function __toString()
{
$string = parent::__toString();
if ($this->hasTrailingLineSeparator) {
$string .= $this->lineSeparator;
}
return $string;
}
/**
* be CAREFUL using this function since if the file is large
* loading he whole file into memory could immediately cause
* you to run out of memory. This has been made available to the developer
* if they want to use it but it seems it would usually not be a good idea.
* If you want to go over each line and do something then using iteration
* would probably be better since only one or 2 lines are usually in memory
* at a time
*
* @return array
*/
public function getLines()
{
return iterator_to_array($this);
}
public function getLine($index)
{
if ($index >= $this->count()) {
throw new \OutOfBoundsException('The index is outside of the available indexes of lines.');
}
return new FileSystemLine($this->fileObject, $this->getLinePosition($index), $this->width);
}
public function count()
{
return ceil($this->getSize()/$this->realLineWidth);
}
public function getName()
{
return $this->fileObject->getFilename();
}
public function getIterator()
{
return new FileIterator($this);
}
public function addLine($line)
{
$line = $this->validateLine($line);
if ($this->getSize() != $this->fileObject->ftell()) {
$this->fileObject->fseek(0, SEEK_END);
}
if (!$this->hasTrailingLineSeparator) {
$this->fileObject->fwrite($this->lineSeparator);
}
$this->fileObject->fwrite((string)$line);
if ($this->hasTrailingLineSeparator) {
$this->fileObject->fwrite($this->lineSeparator);
}
return $this;
}
public function setLine($index, $line)
{
if ($index >= $this->count()) {
throw new \OutOfBoundsException('setLine can only be used to update lines. To add a new line use addLine.');
}
$line = $this->validateLine($line);
$this->fileObject->fseek($this->getLinePosition($index), SEEK_SET);
$this->fileObject->fwrite((string)$line);
$this->fileObject->fwrite($this->lineSeparator);
return $this;
}
public function removeLine($index)
{
throw new \BadMethodCallException('This method is not yet implemented.');
}
protected function getLinePosition($lineIndex)
{
return $this->realLineWidth * $lineIndex;
}
public function offsetExists($offset)
{
return $offset < $this->count();
}
public function offsetGet($offset)
{
return $this->getLine($offset);
}
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->addLine($value);
return;
}
$this->setLine($offset, $value);
}
public function offsetUnset($offset)
{
$this->removeLine($offset);
}
/**
* @return int
*/
public function getWidth()
{
return $this->width;
}
/**
* @return string
*/
public function getLineSeparator()
{
return $this->lineSeparator;
}
public function getFileObject()
{
return $this->fileObject;
}
protected function validateLine($line)
{
$line = (string)$line;
$length = strlen($line);
if ($length != $this->width) {
throw new \InvalidArgumentException(sprintf(
'All lines in a batch file must be %d chars wide this line is %d chars wide.',
$this->width,
strlen($line)
));
}
return $line;
}
protected function getSize()
{
$fileData = $this->fileObject->fstat();
return $fileData['size'];
}
}