Passed
Push — master ( b47dcf...3f2121 )
by Aleksandr
04:26 queued 49s
created

Classifier::getReferenceBookById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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