1
|
|
|
<?php namespace Zenwalker\CommerceML\ORM; |
2
|
|
|
|
3
|
|
|
use Zenwalker\CommerceML\CommerceML; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Model |
7
|
|
|
* |
8
|
|
|
* @package Zenwalker\CommerceML\ORM |
9
|
|
|
* @property string id |
10
|
|
|
* @property string name |
11
|
|
|
* @property string value |
12
|
|
|
*/ |
13
|
|
|
abstract class Model extends \ArrayObject |
14
|
|
|
{ |
15
|
|
|
private $namespaceRegistered = false; |
16
|
|
|
|
17
|
4 |
|
public function defaultProperties() |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
4 |
|
'Ид' => 'id', |
21
|
|
|
'Наименование' => 'name', |
22
|
|
|
'Значение' => 'value', |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public function getClearId() |
27
|
|
|
{ |
28
|
1 |
|
return explode('#', $this->id)[0]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getIdSuffix() |
32
|
|
|
{ |
33
|
|
|
return array_slice(explode('#', (string)$this->id), 1)[0]; |
34
|
|
|
} |
35
|
|
|
|
36
|
14 |
|
public function __construct(CommerceML $owner, \SimpleXMLElement $xml = null) |
37
|
|
|
{ |
38
|
14 |
|
$this->owner = $owner; |
39
|
14 |
|
$this->xml = $xml ?: $this->loadXml(); |
|
|
|
|
40
|
14 |
|
$this->init(); |
41
|
14 |
|
parent::__construct(); |
42
|
14 |
|
} |
43
|
|
|
|
44
|
6 |
|
public function __get($name) |
45
|
|
|
{ |
46
|
6 |
|
if (method_exists($this, $method = 'get' . ucfirst($name))) { |
47
|
3 |
|
return call_user_func([$this, $method]); |
48
|
|
|
} |
49
|
5 |
|
if ($this->xml) { |
50
|
5 |
|
$attributes = $this->xml; |
51
|
5 |
|
if (isset($attributes[$name])) { |
52
|
|
|
return (string)$attributes[$name]; |
53
|
|
|
} |
54
|
5 |
|
if ($value = $this->xml->{$name}) { |
55
|
1 |
|
return $value; |
56
|
|
|
} |
57
|
4 |
|
if ($idx = array_search($name, $this->defaultProperties())) { |
58
|
4 |
|
return (string)$this->xml->{$idx}; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function __set($name, $value) |
65
|
|
|
{ |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function __isset($name) |
69
|
|
|
{ |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function loadXml() |
73
|
|
|
{ |
74
|
|
|
$this->registerNamespace(); |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var CommerceML |
80
|
|
|
*/ |
81
|
|
|
public $owner; |
82
|
|
|
/** |
83
|
|
|
* @var \SimpleXMLElement |
84
|
|
|
*/ |
85
|
|
|
public $xml; |
86
|
|
|
|
87
|
14 |
|
public function init() |
88
|
|
|
{ |
89
|
14 |
|
$this->registerNamespace(); |
90
|
14 |
|
} |
91
|
|
|
|
92
|
14 |
|
protected function registerNamespace() |
93
|
|
|
{ |
94
|
14 |
|
if ($this->xml && !$this->namespaceRegistered && ($namespaces = $this->xml->getNamespaces())) { |
95
|
12 |
|
$this->namespaceRegistered = true; |
96
|
12 |
|
foreach ($namespaces as $namespace) { |
97
|
12 |
|
$this->xml->registerXPathNamespace('c', $namespace); |
98
|
|
|
} |
99
|
|
|
} |
100
|
14 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Лучше использовать данный метод, вместо стандартного xpath у SimpleXMLElement, |
104
|
|
|
* т.к. есть проблемы с неймспейсами xmlns |
105
|
|
|
* |
106
|
|
|
* Для каждого элемента необходимо указывать наймспейс "c", например: |
107
|
|
|
* //c:Свойство/c:ВариантыЗначений/c:Справочник[c:ИдЗначения = '{$id}'] |
108
|
|
|
* |
109
|
|
|
* @param string $path |
110
|
|
|
* @return \SimpleXMLElement[] |
111
|
|
|
*/ |
112
|
|
|
public function xpath($path) |
113
|
|
|
{ |
114
|
|
|
$this->registerNamespace(); |
115
|
|
|
if (!$this->namespaceRegistered) { |
116
|
|
|
$path = str_replace('c:', '', $path); |
117
|
|
|
} |
118
|
|
|
return $this->xml->xpath($path); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.