Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Update XML deserializer:
Browse files Browse the repository at this point in the history
- add xmlEntities() method
  • Loading branch information
webeweb committed Jul 17, 2021
1 parent e350830 commit 20473f0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
27 changes: 26 additions & 1 deletion src/Serializer/XmlDeserializerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class XmlDeserializerHelper extends SerializerHelper {
*
* @param DOMNode $domNode The DOM node.
* @param string $nodeName The node name.
* @return string|null Returns the attribute value in case of success, null otherwise.
* @return string|null Returns the text content in case of success, null otherwise.
*/
public static function getChildDomNodeTextContent(DOMNode $domNode, string $nodeName): ?string {

Expand Down Expand Up @@ -128,4 +128,29 @@ public static function log(DOMNode $domNode): void {

static::$logger->debug(sprintf('Deserialize a DOM node with name "%s"', $domNode->nodeName), $context);
}

/**
* Convert XML entities.
*
* @param string $filename The filename.
* @param string $encoding The encoding.
* @return string Returns the converted XML entities.
*/
public static function xmlEntities(string $filename, string $encoding = "utf-8"): string {

$pattern = "/(<[A-Za-z_]*>)(.*)(<\/[A-Za-z_]*>)/";
$content = file_get_contents($filename);
$callback = function($matches) use ($encoding) {

$output = [
$matches[1],
htmlentities($matches[2], ENT_XML1 | ENT_QUOTES, $encoding),
$matches[3],
];

return implode("", $output);
};

return preg_replace_callback($pattern, $callback, $content);
}
}
12 changes: 0 additions & 12 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace WBW\Library\Provider\Tests;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

Expand All @@ -24,13 +23,6 @@
*/
abstract class AbstractTestCase extends TestCase {

/**
* DOM document.
*
* @var DOMDocument
*/
protected $document;

/**
* Logger.
*
Expand All @@ -44,10 +36,6 @@ abstract class AbstractTestCase extends TestCase {
protected function setUp(): void {
parent::setUp();

// Set a DOM document mock.
$this->document = new DOMDocument();
$this->document->load(realpath(__DIR__ . "/Serializer/XmlSerializerHelperTest.xml"));

// Set a Logger mock.
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
}
Expand Down
33 changes: 32 additions & 1 deletion tests/Serializer/XmlDeserializerHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace WBW\Library\Provider\Tests\Serializer;

use DOMDocument;
use WBW\Library\Provider\Serializer\XmlDeserializerHelper;
use WBW\Library\Provider\Tests\AbstractTestCase;

Expand All @@ -22,14 +23,44 @@
*/
class XmlDeserializerHelperTest extends AbstractTestCase {

/**
* DOM document.
*
* @var DOMDocument
*/
private $document;

/**
* Filename.
*
* @var string
*/
private $filename;

/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();

// Set a filename mock.
$this->filename = realpath(__DIR__ . "/../Serializer/XmlSerializerHelperTest.xml");

$content = XmlDeserializerHelper::xmlEntities($this->filename);

// Set a DOM document mock.
$this->document = new DOMDocument();
$this->document->loadXml($content);
}

/**
* Tests the getChildDomNodeAttribute() method.
*
* @return void
*/
public function testGetChildDomNodeTextContent(): void {

$this->assertEquals("text content", XmlDeserializerHelper::getChildDomNodeTextContent($this->document->documentElement, "children"));
$this->assertEquals("text & content", XmlDeserializerHelper::getChildDomNodeTextContent($this->document->documentElement, "children"));
$this->assertNull(XmlDeserializerHelper::getChildDomNodeTextContent($this->document->documentElement, "child"));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Serializer/XmlSerializerHelperTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<root attribute="value">
<child></child>
<child></child>
<children> text content </children>
<children> text & content </children>
</root>

0 comments on commit 20473f0

Please sign in to comment.