Skip to content

Commit

Permalink
Merge pull request #11 from elecena/add/parentName-property
Browse files Browse the repository at this point in the history
Add the "parentName" property
  • Loading branch information
macbre committed Jan 7, 2024
2 parents 43d3bc3 + 1ffcd75 commit a89ca71
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/Nodes/XMLNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
public string $name,
public array $attributes = [],
public ?string $content = null,
public ?string $parentName = null,
) {
}
}
5 changes: 3 additions & 2 deletions src/Nodes/XMLNodeClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
class XMLNodeClose extends XMLNode
{
public function __construct(
string $name
string $name,
?string $parentName,
) {
parent::__construct($name, attributes: [], content: null);
parent::__construct($name, attributes: [], content: null, parentName: $parentName);
}
}
7 changes: 4 additions & 3 deletions src/Nodes/XMLNodeOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
class XMLNodeOpen extends XMLNode
{
public function __construct(
string $name,
array $attributes,
string $name,
array $attributes,
?string $parentName,
) {
parent::__construct($name, $attributes, content: null);
parent::__construct($name, $attributes, content: null, parentName: $parentName);
}
}
9 changes: 6 additions & 3 deletions src/XMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function startXML(\XMLParser $parser, string $tagName, array $attributes)
$this->nodesQueue[] = new Nodes\XMLNodeOpen(
name: $this->currentTagName,
attributes: $this->currentTagAttributes,
parentName: end($this->nodeNamesStack) ?: null
);

$this->nodeNamesStack[] = $tagName;
Expand All @@ -100,19 +101,21 @@ public function charXML(\XMLParser $parser, string $tagContent): void
name: $this->currentTagName,
attributes: $this->currentTagAttributes,
content: $tagContent,
parentName: array_slice($this->nodeNamesStack, -2, 1)[0] ?: null
);
}

public function endXML(\XMLParser $parser, string $tagName): void
{
// Pop the node name off the end of stack
array_pop($this->nodeNamesStack);

// append to the queue of items to iterate over
$this->nodesQueue[] = new Nodes\XMLNodeClose(
name: $tagName,
parentName: end($this->nodeNamesStack) ?: null
);

// Pop the node name off the end of stack
array_pop($this->nodeNamesStack);

// and update the current tag name to properly handle consecutive closing tag and whitespaces
// e.g. </foo>\n\n</bar>
$this->currentTagName = end($this->nodeNamesStack);
Expand Down
1 change: 1 addition & 0 deletions tests/XMLParserCDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ public function testCDataIsProperlyParsed(): void

$this->assertInstanceOf(XMLNodeContent::class, $node);
$this->assertStringStartsWith("<p>\n <a href=\"/mylink/article1\">", trim($node->content));
$this->assertEquals('item', $node->parentName);
}
}
1 change: 1 addition & 0 deletions tests/XMLParserLargeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testIterateByNodeContent()
foreach($this->getParser()->iterateByNodeContent(name: 'loc') as $node) {
$this->assertInstanceOf(XMLNodeContent::class, $node);
$this->assertEquals('loc', $node->name);
$this->assertEquals('url', $node->parentName);
$this->assertStringStartsWith('http', $node->content);

$cnt++;
Expand Down
3 changes: 3 additions & 0 deletions tests/XMLParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function testParsesTheOpeningTags(): void

$this->assertInstanceOf(XMLNodeOpen::class, $sitemapIndex);
$this->assertEquals('sitemapindex', $sitemapIndex->name);
$this->assertNull($sitemapIndex->parentName, 'Root nodes will get null as their parent');
$this->assertEquals('http://www.sitemaps.org/schemas/sitemap/0.9', $sitemapIndex->attributes['xmlns'] ?? null);
}

Expand All @@ -39,6 +40,7 @@ public function testParsesTheClosingTags(): void

$this->assertInstanceOf(XMLNodeClose::class, $closingTag);
$this->assertEquals('sitemapindex', $closingTag->name);
$this->assertNull($closingTag->parentName);
}

public function testParsesTheLocNodes(): void
Expand All @@ -48,6 +50,7 @@ public function testParsesTheLocNodes(): void
foreach($this->getParser() as $item) {
if ($item instanceof XMLNodeContent && $item->name === 'loc') {
$locations[] = $item->content;
$this->assertEquals('sitemap', $item->parentName);
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/XMLParserWhitespacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ public function testProperlyReportWhitespacesBetweenClosingTags(): void

$this->assertCount(1, $locNodesContent);
$this->assertEquals('https://elecena.pl/sitemap-001-search.xml.gz', $locNodesContent[0]->content);
$this->assertEquals('sitemap', $locNodesContent[0]->parentName);
}
}

0 comments on commit a89ca71

Please sign in to comment.