Passed
Push — master ( 49f4cf...71f366 )
by Aleksandr
01:07
created

PropertyCollection::init()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 0
1
<?php
2
3
4
namespace Zenwalker\CommerceML\Model;
5
6
7
/**
8
 * Class ValueProperties
9
 *
10
 * @package Zenwalker\CommerceML\Model
11
 */
12
class PropertyCollection extends Simple
13
{
14
    /**
15
     * @param $id
16
     * @return Property|null
17
     */
18
    public function getById($id)
19
    {
20
        foreach ($this as $property) {
21
            if ($property->id == (string)$id) {
22
                return $property;
23
            }
24
        }
25
        return null;
26
    }
27
28
    protected function loadPropertiesValue()
29
    {
30
        foreach ($this->xml->ЗначенияСвойства as $property) {
31
            $properties = $this->owner->classifier->getProperties();
32
            $object = clone $properties->getById($property->Ид);
33
            $object->productId = (string)$this->xpath('..')[0]->Ид;
34
            $object->init();
35
            $this->append($object);
36
        }
37
    }
38
39
    protected function loadProperties()
40
    {
41
        foreach ($this->xml->Свойство as $property) {
42
            $this->append(new Property($this->owner, $property));
43
        }
44
    }
45
46
    public function init()
47
    {
48
        if (isset($this->xml->ЗначенияСвойства)) {
49
            $this->loadPropertiesValue();
50
        }
51
        if (isset($this->xml->Свойство)) {
52
            $this->loadProperties();
53
        }
54
        parent::init();
55
    }
56
}