Model   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 86.44%

Importance

Changes 11
Bugs 0 Features 0
Metric Value
wmc 30
eloc 49
c 11
b 0
f 0
dl 0
loc 152
ccs 51
cts 59
cp 0.8644
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A propertyAliases() 0 6 1
A getIdSuffix() 0 3 1
A __construct() 0 6 2
A getClearId() 0 3 1
A loadXml() 0 4 1
A getPropertyAlias() 0 14 4
A xpath() 0 13 6
A __set() 0 2 1
A init() 0 3 1
A __isset() 0 2 1
A registerNamespace() 0 6 5
A __get() 0 18 6
1
<?php namespace Zenwalker\CommerceML\ORM;
2
3
use Zenwalker\CommerceML\CommerceML;
4
use Zenwalker\CommerceML\Model\Simple;
5
6
/**
7
 * Class Model
8
 *
9
 * @package Zenwalker\CommerceML\ORM
10
 * @property string id
11
 * @property string name
12
 * @property string value
13
 */
14
abstract class Model extends \ArrayObject
15
{
16
    private $namespaceRegistered = false;
17
18
    /**
19
     * @var CommerceML
20
     */
21
    public $owner;
22
    /**
23
     * @var \SimpleXMLElement
24
     */
25
    public $xml;
26
27
    /**
28
     * @return array
29
     */
30 16
    public function propertyAliases()
31
    {
32
        return [
33 16
            'Ид' => 'id',
34
            'Наименование' => 'name',
35
            'Значение' => 'value',
36
        ];
37
    }
38
39
    /**
40
     * @return string
41
     */
42 6
    public function getClearId()
43
    {
44 6
        return (string)explode('#', $this->id)[0];
45
    }
46
47
    /**
48
     * @return string
49
     */
50 1
    public function getIdSuffix()
51
    {
52 1
        return (string)\array_slice(explode('#', (string)$this->id), 1)[0];
53
    }
54
55
    /**
56
     * Model constructor.
57
     *
58
     * @param CommerceML $owner
59
     * @param \SimpleXMLElement|null $xml
60
     */
61 33
    public function __construct(CommerceML $owner, \SimpleXMLElement $xml = null)
62
    {
63 33
        $this->owner = $owner;
64 33
        $this->xml = $xml ?: $this->loadXml();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->loadXml() targeting Zenwalker\CommerceML\ORM\Model::loadXml() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
65 33
        $this->init();
66 33
        parent::__construct();
67 33
    }
68
69
    /**
70
     * @param $name
71
     * @return null|string
72
     */
73 16
    protected function getPropertyAlias($name)
74
    {
75 16
        $attributes = $this->xml;
76 16
        $aliases = $this->propertyAliases();
77 16
        while ($idx = array_search($name, $aliases)) {
78 16
            if (isset($attributes[$idx])) {
79 2
                return trim((string)$attributes[$idx]);
80
            }
81 16
            if (isset($this->xml->{$idx})) {
82 16
                return trim((string)$this->xml->{$idx});
83
            }
84 2
            unset($aliases[$idx]);
85
        }
86 1
        return null;
87
    }
88
89
    /**
90
     * @param $name
91
     * @return mixed|null|\SimpleXMLElement|string
92
     */
93 18
    public function __get($name)
94
    {
95 18
        if (method_exists($this, $method = 'get' . ucfirst($name))) {
96 11
            return \call_user_func([$this, $method]);
97
        }
98 17
        if ($this->xml) {
99 17
            $attributes = $this->xml;
100 17
            if (isset($attributes[$name])) {
101 1
                return trim((string)$attributes[$name]);
102
            }
103 17
            if ($value = $this->xml->{$name}) {
104 3
                return $value;
105
            }
106 16
            if (($value = $this->getPropertyAlias($name)) !== null) {
107 16
                return $value;
108
            }
109
        }
110 1
        return null;
111
    }
112
113
    public function __set($name, $value)
114
    {
115
    }
116
117
    public function __isset($name)
118
    {
119
    }
120
121
    public function loadXml()
122
    {
123
        $this->registerNamespace();
124
        return null;
125
    }
126
127 33
    public function init()
128
    {
129 33
        $this->registerNamespace();
130 33
    }
131
132 33
    protected function registerNamespace()
133
    {
134 33
        if ($this->xml && !$this->namespaceRegistered && ($namespaces = $this->xml->getNamespaces())) {
135 31
            $this->namespaceRegistered = true;
136 31
            foreach ($namespaces as $namespace) {
137 31
                $this->xml->registerXPathNamespace('c', $namespace);
138
            }
139
        }
140 33
    }
141
142
    /**
143
     * Лучше использовать данный метод, вместо стандартного xpath у SimpleXMLElement,
144
     * т.к. есть проблемы с неймспейсами xmlns
145
     *
146
     * Для каждого элемента необходимо указывать наймспейс "c", например:
147
     * //c:Свойство/c:ВариантыЗначений/c:Справочник[c:ИдЗначения = ':параметр']
148
     *
149
     * @param string $path
150
     * @param array $args - Аргументы задаём в бинд стиле ['параметр'=>'значение'] без двоеточия
151
     * @return \SimpleXMLElement[]
152
     */
153 12
    public function xpath($path, $args = [])
154
    {
155 12
        $this->registerNamespace();
156 12
        if (!$this->namespaceRegistered) {
157
            $path = str_replace('c:', '', $path);
158
        }
159 12
        if (!empty($args) && \is_array($args)) {
160 11
            foreach ($args as $ka => $kv) {
161 11
                $replace = (false !== strpos($kv, "'") ? ("concat('" . str_replace("'", "',\"'\",'", $kv) . "')") : "'" . $kv . "'");
162 11
                $path = str_replace(':' . $ka, $replace, $path);
163
            }
164
        }
165 12
        return $this->xml->xpath($path);
166
    }
167
}
168