Skip to content

Commit 70e0e19

Browse files
committed
Added $elementMap override to parseInnerTree.
1 parent 3c64c15 commit 70e0e19

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

ChangeLog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
ChangeLog
22
=========
33

4+
0.3.0 (2015-02-06)
5+
------------------
6+
7+
* Added `$elementMap` argument to parseInnerTree, for quickly overriding
8+
parsing rules within an element.
9+
10+
411
0.2.2 (2015-02-05)
512
------------------
613

lib/Reader.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ function parse() {
8787
*
8888
* If there's both text and sub-elements, the text will be discarded.
8989
*
90+
* If the $elementMap argument is specified, the existing elementMap will
91+
* be overridden while parsing the tree, and restored after this process.
92+
*
93+
* @param array $elementMap
9094
* @return array|string
9195
*/
92-
function parseInnerTree() {
96+
function parseInnerTree(array $elementMap = null) {
9397

9498
$previousDepth = $this->depth;
9599

@@ -103,6 +107,12 @@ function parseInnerTree() {
103107
return null;
104108
}
105109

110+
if (!is_null($elementMap)) {
111+
$this->pushContext();
112+
$this->elementMap = $elementMap;
113+
}
114+
115+
106116
// Really sorry about the silence operator, seems like I have no
107117
// choice. See:
108118
//
@@ -125,7 +135,7 @@ function parseInnerTree() {
125135
$this->read();
126136
break 2;
127137
case self::NONE :
128-
throw new ParseException('We hit the end of the document prematurely. This likely means that some parser "eats" too many elements.');
138+
throw new ParseException('We hit the end of the document prematurely. This likely means that some parser "eats" too many elements. Do not attempt to continue parsing.');
129139
default :
130140
// Advance to the next element
131141
$this->read();
@@ -134,6 +144,9 @@ function parseInnerTree() {
134144

135145
}
136146

147+
if (!is_null($elementMap)) {
148+
$this->popContext();
149+
}
137150
return ($elements?$elements:$text);
138151

139152
}

lib/Version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ class Version {
1414
/**
1515
* Full version number
1616
*/
17-
const VERSION = '0.2.2';
17+
const VERSION = '0.3.0';
1818

1919
}

tests/Sabre/XML/ReaderTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,58 @@ function testBrokenXML() {
340340

341341
}
342342

343+
/**
344+
* @depends testMappedElement
345+
*/
346+
function testParseInnerTree() {
347+
348+
$input = <<<BLA
349+
<?xml version="1.0"?>
350+
<root xmlns="http://sabredav.org/ns">
351+
<elem1>
352+
<elem1 />
353+
</elem1>
354+
</root>
355+
BLA;
356+
357+
$reader = new Reader();
358+
$reader->elementMap = [
359+
'{http://sabredav.org/ns}elem1' => function(Reader $reader) {
360+
361+
$innerTree = $reader->parseInnerTree(['{http://sabredav.org/ns}elem1' => function(Reader $reader) {
362+
$reader->next();
363+
return "foobar";
364+
}]);
365+
366+
return $innerTree;
367+
}
368+
];
369+
$reader->xml($input);
370+
371+
$output = $reader->parse();
372+
373+
$expected = [
374+
'name' => '{http://sabredav.org/ns}root',
375+
'value' => [
376+
[
377+
'name' => '{http://sabredav.org/ns}elem1',
378+
'value' => [
379+
[
380+
'name' => '{http://sabredav.org/ns}elem1',
381+
'value' => 'foobar',
382+
'attributes' => [],
383+
]
384+
],
385+
'attributes' => [],
386+
],
387+
],
388+
'attributes' => [],
389+
390+
];
391+
392+
$this->assertEquals($expected, $output);
393+
394+
}
395+
343396
}
344397

0 commit comments

Comments
 (0)