Skip to content

Commit

Permalink
add the code to support 3 levels of array inside the xml, so it will …
Browse files Browse the repository at this point in the history
…be somthig like that 'product' => ['uses' => 'T_NEW_CATALOG.PRODUCT[KNOTEN_ID>node_id,NAME>name,PRODUCT_DETAILS{DESCRIPTION_SHORT>short,EAN>ean}>details]']
  • Loading branch information
Ahmed Bermawy committed Mar 29, 2018
1 parent 1597072 commit d31f763
Showing 1 changed file with 63 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/Xml/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ protected function getValueCollection(SimpleXMLElement $content, array $matches,
$collection = data_get($content, $parent);
$namespaces = $this->getAvailableNamespaces();

$uses = explode(',', $matches[2]);
//$uses = explode(',', $matches[2]);
// use preg_split to split all comma ignoring
// the commas inside curly brackets
$uses = preg_split('/(,)(?=(?:[^}]|{[^{]*})*$)/',$matches[2]);
$values = [];

if (! $collection instanceof SimpleXMLElement) {
Expand Down Expand Up @@ -198,32 +201,86 @@ protected function parseValueCollection(SimpleXMLElement $content, array $uses)
$value = [];

foreach ($uses as $use) {
list($name, $as) = strpos($use, '>') !== false ? explode('>', $use, 2) : [$use, $use];
if (preg_match("/^(.*)\{(.*)\}(\>(.*)|)/", $use, $output_array))
{
$thirdArrayName = $output_array[1];
$thirdArrayData = $output_array[2];
$thirdArrayOutputName = (isset($output_array[4]))? $output_array[4]:$thirdArrayName;
$usesThird = explode(',', $thirdArrayData);
$value[$thirdArrayOutputName] = $this->parseValueCollectionThird($content,$thirdArrayName,$usesThird);
}
else
{
list($name, $as) = strpos($use, '>') !== false ? explode('>', $use, 2) : [$use, $use];

if (preg_match('/^([A-Za-z0-9_\-\.]+)\((.*)\=(.*)\)$/', $name, $matches)) {
if ($name == $as) {
$as = null;
}

$item = $this->getSelfMatchingValue($content, $matches, $as);

if (is_null($as)) {
$value = array_merge($value, $item);
} else {
Arr::set($value, $as, $item);
}
}
else
{
if ($name == '@') {
$name = null;
}
Arr::set($value, $as, $this->getValue($content, $name));
}
}
}

return $value;
}

/**
* Resolve values by collection for third level.
*
* @param \SimpleXMLElement $content
* @param string $thirdArrayName
* @param array $uses
*
* @return array
*/
protected function parseValueCollectionThird(SimpleXMLElement $content,$thirdArrayName, array $uses)
{
$value = [];

foreach ($uses as $use)
{
list($name, $as) = strpos($use, '>') !== false ? explode('>', $use, 2) : [$use, $use];
if (preg_match('/^([A-Za-z0-9_\-\.]+)\((.*)\=(.*)\)$/', $name, $matches)) {
if ($name == $as) {
$as = null;
}

$item = $this->getSelfMatchingValue($content, $matches, $as);
$item = $this->getSelfMatchingValue($content->{$thirdArrayName}, $matches, $as);

if (is_null($as)) {
$value = array_merge($value, $item);
} else {
Arr::set($value, $as, $item);
}
} else {
}
else
{
if ($name == '@') {
$name = null;
}

Arr::set($value, $as, $this->getValue($content, $name));
Arr::set($value, $as, $this->getValue($content->$thirdArrayName, $name));
}
}

return $value;
}


/**
* Get self matching value.
*
Expand Down

0 comments on commit d31f763

Please sign in to comment.