Passed
Push — master ( 952456...98ea30 )
by Aleksandr
01:40
created

Model::__get()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 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 15
    public function propertyAliases()
31
    {
32
        return [
33 15
            'Ид' => 'id',
34
            'Наименование' => 'name',
35
            'Значение' => 'value',
36
        ];
37
    }
38
39
    /**
40
     * @return string
41
     */
42 5
    public function getClearId()
43
    {
44 5
        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 32
    public function __construct(CommerceML $owner, \SimpleXMLElement $xml = null)
62
    {
63 32
        $this->owner = $owner;
64 32
        $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 32
        $this->init();
66 32
        parent::__construct();
67 32
    }
68
69
    /**
70
     * @param $name
71
     * @return null|string
72
     */
73 15
    protected function getPropertyAlias($name)
74
    {
75 15
        $attributes = $this->xml;
76 15
        if ($idx = array_search($name, $this->propertyAliases())) {
77 15
            if (isset($attributes[$idx])) {
78 1
                return trim((string)$attributes[$idx]);
79
            }
80 15
            return trim((string)$this->xml->{$idx});
81
        }
82 1
        return null;
83
    }
84
85
    /**
86
     * @param $name
87
     * @return mixed|null|\SimpleXMLElement|string
88
     */
89 17
    public function __get($name)
90
    {
91 17
        if (method_exists($this, $method = 'get' . ucfirst($name))) {
92 10
            return \call_user_func([$this, $method]);
93
        }
94 16
        if ($this->xml) {
95 16
            $attributes = $this->xml;
96 16
            if (isset($attributes[$name])) {
97 1
                return trim((string)$attributes[$name]);
98
            }
99 16
            if ($value = $this->xml->{$name}) {
100 3
                return $value;
101
            }
102 15
            if (($value = $this->getPropertyAlias($name)) !== null) {
103 15
                return $value;
104
            }
105
        }
106 1
        return null;
107
    }
108
109
    public function __set($name, $value)
110
    {
111
    }
112
113
    public function __isset($name)
114
    {
115
    }
116
117
    public function loadXml()
118
    {
119
        $this->registerNamespace();
120
        return null;
121
    }
122
123 32
    public function init()
124
    {
125 32
        $this->registerNamespace();
126 32
    }
127
128 32
    protected function registerNamespace()
129
    {
130 32
        if ($this->xml && !$this->namespaceRegistered && ($namespaces = $this->xml->getNamespaces())) {
131 30
            $this->namespaceRegistered = true;
132 30
            foreach ($namespaces as $namespace) {
133 30
                $this->xml->registerXPathNamespace('c', $namespace);
134
            }
135
        }
136 32
    }
137
138
    /**
139
     * Лучше использовать данный метод, вместо стандартного xpath у SimpleXMLElement,
140
     * т.к. есть проблемы с неймспейсами xmlns
141
     *
142
     * Для каждого элемента необходимо указывать наймспейс "c", например:
143
     * //c:Свойство/c:ВариантыЗначений/c:Справочник[c:ИдЗначения = ':параметр']
144
     *
145
     * @param string $path
146
     * @param array $args - Аргументы задаём в бинд стиле ['параметр'=>'значение'] без двоеточия
147
     * @return \SimpleXMLElement[]
148
     */
149 11
    public function xpath($path, $args = [])
150
    {
151 11
        $this->registerNamespace();
152 11
        if (!$this->namespaceRegistered) {
153
            $path = str_replace('c:', '', $path);
154
        }
155 11
        if (!empty($args) && \is_array($args)) {
156 8
            foreach ($args as $ka => $kv) {
157 8
                $replace = (false !== strpos($kv, "'") ? ("concat('" . str_replace("'", "',\"'\",'", $kv) . "')") : "'" . $kv . "'");
158 8
                $path = str_replace(':' . $ka, $replace, $path);
159
            }
160
        }
161 11
        return $this->xml->xpath($path);
162
    }
163
}
164