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

Commit

Permalink
Added smart list to array argument pass. Auto remove <arg> tags from …
Browse files Browse the repository at this point in the history
…inner XML
  • Loading branch information
hxtree committed Dec 13, 2019
1 parent 0a2afa9 commit 9d6f5b8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/ConditionExample/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1>Condition Examples</h1>
-->
<condition>
<arg name="date_start">Aug 1. 2019</arg>
<arg name="date_end">December 12, 2019</arg>
<arg name="date_end">December 30, 2019</arg>
<p>This page was loaded between Aug 1 and Dec 30 2019</p>
</condition>

Expand Down
57 changes: 48 additions & 9 deletions src/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ public function instantiateElement(string $xpath_expression, string $class_name)
continue;
}

// get args from element and remove child arg
$args = $this->getArgs($element);

// get xml from element
$xml = $this->getXml($element);

// get args from element
$args = $this->getArgs($element);

// instantiate element
$element_object = new $element_class_name($xml, $args);

Expand Down Expand Up @@ -284,6 +284,35 @@ private function getXml(\DOMElement $element): string
return $xml;
}

/**
* Adds an item to smart list.
*
* @param array $args
* @param string $name
* @param string $value
*/
private function addToSmartList(array &$args, string $name, string $value) : void {

if( ! isset($args[$name]) ) {
// set value
$args[$name] = $value;
} else if ($args[$name] == $value ) {
// if item value exists as string skip
} else if( is_string($args[$name]) ) {
// change string value to array
$present_value = $args[$name];
$args[$name] = [];
array_push($args[$name], $present_value);
array_push($args[$name], $value);
} else if (in_array($value, $args[$name]) ) {
// if item already exists return
return;
} else if ( is_array($args[$name]) ) {
// add to array
array_push($args[$name], $value);
}
}

/**
* Get element's ARGs
*
Expand All @@ -297,16 +326,26 @@ private function getArgs(\DOMElement &$element): array
// get attributes
if ($element->hasAttributes()) {
foreach ($element->attributes as $name => $attribute) {
$args[$name] = $attribute->value;
$this->addToSmartList($args, $name, $attribute->value);
}
}

// get child args
$objects = $element->getElementsByTagName('arg');
foreach ($objects as $object) {
$name = $object->getAttribute('name');
$value = $object->nodeValue;
$args[$name] = $value;
$arg_elements = $element->getElementsByTagName('arg');
// iterate in reverse threw list of arguments to avoid bug with removing
for($i = $arg_elements->length - 1; $i >= 0; $i--){

// get argument
$arg_element = $arg_elements->item($i);

// add item to args
$name = $arg_element->getAttribute('name');
$value = $arg_element->nodeValue;

$this->addToSmartList($args, $name, $value);

// remove element
$arg_element->parentNode->removeChild($arg_element);
}

// use element id attribute to load args
Expand Down

0 comments on commit 9d6f5b8

Please sign in to comment.