-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixedWidthFieldRow.php
63 lines (52 loc) · 1.12 KB
/
FixedWidthFieldRow.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
<?php
class FixedWidthField
{
protected $width;
protected $value;
protected $align;
protected $padding;
public function __construct($width, $value, $align='L', $padding=' ')
{
settype($width, 'int');
$this->width = $width;
$this->value = (string)$value;
if (strlen($value) > $width)
throw new Exception('Value too wide for field');
$this->align = $align;
$this->padding = $padding;
}
public function __toString()
{
$format = '%';
$format .= "'" . $this->padding;
if ('L' == $this->align)
$format .= '-';
$format .= $this->width;
$format .= 's';
return sprintf($format, $this->value);
}
}
class FixedWidthFieldRow
{
protected $fields = array();
public function addField(FixedWidthField $field)
{
$this->fields[] = $field;
}
public function __toString()
{
return implode('', $this->fields);
}
}
class FixedWidthFieldFile
{
protected $rows = array();
public function addRow(FixedWidthFieldRow $row)
{
$this->rows[] = $row;
}
public function __toString()
{
return implode(PHP_EOL, $this->rows());
}
}