Skip to content

Commit 79a5be8

Browse files
committed
Fix PHP 5.6 libxml flags
1 parent 39b34e2 commit 79a5be8

4 files changed

Lines changed: 60 additions & 10 deletions

File tree

src/SbWereWolf/XmlNavigator/Conversion/FastXmlToArray.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ public static function convert(
2626
$name = Notation::NAME,
2727
$seq = Notation::SEQUENCE,
2828
$encoding = null,
29-
$flags = LIBXML_BIGLINES | LIBXML_COMPACT
29+
$flags = null
3030
) {
31+
if ($flags === null) {
32+
$flags = self::defaultLibxmlFlags();
33+
}
34+
3135
/** @var \Closure(XMLReader):array<mixed, mixed> $parse */
3236
$parse = static function (
3337
XMLReader $reader
@@ -76,8 +80,12 @@ public static function prettyPrint(
7680
$val = Notation::VAL,
7781
$attr = Notation::ATTR,
7882
$encoding = null,
79-
$flags = LIBXML_BIGLINES | LIBXML_COMPACT
83+
$flags = null
8084
) {
85+
if ($flags === null) {
86+
$flags = self::defaultLibxmlFlags();
87+
}
88+
8189
/** @var \Closure(XMLReader):array<mixed, mixed> $parse */
8290
$parse = static function (
8391
XMLReader $reader
@@ -248,6 +256,20 @@ private static function requireArrayResult(
248256
return $result;
249257
}
250258

259+
/**
260+
* @return int
261+
*/
262+
private static function defaultLibxmlFlags()
263+
{
264+
$flags = LIBXML_COMPACT;
265+
266+
if (defined('LIBXML_BIGLINES')) {
267+
$flags |= LIBXML_BIGLINES;
268+
}
269+
270+
return $flags;
271+
}
272+
251273
private static function formatLibxmlErrors()
252274
{
253275
$errors = libxml_get_errors();

src/SbWereWolf/XmlNavigator/Conversion/IFastXmlToArray.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface IFastXmlToArray
2222
* @param string $name index for element name
2323
* @param string $seq index for child elements collection
2424
* @param string|null $encoding The document encoding or NULL
25-
* @param int $flags A bitmask of the LIBXML_* constants.
25+
* @param int|null $flags A bitmask of the LIBXML_* constants.
2626
* @return HierarchyNode
2727
*/
2828
public static function convert(
@@ -33,7 +33,7 @@ public static function convert(
3333
$name = Notation::NAME,
3434
$seq = Notation::SEQUENCE,
3535
$encoding = null,
36-
$flags = LIBXML_BIGLINES | LIBXML_COMPACT
36+
$flags = null
3737
);
3838

3939
/** Convert xml document into compact array
@@ -42,7 +42,7 @@ public static function convert(
4242
* @param string $val index for element value
4343
* @param string $attr index for element attributes collection
4444
* @param string|null $encoding The document encoding or NULL
45-
* @param int $flags A bitmask of the LIBXML_* constants.
45+
* @param int|null $flags A bitmask of the LIBXML_* constants.
4646
* @return PrettyNode
4747
*/
4848
public static function prettyPrint(
@@ -51,6 +51,6 @@ public static function prettyPrint(
5151
$val = Notation::VAL,
5252
$attr = Notation::ATTR,
5353
$encoding = null,
54-
$flags = LIBXML_BIGLINES | LIBXML_COMPACT
54+
$flags = null
5555
);
5656
}

src/SbWereWolf/XmlNavigator/Conversion/XmlConverter.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ class XmlConverter implements IXmlConverter
4141
* @param string $name index for the element name
4242
* @param string $seq Index for child elements
4343
* @param string|null $encoding XML document encoding or `null`
44-
* @param int $flags
44+
* @param int|null $flags
4545
*/
4646
public function __construct(
4747
$val = Notation::VALUE,
4848
$attr = Notation::ATTRIBUTES,
4949
$name = Notation::NAME,
5050
$seq = Notation::SEQUENCE,
5151
$encoding = null,
52-
$flags = LIBXML_BIGLINES | LIBXML_COMPACT
52+
$flags = null
5353
) {
5454
$this->name = $name;
5555
$this->val = $val;
5656
$this->attr = $attr;
5757
$this->seq = $seq;
5858
$this->encoding = $encoding;
59-
$this->flags = $flags;
59+
$this->flags = $flags === null ? self::defaultLibxmlFlags() : $flags;
6060
}
6161

6262
/**
@@ -135,4 +135,18 @@ private function isPrevious(
135135
}
136136
return $isPrevious;
137137
}
138+
139+
/**
140+
* @return int
141+
*/
142+
private static function defaultLibxmlFlags()
143+
{
144+
$flags = LIBXML_COMPACT;
145+
146+
if (defined('LIBXML_BIGLINES')) {
147+
$flags |= LIBXML_BIGLINES;
148+
}
149+
150+
return $flags;
151+
}
138152
}

tests/Unit/Conversion/FastXmlToArrayTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function testParseRootElementRejectsBufferedLibxmlErrorsAfterParse()
273273
'<root/>',
274274
'',
275275
null,
276-
LIBXML_BIGLINES | LIBXML_COMPACT,
276+
self::defaultLibxmlFlags(),
277277
static function (\XMLReader $reader){
278278
$dom = new \DOMDocument();
279279
@$dom->loadXML('<broken>');
@@ -304,4 +304,18 @@ static function (\XMLReader $reader){
304304
);
305305
}
306306
}
307+
308+
/**
309+
* @return int
310+
*/
311+
private static function defaultLibxmlFlags()
312+
{
313+
$flags = LIBXML_COMPACT;
314+
315+
if (defined('LIBXML_BIGLINES')) {
316+
$flags |= LIBXML_BIGLINES;
317+
}
318+
319+
return $flags;
320+
}
307321
}

0 commit comments

Comments
 (0)