Skip to content

Commit 1647448

Browse files
committed
Add nodeXml...
1 parent 8ce8e78 commit 1647448

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/class-xml-file.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
/*
44
Module Name: xml_file
5-
Module Source: http://www.moreglory.net/
5+
Module Source: http://www.github.com/bhoogter/xml-file
66
Description: A convenient XML file wrapper designed for datastore and retrieval.
7-
Version: 1.0
7+
Version: 0.2.70
88
Author: Benjamin Hoogterp
99
Author URI: http://www.BenjaminHoogterp.com/
1010
License: LGPLv3
@@ -854,7 +854,7 @@ static function tidyJson_string($json, $options = JSON_PRETTY_PRINT) {
854854
if (!is_string($json)) throw new InvalidArgumentException("Expecting string, got [" . gettype($json) . "]");
855855
$data = json_decode($json);
856856
$result = json_encode($data, $options);
857-
print $result;
857+
// print $result;
858858
return $result;
859859
}
860860

@@ -978,6 +978,14 @@ static function tidy_cleanup($s, $style = 'auto') // when tidy makes
978978
return $s;
979979
}
980980

981+
static function nodeXml($element) {
982+
return $element->ownerDocument->saveXML($element);
983+
}
984+
985+
static function nodeXmlFile($element) {
986+
return new xml_file(self::nodeXml($element));
987+
}
988+
981989
static function backtrace($m = '')
982990
{
983991
print "ERROR: $m";

tests/nodeXml-tests.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
use PHPUnit\Framework\TestCase;
4+
5+
class node_xml_tests extends TestCase
6+
{
7+
public function testNodeXml(): void
8+
{
9+
$obj = new xml_file(__DIR__ . "/resources/test-data.xml");
10+
$element = $obj->nde("/items/item[@id='2']");
11+
$result = xml_file::nodeXml($element);
12+
$this->assertTrue(strpos($result, "<size>Medium</size>") != false);
13+
}
14+
15+
public function testNodeXmlFile(): void
16+
{
17+
$obj = new xml_file(__DIR__ . "/resources/test-data.xml");
18+
$element = $obj->nde("/items/item[@id='2']");
19+
$result = xml_file::nodeXmlFile($element);
20+
$this->assertEquals("Medium", $result->get("/item/size"));
21+
}
22+
}

0 commit comments

Comments
 (0)