SimpleXML   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 3 Features 1
Metric Value
c 9
b 3
f 1
dl 0
loc 171
wmc 21
lcom 1
cbo 3
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCategories() 0 14 2
B getOffers() 0 26 6
A getCurrencies() 0 15 2
A countOffers() 0 9 2
A open() 0 6 1
A parseParamsFromElement() 0 14 3
A parsePicturesFromElement() 0 12 3
A getElementAttributes() 0 9 2
1
<?php
2
3
/**
4
 * @author Serkin Alexander <[email protected]>
5
 * @license https://github.com/serkin/ymlparser/LICENSE MIT
6
 */
7
8
namespace YMLParser\Driver;
9
10
class SimpleXML implements DriverInterface {
11
12
    /**
13
     * @var \SimpleXMLElement
14
     */
15
    private $xml;
16
17
    /**
18
     * Gets categories.
19
     *
20
     * @return arry Array of \YMLParser\Node\Category instances or empty array
21
     */
22
    public function getCategories() {
23
        $returnArr = [];
24
25
        foreach ($this->xml->shop->categories->category as $category):
26
27
            $arr = array_merge(
28
                    ['value' => (string) $category], $this->getElementAttributes($category));
29
30
            $returnArr[] = new \YMLParser\Node\Category($arr);
31
32
        endforeach;
33
34
        return $returnArr;
35
    }
36
37
    /**
38
     * Gets offers.
39
     *
40
     * @param \Closure $filter Anonymous function
41
     *
42
     * @return \Iterator Array of of \YMLParser\Node\Offer instances or empty array
43
     */
44
    public function getOffers(\Closure $filter = null) {
45
        foreach ($this->xml->shop->offers->offer as $offer) {
46
47
            $arr = $this->getElementAttributes($offer);
48
            $arr['params'] = $this->parseParamsFromElement($offer);
49
            $arr['pictures'] = $this->parsePicturesFromElement($offer);
50
51
            foreach ($offer->children() as $element) {
52
                $name = mb_strtolower($element->getName());
53
54
                if ($name != 'param') {
55
                    $arr[$name] = (string) $element;
56
                }
57
            }
58
59
            $returnValue = new \YMLParser\Node\Offer($arr);
60
61
            if (!is_null($filter)) {
62
                if ($filter($returnValue)) {
63
                    yield $returnValue;
64
                }
65
            } else {
66
                yield $returnValue;
67
            }
68
        }
69
    }
70
71
    /**
72
     * Gets currencies.
73
     *
74
     * @return array
75
     */
76
    public function getCurrencies() {
77
78
        $returnArr = [];
79
80
        foreach ($this->xml->shop->currencies->currency as $category):
81
82
            $arr = array_merge(
83
                    ['value' => (string) $category], $this->getElementAttributes($category));
84
85
            $returnArr[] = new \YMLParser\Node\Currency($arr);
86
87
        endforeach;
88
89
        return $returnArr;
90
    }
91
92
    /**
93
     * Gets amount of offers.
94
     *
95
     * @return int
96
     */
97
    public function countOffers(\Closure $filter = null) {
98
        $returnValue = 0;
99
100
        foreach ($this->getOffers($filter) as $el):
101
            $returnValue++;
102
        endforeach;
103
104
        return $returnValue;
105
    }
106
107
    /**
108
     * Opens filename.
109
     *
110
     * @param string $filename
111
     *
112
     * @return bool
113
     */
114
    public function open($filename) {
115
116
        $this->xml = simplexml_load_file($filename);
117
118
        return (bool) $this->xml;
119
    }
120
121
    /**
122
     * Gets element params.
123
     *
124
     * @param \SimpleXMLElement $offer
125
     *
126
     * @return array
127
     */
128
    private function parseParamsFromElement(\SimpleXMLElement $offer) {
129
        $returnArr = [];
130
131
        foreach ($offer->children() as $element) {
132
133
            if (mb_strtolower($element->getName()) == 'param') {
134
                $returnArr[] = array_merge(
135
                        ['value' => (string) $element], $this->getElementAttributes($element)
136
                );
137
            }
138
        }
139
140
        return $returnArr;
141
    }
142
143
    /**
144
     * Gets element pictures.
145
     *
146
     * @param \SimpleXMLElement $offer
147
     *
148
     * @return array
149
     */
150
    private function parsePicturesFromElement(\SimpleXMLElement $offer) {
151
        $returnArr = [];
152
153
        foreach ($offer->children() as $element) {
154
155
            if (mb_strtolower($element->getName()) == 'picture') {
156
                $returnArr[] = (string) $element;
157
            }
158
        }
159
160
        return $returnArr;
161
    }
162
163
    /**
164
     * Gets lement attributes.
165
     *
166
     * @param \SimpleXMLElement $element
167
     *
168
     * @return array
169
     */
170
    private function getElementAttributes(\SimpleXMLElement $element) {
171
        $returnArr = [];
172
173
        foreach ($element->attributes() as $attrName => $attrValue):
174
            $returnArr[strtolower($attrName)] = (string) $attrValue;
175
        endforeach;
176
177
        return $returnArr;
178
    }
179
180
}
181