Passed
Push — master ( 01a1a8...191f59 )
by Aleksandr
01:35
created

Model::__get()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.5625

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 9
cts 12
cp 0.75
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6.5625
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 2
    public function defaultProperties()
18
    {
19
        return [
20 2
            'Ид' => 'id',
21
            'Наименование' => 'name',
22
            'Значение' => 'value',
23
        ];
24
    }
25
26
    public function getClearId()
27
    {
28
        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 11
    public function __construct(CommerceML $owner, \SimpleXMLElement $xml = null)
37
    {
38 11
        $this->owner = $owner;
39 11
        $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...
40 11
        $this->init();
41 11
        parent::__construct();
42 11
    }
43
44 3
    public function __get($name)
45
    {
46 3
        if (method_exists($this, $method = 'get' . ucfirst($name))) {
47
            return call_user_func([$this, $method]);
48
        }
49 3
        if ($this->xml) {
50 3
            $attributes = $this->xml;
51 3
            if (isset($attributes[$name])) {
52
                return (string)$attributes[$name];
53
            }
54 3
            if ($value = $this->xml->{$name}) {
55 1
                return $value;
56
            }
57 2
            if ($idx = array_search($name, $this->defaultProperties())) {
58 2
                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 11
    public function init()
88
    {
89 11
        $this->registerNamespace();
90 11
    }
91
92 11
    protected function registerNamespace()
93
    {
94 11
        if ($this->xml && !$this->namespaceRegistered && ($namespaces = $this->xml->getNamespaces())) {
95 9
            $this->namespaceRegistered = true;
96 9
            foreach ($namespaces as $namespace) {
97 9
                $this->xml->registerXPathNamespace('c', $namespace);
98
            }
99
        }
100 11
    }
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