Skip to content

Commit cf0807f

Browse files
author
Malte Riesch
committed
comments in headers
1 parent 124e2af commit cf0807f

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lib/TestDbAcle/Psv/PsvParser.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public function parsePsv($psvTableContent)
105105
$psvHeaderLine = $this->extractHeaders($psvRows);
106106
$headers = $this->splitByPipe($psvHeaderLine);
107107

108+
foreach($headers as $index=>$header){
109+
110+
$headers[$index] = $this->filterHeader($header);
111+
}
112+
108113
$contentTable = array();
109114

110115
foreach ($psvRows as $psvRow) {
@@ -158,6 +163,24 @@ protected function trimArrayElements(array $row)
158163
}
159164
return $row;
160165
}
166+
167+
protected function filterHeader($value)
168+
{
169+
$self = $this;//we need to to do this for PHP 5.3 compatability
170+
$filters = array(//@TODO remove duplication
171+
'stripComments' => function(&$value) use($self){
172+
if(strpos($value, $self::SYMBOL_COMMENT) !== false && strpos($value, $self::SYMBOL_COMMENT)!==0){
173+
list($valuePart,) = preg_split('/(?<!\\\\)'.$self::SYMBOL_COMMENT.'/', $value);
174+
$value = trim($valuePart);
175+
}
176+
},
177+
);
178+
179+
foreach($filters as $filter){
180+
$filter($value);
181+
}
182+
return $value;
183+
}
161184

162185
protected function filterColumnValue($value)
163186
{

tests/TestDbAcle/Psv/PsvParserTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,48 @@ function test_parsePsv_WithEscapedCharacters() {
6666
$this->assertSame($expectedArray, $psvParser->parsePsv($psvToParse));
6767
}
6868

69+
function test_parsePsv_WithNull_AndCommentInLastHeader() {
70+
71+
$psvParser = new \TestDbAcle\Psv\PsvParser();
72+
73+
$psvToParse = "
74+
id |first_name #moo |last_name
75+
10 |NULL #nul |miller
76+
20 |stu |Smith
77+
";
78+
79+
$expectedArray = array(
80+
array("id" => "10",
81+
"first_name" => NULL,
82+
"last_name" => "miller"),
83+
array("id" => "20",
84+
"first_name" => "stu",
85+
"last_name" => "Smith"),
86+
);
87+
$this->assertSame($expectedArray, $psvParser->parsePsv($psvToParse));
88+
}
89+
90+
function test_parsePsv_WithNull_AndCommentInHeaders() {
91+
92+
$psvParser = new \TestDbAcle\Psv\PsvParser();
93+
94+
$psvToParse = "
95+
id |first_name |last_name #comment
96+
10 |NULL #nul |miller
97+
20 |stu |Smith
98+
";
99+
100+
$expectedArray = array(
101+
array("id" => "10",
102+
"first_name" => NULL,
103+
"last_name" => "miller"),
104+
array("id" => "20",
105+
"first_name" => "stu",
106+
"last_name" => "Smith"),
107+
);
108+
$this->assertSame($expectedArray, $psvParser->parsePsv($psvToParse));
109+
}
110+
69111
function test_parsePsv_WithNull_AndInlineComment() {
70112

71113
$psvParser = new \TestDbAcle\Psv\PsvParser();

0 commit comments

Comments
 (0)