|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace carono\exchange1c\helpers; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
|
|
9
|
|
|
class NodeHelper |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param \SimpleXMLElement $parent |
|
13
|
|
|
* @param \SimpleXMLElement $child |
|
14
|
|
|
* |
|
15
|
|
|
* @return \SimpleXMLElement |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function appendNode(\SimpleXMLElement $parent, \SimpleXMLElement $child) |
|
18
|
|
|
{ |
|
19
|
|
|
$parentDom = dom_import_simplexml($parent); |
|
20
|
|
|
$childDom = dom_import_simplexml($child); |
|
21
|
|
|
$childDom = $parentDom->ownerDocument->importNode($childDom, true); |
|
22
|
|
|
$parentDom->appendChild($childDom); |
|
23
|
|
|
return $child; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param \SimpleXMLElement $xml |
|
28
|
|
|
* @param $model |
|
29
|
|
|
* @param $field1c |
|
30
|
|
|
* @param $attribute |
|
31
|
|
|
* @return int|\SimpleXMLElement|string |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function addChild($xml, $model, $field1c, $attribute) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($attribute instanceof \Closure) { |
|
36
|
|
|
return self::addChild($xml, $model, $field1c, call_user_func($attribute, $model)); |
|
37
|
|
|
} elseif (is_string($attribute) && isset($model->{$attribute})) { |
|
38
|
|
|
return self::addChild($xml, $model, $field1c, $model->{$attribute}); |
|
39
|
|
|
} elseif (is_array($attribute)) { |
|
40
|
|
|
return self::addArrayChild($xml, $model, $field1c, $attribute); |
|
41
|
|
|
} elseif ($attribute instanceof \SimpleXMLElement) { |
|
42
|
|
|
return self::appendNode($xml, $attribute); |
|
43
|
|
|
} else { |
|
44
|
|
|
return $xml->addChild($field1c, $attribute); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param \SimpleXMLElement $xml |
|
50
|
|
|
* @param $model |
|
51
|
|
|
* @param $field1c |
|
52
|
|
|
* @param $attribute |
|
53
|
|
|
* @return int|\SimpleXMLElement|string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected static function addArrayChild($xml, $model, $field1c, $attribute) |
|
56
|
|
|
{ |
|
57
|
|
|
$array = $attribute; |
|
58
|
|
|
$content = ArrayHelper::remove($array, '@content', ''); |
|
59
|
|
|
$field1c = ArrayHelper::remove($array, '@name', $field1c); |
|
60
|
|
|
$attributes = ArrayHelper::remove($array, '@attributes', []); |
|
61
|
|
|
if (is_array($content)) { |
|
62
|
|
|
$item = self::addChild($xml, $model, $field1c, $content); |
|
63
|
|
|
return $item; |
|
64
|
|
|
} else { |
|
65
|
|
|
$item = new \SimpleXMLElement("<{$field1c}>$content</{$field1c}>"); |
|
66
|
|
|
foreach ($attributes as $name => $value) { |
|
67
|
|
|
$item->addAttribute($name, $value); |
|
68
|
|
|
} |
|
69
|
|
|
foreach ($array as $field => $value) { |
|
70
|
|
|
self::addChild($item, $model, $field, $value); |
|
71
|
|
|
} |
|
72
|
|
|
return self::appendNode($xml, $item); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |