-
Notifications
You must be signed in to change notification settings - Fork 4
/
AbstractFile.php
69 lines (54 loc) · 1.39 KB
/
AbstractFile.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
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 9/2/14
* Time: 1:00 PM
*/
namespace Giftcards\FixedWidth;
abstract class AbstractFile implements \IteratorAggregate, \ArrayAccess, FileInterface
{
public function __toString()
{
return implode($this->getLineSeparator(), $this->getLines());
}
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);
}
public function getIterator()
{
return new FileIterator($this);
}
public function newLine()
{
$this->addLine(str_repeat(' ', $this->getWidth()));
return $this->getLine($this->count() - 1);
}
protected function validateLine($line)
{
if (!$line instanceof Line) {
$line = new Line((string)$line);
}
if ($line->getLength() != $this->getWidth()) {
throw new \InvalidArgumentException(sprintf(
'All lines in a batch file must be %d chars wide this line is %d chars wide.',
$this->getWidth(),
strlen($line)
));
}
return $line;
}
}