Skip to content

Commit 70ef3a0

Browse files
committed
Issue #117 Fix datafield parsing with warnings
1 parent 4b30859 commit 70ef3a0

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/DataFields.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public function __toArray()
8080
*/
8181
public static function parse($input)
8282
{
83+
// Strip any warning blocks from top
84+
if (strncmp('WARNING:', $input, 8) === 0) {
85+
// Remove the 'WARNING:' line and all following lines starting with a space
86+
$input = preg_replace('/^WARNING:.*$[\n\r]+( .*$[\n\r]+)*/m', '', $input);
87+
}
8388
if (strncmp('---', $input, 3) === 0) {
8489
// Split blocks only if '---' is followed by 'FieldType'
8590
$blocks = preg_split(

tests/DataFieldsTest.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ class DataFieldsTest extends \PHPUnit\Framework\TestCase
66
public function testDataFieldParsing()
77
{
88
$dataFields = new DataFields($this->_testInput);
9-
//print_r($dataFields->__toArray());exit;
109
$this->assertEquals($this->_parsedResult, $dataFields->__toArray());
1110
}
1211

12+
public function testParsingOfDataFieldWithWarning()
13+
{
14+
$dataFields = new DataFields($this->_warningInput);
15+
$this->assertEquals($this->_parsedWarningResult, $dataFields->__toArray());
16+
}
17+
1318
protected $_testInput = <<<DATA
1419
---
1520
FieldType: Text
@@ -59,6 +64,7 @@ public function testDataFieldParsing()
5964
---more:colons:
6065
and
6166
multi lines
67+
even indented
6268
6369
FieldJustification: Left
6470
DATA;
@@ -101,8 +107,46 @@ public function testDataFieldParsing()
101107
'FieldNameAlt' => 'field5_alt',
102108
'FieldFlags' => 0,
103109
'FieldValue' => "field:with:colons\n\n---more:colons:\nand\nmulti lines\n",
104-
'FieldValueDefault' => "default:with:colons\n\n---more:colons:\nand\nmulti lines\n",
110+
'FieldValueDefault' => "default:with:colons\n\n---more:colons:\nand\nmulti lines\n even indented\n",
105111
'FieldJustification' => 'Left',
106112
]
107113
];
114+
115+
protected $_warningInput = <<<DATA
116+
WARNING: The creator of the input PDF:
117+
example_pdf_with_password.pdf
118+
has set an owner password (which is not required to handle this PDF).
119+
You did not supply this password. Please respect any copyright.
120+
---
121+
FieldType: Text
122+
FieldName: field1
123+
FieldNameAlt: field1_alt
124+
FieldFlags: 0
125+
FieldJustification: Left
126+
---
127+
FieldType: Text
128+
FieldName: field2
129+
FieldNameAlt: field2_alt
130+
FieldFlags: 0
131+
FieldValue: value:with:colons
132+
FieldJustification: Left
133+
DATA;
134+
135+
protected $_parsedWarningResult = [
136+
[
137+
'FieldType' => 'Text',
138+
'FieldName' => 'field1',
139+
'FieldNameAlt' => 'field1_alt',
140+
'FieldFlags' => 0,
141+
'FieldJustification' => 'Left',
142+
],
143+
[
144+
'FieldType' => 'Text',
145+
'FieldName' => 'field2',
146+
'FieldNameAlt' => 'field2_alt',
147+
'FieldFlags' => 0,
148+
'FieldValue' => 'value:with:colons',
149+
'FieldJustification' => 'Left',
150+
],
151+
];
108152
}

0 commit comments

Comments
 (0)