Skip to content

Commit

Permalink
Add interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtree committed Oct 8, 2021
1 parent 9a586da commit d62e09c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 19 deletions.
8 changes: 3 additions & 5 deletions src/ArgumentArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}


}
15 changes: 14 additions & 1 deletion src/Contract/EngineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,7 +118,7 @@ public function getElementArgs(DOMElement $element): ArgumentArray;
* @return ArgumentArray
*/
public function getArgsByElementId(string $element_id): ArgumentArray;

/**
* @return string
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public function getArgs(): ArgumentArray
*/
public function innerText(): ?string
{
return $this->engine->sanitizeXml($this->xml);;
return $this->engine->sanitizeXml($this->xml);
;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.*?>(.*)?<\/arg>/im",'',$xml);
$xml = preg_replace("/<arg.*?>(.*)?<\/arg>/im", '', $xml);
// strip INDEX_ATTRIBUTE
$xml = $this->stripAttribute($xml, [self::INDEX_ATTRIBUTE]);

Expand All @@ -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 = '<div>' . $xml . '</div>';
$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);
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/src/Unit/ArgumentArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ArgumentArrayTest extends TestCase
*/
public $args;

public function setUp() : void
public function setUp(): void
{
$this->args = new ArgumentArray();
}
Expand Down Expand Up @@ -147,7 +147,6 @@ public function testMerge()
*/
public function testCurrent()
{

$this->args[] = 'test 1';
$this->assertEquals('test 1', $this->args->current());
}
Expand Down
1 change: 0 additions & 1 deletion tests/src/Unit/Element/AbstractElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public function testInnerText()
// test to ensure INDEX_ATTRIBUTE removed
$this->element->xml = "<div {Engine::INDEX_ATTRIBUTE}<arg>Hello, World</arg>";
$this->assertStringNotContainsString(Engine::INDEX_ATTRIBUTE, $this->element->innerText());

}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Unit/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function tearDown(): void
{
unset($this->engine);
}

/**
* @covers \Ouxsoft\PHPMarkup\Engine::queryFetchAll
*/
Expand Down

0 comments on commit d62e09c

Please sign in to comment.