Skip to content

Commit 3722f6a

Browse files
committed
Issue #118 Fix DataField parsing of Choice fields
1 parent a5f49ca commit 3722f6a

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/DataFields.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public static function parseBlock($block)
119119
} elseif (preg_match('/([^:]*): ?(.*)/', $line, $match)) {
120120
$key = $match[1];
121121
$value = $match[2];
122-
// Convert multiple keys like 'FieldStateOption' to array
122+
// Convert multiple keys like 'FieldStateOption' or 'FieldValue'
123+
// from Choice fields to array
123124
if (isset($data[$key])) {
124125
$data[$key] = (array) $data[$key];
125126
$data[$key][] = $value;
@@ -136,8 +137,18 @@ public static function parseBlock($block)
136137

137138
/**
138139
* Checks whether the value for the given line number continues on the next
139-
* line. This is the case if the next line does not start with either
140-
* 'FieldValueDefault:' or 'FieldJustification:'.
140+
* line, i.e. is a multiline string.
141+
*
142+
* This can be the case for 'FieldValue' and 'FieldValueDefault' keys. To
143+
* find the end of the string we don't simply test for /^Field/, as this
144+
* would also match multiline strings where a line starts with 'Field'.
145+
*
146+
* Instead we assume that the string is always followed by one of these
147+
* keys:
148+
*
149+
* - 'FieldValue:'
150+
* - 'FieldValueDefault:'
151+
* - 'FieldJustification:'
141152
*
142153
* @param array $lines all lines of the block
143154
* @param int $n the 0-based index of the current line
@@ -150,6 +161,6 @@ protected static function lineContinues($lines, $n, $key)
150161
return
151162
in_array($key, ['FieldValue', 'FieldValueDefault']) &&
152163
array_key_exists($n + 1, $lines) &&
153-
!preg_match('/^Field(ValueDefault|Justification):/', $lines[$n + 1]);
164+
!preg_match('/^Field(Value|ValueDefault|Justification):/', $lines[$n + 1]);
154165
}
155166
}

tests/DataFieldsTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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

@@ -60,6 +59,15 @@ public function testDataFieldParsing()
6059
and
6160
multi lines
6261
62+
FieldJustification: Left
63+
---
64+
FieldType: Choice
65+
FieldName: field6
66+
FieldFlags: 2097152
67+
FieldValue: 1
68+
FieldValue: 2
69+
FieldValue: 3
70+
FieldValue: 4
6371
FieldJustification: Left
6472
DATA;
6573

@@ -103,6 +111,13 @@ public function testDataFieldParsing()
103111
'FieldValue' => "field:with:colons\n\n---more:colons:\nand\nmulti lines\n",
104112
'FieldValueDefault' => "default:with:colons\n\n---more:colons:\nand\nmulti lines\n",
105113
'FieldJustification' => 'Left',
106-
]
114+
],
115+
[
116+
'FieldType' => 'Choice',
117+
'FieldName' => 'field6',
118+
'FieldFlags' => 2097152,
119+
'FieldValue' => [1, 2, 3, 4],
120+
'FieldJustification' => 'Left',
121+
],
107122
];
108123
}

0 commit comments

Comments
 (0)