From d62e09c911f7e91f2f93831458a043b484326257 Mon Sep 17 00:00:00 2001 From: hxtree Date: Fri, 8 Oct 2021 00:34:14 -0500 Subject: [PATCH] Add interface methods --- src/ArgumentArray.php | 8 +++----- src/Contract/EngineInterface.php | 15 ++++++++++++++- src/Element/AbstractElement.php | 3 ++- src/Engine.php | 16 ++++++++-------- tests/src/Unit/ArgumentArrayTest.php | 3 +-- tests/src/Unit/Element/AbstractElementTest.php | 1 - tests/src/Unit/EngineTest.php | 2 +- 7 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/ArgumentArray.php b/src/ArgumentArray.php index 10b7d2f..74f9dcb 100644 --- a/src/ArgumentArray.php +++ b/src/ArgumentArray.php @@ -44,7 +44,7 @@ class ArgumentArray implements * @param string|null $type * @return void */ - public function set(string $name, string $value = null, ?string $type = NULL) : void + public function set(string $name, string $value = null, ?string $type = null): void { $type = strtolower($type); @@ -205,16 +205,14 @@ public function key() /** * @return bool */ - public function valid() : bool + public function valid(): bool { $k = array_keys($this->container); return isset($k[$this->index]); } - public function rewind() : void + public function rewind(): void { $this->index = 0; } - - } diff --git a/src/Contract/EngineInterface.php b/src/Contract/EngineInterface.php index 2adc268..c736fc6 100644 --- a/src/Contract/EngineInterface.php +++ b/src/Contract/EngineInterface.php @@ -71,6 +71,19 @@ public function queryFetchAll(string $query, DOMElement $node = null): ?DOMNodeL */ public function renderElement(string $element_id): bool; + /** + * @param string $xml + * @return string + */ + public function sanitizeXml(string $xml): string; + + /** + * @param string $xml + * @param array $attributes + * @return string + */ + public function stripAttributes(string $xml, array $attributes): string; + /** * @param string $element_id * @return string @@ -105,7 +118,7 @@ public function getElementArgs(DOMElement $element): ArgumentArray; * @return ArgumentArray */ public function getArgsByElementId(string $element_id): ArgumentArray; - + /** * @return string */ diff --git a/src/Element/AbstractElement.php b/src/Element/AbstractElement.php index ded982c..4d227ce 100644 --- a/src/Element/AbstractElement.php +++ b/src/Element/AbstractElement.php @@ -152,7 +152,8 @@ public function getArgs(): ArgumentArray */ public function innerText(): ?string { - return $this->engine->sanitizeXml($this->xml);; + return $this->engine->sanitizeXml($this->xml); + ; } /** diff --git a/src/Engine.php b/src/Engine.php index 6b189bb..ab40c6c 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -34,12 +34,12 @@ class Engine implements EngineInterface { // TODO: implement PHPMarkup const - const RETURN_CALL = 1; + public const RETURN_CALL = 1; /** * marker attribute used by Engine to identify DOMElement during processing */ - const INDEX_ATTRIBUTE = '_ELEMENT_ID'; + public const INDEX_ATTRIBUTE = '_ELEMENT_ID'; /** * @var DocumentInterface|Document|DOMDocument DOM @@ -390,10 +390,10 @@ public function getElementArgs(DOMElement $element): ArgumentArray * @param string $xml * @return string */ - public function sanitizeXml(string $xml) : string + public function sanitizeXml(string $xml): string { // strip args - $xml = preg_replace("/(.*)?<\/arg>/im",'',$xml); + $xml = preg_replace("/(.*)?<\/arg>/im", '', $xml); // strip INDEX_ATTRIBUTE $xml = $this->stripAttribute($xml, [self::INDEX_ATTRIBUTE]); @@ -406,15 +406,15 @@ public function sanitizeXml(string $xml) : string * @param array $attributes * @return string */ - function stripAttributes(string $xml, array $attributes) : string + public function stripAttributes(string $xml, array $attributes): string { $xml = '
' . $xml . '
'; - $dom = new DOMDocument; + $dom = new DOMDocument(); $dom->loadXML($xml); $xPath = new DOMXPath($dom); - foreach($attributes as $attribute) { + foreach ($attributes as $attribute) { $nodes = $xPath->query('//*[@' . $attribute . ']'); - foreach($nodes as $node) { + foreach ($nodes as $node) { $node->removeAttribute($attribute); } } diff --git a/tests/src/Unit/ArgumentArrayTest.php b/tests/src/Unit/ArgumentArrayTest.php index f8a7562..a0991e2 100644 --- a/tests/src/Unit/ArgumentArrayTest.php +++ b/tests/src/Unit/ArgumentArrayTest.php @@ -21,7 +21,7 @@ class ArgumentArrayTest extends TestCase */ public $args; - public function setUp() : void + public function setUp(): void { $this->args = new ArgumentArray(); } @@ -147,7 +147,6 @@ public function testMerge() */ public function testCurrent() { - $this->args[] = 'test 1'; $this->assertEquals('test 1', $this->args->current()); } diff --git a/tests/src/Unit/Element/AbstractElementTest.php b/tests/src/Unit/Element/AbstractElementTest.php index 8f249a3..8db9454 100644 --- a/tests/src/Unit/Element/AbstractElementTest.php +++ b/tests/src/Unit/Element/AbstractElementTest.php @@ -91,7 +91,6 @@ public function testInnerText() // test to ensure INDEX_ATTRIBUTE removed $this->element->xml = "
Hello, World"; $this->assertStringNotContainsString(Engine::INDEX_ATTRIBUTE, $this->element->innerText()); - } /** diff --git a/tests/src/Unit/EngineTest.php b/tests/src/Unit/EngineTest.php index ca51f7e..7712054 100644 --- a/tests/src/Unit/EngineTest.php +++ b/tests/src/Unit/EngineTest.php @@ -34,7 +34,7 @@ public function tearDown(): void { unset($this->engine); } - + /** * @covers \Ouxsoft\PHPMarkup\Engine::queryFetchAll */