Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/main/php/com/calidos/dani/php/XPathSimpleFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ static public function asXML($structure, $options = XPathSimpleFilter::CDATAWRAP
* @param array $structure
* @return xml structure as a string
*/
static public function asSimpleXML($structure) {
static public function asSimpleXML($structure, $options = XPathSimpleFilter::CDATAWRAP) {

$outXmlString_ = XPathSimpleFilter::asXML($structure);
$outSimpleXml_ = simplexml_load_string($outXmlString_);
$outXmlString_ = XPathSimpleFilter::asXML($structure, $options);
$outSimpleXml_ = simplexml_load_string($outXmlString_, "SimpleXMLElement", LIBXML_NOERROR | LIBXML_NOWARNING);

return $outSimpleXml_;

Expand Down Expand Up @@ -186,6 +186,11 @@ static private function applyXPathToXml($xml, $a, &$provisionalKeys) {
} else {
// base case
$content_ = $xml->xpath($xpath_);

// Fix when 1 element is returned as SimpleXMLElement and we want the node content
if (is_array($content_) && count($content_) === 1 ){
$content_ = $content_[0]->__toString();
}
}

}
Expand Down Expand Up @@ -231,9 +236,9 @@ static private function flattenGivenProvisionalKeys($structure, $provisionalKeys

// keys, but still flatten single strings if needed
$outFlattened_ = array();
foreach ($structure as $k_ => $structure) {
foreach ($structure as $k_ => $structure_) {
$key_ = $k_;
$flattenedDataForKey_ = XPathSimpleFilter::flattenAsNeeded($structure);
$flattenedDataForKey_ = XPathSimpleFilter::flattenAsNeeded($structure_);
$outFlattened_[$key_] = $flattenedDataForKey_;

}
Expand Down Expand Up @@ -286,7 +291,7 @@ static private function getProvisionalKey($key, $filter, &$provisionalKeys) {


static private function isRegisteredAsProvisionalKey($key, $provisionalKeys) {
return key_exists($key, $provisionalKeys);
return array_key_exists($key, $provisionalKeys);
}


Expand All @@ -306,7 +311,7 @@ static private function getXPathFromFilter($filter) {

static private function addContent($current, $key, $content) {

if (key_exists($key, $current)) {
if (array_key_exists($key, $current)) {

$currentContentBucket_ = $current[$key];
$currentContentBucket_ = XPathSimpleFilter::prepareForMergeOntoArray($currentContentBucket_);
Expand Down Expand Up @@ -349,7 +354,7 @@ static private function flattenAsNeeded($content) {
}
}
} elseif (is_a($content, 'SimpleXMLElement')) {
if (key_exists('@attributes', $content)) { // flatten attribute
if (array_key_exists('@attributes', $content)) { // flatten attribute
//$content[@attributes] = ['name_of_the_attribute' => 'value' ]
$flattened_ = end($content);
if (isset($flattened_)) {
Expand All @@ -360,8 +365,8 @@ static private function flattenAsNeeded($content) {
}
} else { // flatten xml node (if it has no children)
if ($content->count()===0) {
return end($content);
} else {
return $content->__toString(); // FIXED: return end($content);
} else {
return $content; // don't flatten otherwise it'd only return the children
}
}
Expand Down