-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
agoradesign
committed
Apr 2, 2016
1 parent
93933cb
commit b6b85b8
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Agoradesign\DOMXPathIterator; | ||
|
||
class DOMXpathIterator implements \Iterator { | ||
|
||
/** | ||
* The DOMDocument we are iterating. | ||
* | ||
* @var \DOMDocument | ||
*/ | ||
protected $document; | ||
|
||
protected $filterExpression; | ||
|
||
/** | ||
* @var \DOMNodeList[] | ||
*/ | ||
protected $nodeList; | ||
|
||
protected $index; | ||
|
||
public function __construct(\DOMDocument $document, $filter_expression) { | ||
$this->index = 0; | ||
$this->document = $document; | ||
$this->filterExpression = $filter_expression; | ||
$this->init(); | ||
} | ||
|
||
protected function init() { | ||
$xpath = new \DOMXPath($this->document); | ||
$this->nodeList = $xpath->query($this->filterExpression); | ||
} | ||
|
||
/** | ||
* @return \DOMElement | ||
*/ | ||
public function current() { | ||
return $this->nodeList->item($this->index); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function key() { | ||
return $this->index; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function next() { | ||
$this->index++; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rewind() { | ||
$this->index = 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function valid() { | ||
return isset($this->nodeList[$this->index]); | ||
} | ||
|
||
/** | ||
* @return \DOMDocument | ||
*/ | ||
public function getDocument() { | ||
return $this->document; | ||
} | ||
|
||
public function count() { | ||
return $this->nodeList->length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Agoradesign\DOMXPathIterator; | ||
|
||
class ValueExtractingIterator implements \OuterIterator { | ||
|
||
private $innerIterator; | ||
|
||
protected $childrenXpathExpression; | ||
|
||
protected $resultKeyXpathExpression; | ||
|
||
protected $resultValueXpathExpression; | ||
|
||
protected $virtualProperties = []; | ||
|
||
public function __construct(DefaultIterator $xpath_iterator, $children_xpath_expresion, $result_key_xpath_expression, $result_value_xpath_expression) { | ||
$this->innerIterator = $xpath_iterator; | ||
$this->childrenXpathExpression = $children_xpath_expresion; | ||
$this->resultKeyXpathExpression = $result_key_xpath_expression; | ||
$this->resultValueXpathExpression = $result_value_xpath_expression; | ||
} | ||
|
||
/** | ||
* @var $virtualProperties array keyed by source property and an array as value, | ||
* consisting of 'name' (target name of the virtual property) and 'callback', | ||
* a callback function, that will generate the property based on the element's value. | ||
*/ | ||
public function setVirtualProperties(array $virtualProperties) { | ||
$this->virtualProperties = $virtualProperties; | ||
} | ||
|
||
public function getInnerIterator () { | ||
return $this->innerIterator; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function current() { | ||
$result = []; | ||
$parent = $this->innerIterator->current(); | ||
$document = $this->innerIterator->getDocument(); | ||
$xpath = new \DOMXPath($document); | ||
$children = $xpath->query($this->childrenXpathExpression, $parent); | ||
foreach ($children as $child) { | ||
$key = $xpath->evaluate($this->resultKeyXpathExpression, $child); | ||
if (!empty($key)) { | ||
$value = $xpath->evaluate($this->resultValueXpathExpression, $child); | ||
$result[$key] = $value; | ||
} | ||
if (isset($this->virtualProperties[$key])) { | ||
$name = $this->virtualProperties[$key]['name']; | ||
$callback = $this->virtualProperties[$key]['callback']; | ||
$result[$name] = call_user_func($callback, $value); | ||
} | ||
} | ||
return $result; | ||
} | ||
|
||
public function key() { | ||
return $this->innerIterator->key(); | ||
} | ||
|
||
public function next() { | ||
return $this->innerIterator->next(); | ||
} | ||
|
||
public function rewind() { | ||
return $this->innerIterator->rewind(); | ||
} | ||
|
||
public function valid() { | ||
return $this->innerIterator->valid(); | ||
} | ||
|
||
public function count() { | ||
return $this->innerIterator->count(); | ||
} | ||
} |