Passed
Push — master ( 3ff747...952456 )
by Aleksandr
01:46
created

Classifier::getReferenceBookValueById()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
4
namespace Zenwalker\CommerceML\Model;
5
6
7
use Zenwalker\CommerceML\Collections\PropertyCollection;
8
9
/**
10
 * Class Classifier
11
 *
12
 * @package Zenwalker\CommerceML\Model
13
 * @property PropertyCollection properties
14
 * @property Group[] groups
15
 */
16
class Classifier extends Simple
17
{
18
    /**
19
     * @var Group[]
20
     */
21
    protected $groups = [];
22
    /**
23
     * @var PropertyCollection
24
     */
25
    protected $properties;
26
27
    /**
28
     * @return null|\SimpleXMLElement
29
     */
30 30
    public function loadXml()
31
    {
32 30
        if ($this->owner->importXml && $this->owner->importXml->Классификатор) {
33 30
            return $this->owner->importXml->Классификатор;
34
        }
35
36
        return null;
37
    }
38
39
    /**
40
     * @param $id
41
     * @return \SimpleXMLElement[]
42
     */
43 4
    public function getReferenceBookById($id)
44
    {
45 4
        return $this->xpath('//c:Свойство[c:Ид = :id]/c:ВариантыЗначений/c:Справочник', ['id' => $id]);
46
    }
47
48
    /**
49
     * @param $id
50
     * @return null|\SimpleXMLElement
51
     */
52 6
    public function getReferenceBookValueById($id)
53
    {
54 6
        if ($id) {
55 5
            $xpath = '//c:Свойство/c:ВариантыЗначений/c:Справочник[c:ИдЗначения = :id]';
56 5
            $type = $this->xpath($xpath, ['id' => $id]);
57 5
            return $type ? $type[0] : null;
58
        }
59
60 1
        return null;
61
    }
62
63
    /**
64
     * @param $id
65
     * @return null|Group
66
     */
67 5
    public function getGroupById($id)
68
    {
69 5
        foreach ($this->getGroups() as $group) {
70 5
            if ($group->id === $id) {
71 1
                return $group;
72
            }
73
74 5
            if ($child = $group->getChildById($id)) {
75 2
                return $child;
76
            }
77
        }
78 2
        return null;
79
    }
80
81
    /**
82
     * @return PropertyCollection
83
     */
84 2
    public function getProperties()
85
    {
86 2
        if (!$this->properties) {
87 2
            $this->properties = new PropertyCollection($this->owner, $this->xml->Свойства);
88
        }
89 2
        return $this->properties;
90
    }
91
92
    /**
93
     * @return Group[]
94
     */
95 5
    public function getGroups()
96
    {
97 5
        if (empty($this->groups)) {
98 5
            foreach ($this->xml->Группы->Группа as $group) {
99 5
                $this->groups[] = new Group($this->owner, $group);
100
            }
101
        }
102 5
        return $this->groups;
103
    }
104
}
105