Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileSystemFile.php
More file actions
Latest commit
224 lines (179 loc) · 5.72 KB
/
Copy pathFileSystemFile.php
File metadata and controls
224 lines (179 loc) · 5.72 KB
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
*/
namespaceGiftcards\FixedWidth;
class FileSystemFile extends AbstractFile
{
protected$fileObject;
protected$lineSeparatorLength;
protected$realLineWidth;
protected$lineSeparator;
protected$width;
publicfunction__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) {
thrownew \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
*/
publicfunction__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
*/
publicfunctiongetLines()
{
returniterator_to_array($this);
}
publicfunctiongetLine($index)
{
if ($index >= $this->count()) {
thrownew \OutOfBoundsException('The index is outside of the available indexes of lines.');
}
returnnewFileSystemLine($this->fileObject, $this->getLinePosition($index), $this->width);
}
publicfunctioncount()
{
returnceil($this->getSize()/$this->realLineWidth);
}
publicfunctiongetName()
{
return$this->fileObject->getFilename();
}
publicfunctiongetIterator()
{
returnnewFileIterator($this);
}
publicfunctionaddLine($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;
}
publicfunctionsetLine($index, $line)
{
if ($index >= $this->count()) {
thrownew \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;
}
publicfunctionremoveLine($index)
{
thrownew \BadMethodCallException('This method is not yet implemented.');
}
protectedfunctiongetLinePosition($lineIndex)
{
return$this->realLineWidth * $lineIndex;
}
publicfunctionoffsetExists($offset)
{
return$offset < $this->count();
}
publicfunctionoffsetGet($offset)
{
return$this->getLine($offset);
}
publicfunctionoffsetSet($offset, $value)
{
if (is_null($offset)) {
$this->addLine($value);
return;
}
$this->setLine($offset, $value);
}
publicfunctionoffsetUnset($offset)
{
$this->removeLine($offset);
}
/**
* @return int
*/
publicfunctiongetWidth()
{
return$this->width;
}
/**
* @return string
*/
publicfunctiongetLineSeparator()
{
return$this->lineSeparator;
}
publicfunctiongetFileObject()
{
return$this->fileObject;
}
protectedfunctionvalidateLine($line)
{
$line = (string)$line;
$length = strlen($line);
if ($length != $this->width) {
thrownew \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;
}
protectedfunctiongetSize()
{
$fileData = $this->fileObject->fstat();
return$fileData['size'];
}
}