Passed
Push — master ( 82e9fe...cecb4c )
by Sheldon
04:44
created

Instance::getPropertiesNodes()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 2
nop 0
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sheldon
5
 * Date: 18-6-6
6
 * Time: 下午6:25.
7
 */
8
9
namespace MiotApi\Contract\Instance;
10
11
use MiotApi\Contract\RemoteSpec;
12
use MiotApi\Contract\Specification\DeviceSpecification;
13
use MiotApi\Contract\Specification\Specification;
14
use MiotApi\Util\Collection\Collection;
15
16
class Instance extends Specification
17
{
18
    protected $servicesNode;
19
20
    /**
21
     * type对象
22
     *
23
     * @var
24
     */
25
    protected $specification;
26
27
    /**
28
     * @throws \MiotApi\Exception\SpecificationErrorException
29
     */
30
    public function init()
31
    {
32
        $items = RemoteSpec::instance($this->urn->getExpression());
33
        $this->collection = new Collection($items);
34
        $this->specification = new DeviceSpecification($this->getType());
35
        $this->initServices();
36
    }
37
38
    /**
39
     * @throws \MiotApi\Exception\SpecificationErrorException
40
     */
41
    protected function initServices()
42
    {
43
        if ($this->has('services')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on MiotApi\Contract\Instance\Instance. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        if ($this->/** @scrutinizer ignore-call */ has('services')) {
Loading history...
44
            $services = $this->get('services');
0 ignored issues
show
Bug introduced by
The method get() does not exist on MiotApi\Contract\Instance\Instance. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            /** @scrutinizer ignore-call */ 
45
            $services = $this->get('services');
Loading history...
45
            if (!empty($services)) {
46
                foreach ($services as $service) {
47
                    $this->servicesNode[$service['iid']] = new Service($service);
48
                }
49
            }
50
        }
51
    }
52
53
    /**
54
     * 根据服务的实例id获取服务实例.
55
     *
56
     * @param $siid
57
     *
58
     * @return mixed
59
     */
60
    public function service($siid)
61
    {
62
        return $this->servicesNode[$siid];
63
    }
64
65
    /**
66
     * 根据服务的实例id和属性的实例id获取属性实例.
67
     *
68
     * @param $siid
69
     * @param $piid
70
     *
71
     * @return mixed
72
     */
73
    public function property($siid, $piid)
74
    {
75
        return $this->getPropertiesNode($siid)[$piid];
76
    }
77
78
    /**
79
     * 根据给定的属性名称得到 该属性所在的 sid和pid.
80
     *
81
     * @param $name
82
     *
83
     * @return array
84
     */
85
    public function getSidPidByName($name)
86
    {
87
        $sids = false;
88
        $pids = false;
89
        if (!empty($this->servicesNode)) {
90
            foreach ($this->servicesNode as $service) {
91
                $properties = $this->getPropertiesNode($service->getIid());
92
                if (!empty($properties)) {
93
                    foreach ($properties as $property) {
94
                        if ($property->getUrn()->getName() == $name) {
95
                            $sids[] = $service->getIid();
96
                            $pids[] = $property->getIid();
97
                        }
98
                    }
99
                }
100
            }
101
        }
102
103
        return [
104
            $sids,
105
            $pids,
106
        ];
107
    }
108
109
    /**
110
     * 获取设备的服务实例列表.
111
     *
112
     * @return mixed
113
     */
114
    public function getServicesNode()
115
    {
116
        return $this->servicesNode;
117
    }
118
119
    /**
120
     * 获取所有属性列表.
121
     *
122
     * @param $siid
123
     *
124
     * @return mixed
125
     */
126
    public function getPropertiesNodes()
127
    {
128
        $propertiesNodes = [];
129
        $services = $this->getServicesNode();
130
131
        if (!empty($services)) {
132
            foreach ($services as $index => $service) {
133
                if (!empty($this->getPropertiesNode($index))) {
134
                    foreach ($this->getPropertiesNode($index) as $i => $item) {
135
                        $propertiesNodes[($index . '.' . $i)] = $item;
136
                    }
137
                }
138
            }
139
        }
140
141
        return $propertiesNodes;
142
    }
143
144
    /**
145
     * 根据服务实例id 获取属性列表.
146
     *
147
     * @param $siid
148
     *
149
     * @return mixed
150
     */
151
    public function getPropertiesNode($siid)
152
    {
153
        $service = $this->service($siid);
154
155
        return $service->getPropertiesNode();
156
    }
157
158
    public function getSpecification()
159
    {
160
        return $this->specification;
161
    }
162
163
    public function getSpecificationContext()
164
    {
165
        return $this->specification->toContext();
166
    }
167
}
168