-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInMemoryFile.php
103 lines (85 loc) · 1.96 KB
/
InMemoryFile.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
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 9/2/14
* Time: 1:00 PM
*/
namespace Giftcards\FixedWidth;
class InMemoryFile extends AbstractFile
{
protected $name;
protected $width;
protected $lines = array();
protected $lineSeparator;
public function __construct(
$name,
$width,
array $lines = array(),
$lineSeparator = "\r\n"
) {
$this->name = $name;
$this->width = (int)$width;
array_walk($lines, array($this, 'addLine'));
$this->lineSeparator = $lineSeparator;
}
/**
* @return string
*/
public function getLines()
{
return $this->lines;
}
public function getLine($index)
{
if ($index >= $this->count()) {
throw new \OutOfBoundsException('The index is outside of the available indexes of lines.');
}
return $this->lines[$index];
}
public function offsetExists($offset)
{
return isset($this->lines[$offset]);
}
public function count()
{
return count($this->lines);
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
public function addLine($line)
{
$this->lines[] = $this->validateLine($line);
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.');
}
$this->lines[$index] = $this->validateLine($line);
return $this;
}
public function removeLine($index)
{
unset($this->lines[$index]);
$this->lines = array_values($this->lines);
return $this;
}
/**
* @return int
*/
public function getWidth()
{
return $this->width;
}
public function getLineSeparator()
{
return $this->lineSeparator;
}
}