Yeelight /
miot-api
| 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\Specification\PropertySpecification; |
||||||
| 12 | use MiotApi\Contract\Urn; |
||||||
| 13 | use MiotApi\Exception\SpecificationErrorException; |
||||||
| 14 | use MiotApi\Util\Collection\Arr; |
||||||
| 15 | use MiotApi\Util\Collection\Collection; |
||||||
| 16 | |||||||
| 17 | class Property extends PropertySpecification |
||||||
| 18 | { |
||||||
| 19 | protected $data; |
||||||
| 20 | |||||||
| 21 | /** |
||||||
| 22 | * 实例ID(Instance ID,简称iid). |
||||||
| 23 | * |
||||||
| 24 | * @var |
||||||
| 25 | */ |
||||||
| 26 | protected $iid; |
||||||
| 27 | |||||||
| 28 | /** |
||||||
| 29 | * type对象 |
||||||
| 30 | * |
||||||
| 31 | * @var |
||||||
| 32 | */ |
||||||
| 33 | protected $specification; |
||||||
| 34 | |||||||
| 35 | public function __construct($data = []) |
||||||
| 36 | { |
||||||
| 37 | $this->data = $data; |
||||||
| 38 | $this->collection = new Collection($this->data); |
||||||
| 39 | |||||||
| 40 | $this->init(); |
||||||
| 41 | } |
||||||
| 42 | |||||||
| 43 | public function init() |
||||||
| 44 | { |
||||||
| 45 | $this->iid = $this->collection->get('iid'); |
||||||
| 46 | $this->urn = new Urn($this->collection->get('type')); |
||||||
| 47 | |||||||
| 48 | if ($this->has('format')) { |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 49 | $format = $this->__get('format'); |
||||||
| 50 | if (!in_array($format, $this->formatMap)) { |
||||||
| 51 | throw new SpecificationErrorException('属性-》数据格式(format)的取值不在合法范围内'); |
||||||
| 52 | } |
||||||
| 53 | $this->format = $format; |
||||||
| 54 | } |
||||||
| 55 | |||||||
| 56 | if ($this->has('access')) { |
||||||
| 57 | $access = $this->__get('access'); |
||||||
| 58 | if (!empty($access)) { |
||||||
| 59 | foreach ($access as $item) { |
||||||
| 60 | if (!in_array($item, $this->accessMap)) { |
||||||
| 61 | throw new SpecificationErrorException('属性-》访问方式(access)的取值不在合法范围内'); |
||||||
| 62 | } |
||||||
| 63 | } |
||||||
| 64 | $this->access = $access; |
||||||
| 65 | } else { |
||||||
| 66 | $this->access = []; |
||||||
| 67 | } |
||||||
| 68 | } |
||||||
| 69 | |||||||
| 70 | if ($this->has('value-range')) { |
||||||
| 71 | $valueRange = $this->__get('value-range'); |
||||||
| 72 | $this->valueRange = $valueRange; |
||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | if ($this->has('value-list')) { |
||||||
| 76 | $valueList = $this->__get('value-list'); |
||||||
| 77 | |||||||
| 78 | // TODO 当format为整型,可定义"value-list"的验证 |
||||||
| 79 | $this->valueList = $valueList; |
||||||
| 80 | } |
||||||
| 81 | |||||||
| 82 | if ($this->has('unit')) { |
||||||
| 83 | $unit = $this->__get('unit'); |
||||||
| 84 | if (!in_array($unit, $this->unitMap)) { |
||||||
| 85 | throw new SpecificationErrorException('属性-》单位(unit)的取值不在合法范围内'); |
||||||
| 86 | } |
||||||
| 87 | |||||||
| 88 | // TODO 当format为整型或浮点型,可定义unit值 的验证 |
||||||
| 89 | $this->unit = $unit; |
||||||
| 90 | } |
||||||
| 91 | |||||||
| 92 | $this->specification = new PropertySpecification($this->urn->getBaseUrn()); |
||||||
| 93 | } |
||||||
| 94 | |||||||
| 95 | public function getIid() |
||||||
| 96 | { |
||||||
| 97 | return $this->iid; |
||||||
| 98 | } |
||||||
| 99 | |||||||
| 100 | /** |
||||||
| 101 | * 验证给定的值是否 符合 format. |
||||||
| 102 | * |
||||||
| 103 | * @param $value |
||||||
| 104 | * |
||||||
| 105 | * @return bool |
||||||
| 106 | */ |
||||||
| 107 | public function verify($value) |
||||||
| 108 | { |
||||||
| 109 | if ($this->has('value-range')) { |
||||||
| 110 | $valueRange = $this->get('value-range'); |
||||||
|
0 ignored issues
–
show
The method
get() does not exist on MiotApi\Contract\Instance\Property. 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
Loading history...
|
|||||||
| 111 | if ($value > $valueRange[1] || $value < $valueRange[0]) { |
||||||
| 112 | return false; |
||||||
| 113 | } |
||||||
| 114 | } |
||||||
| 115 | |||||||
| 116 | if ($this->has('value-list')) { |
||||||
| 117 | $valueList = $this->get('value-list'); |
||||||
| 118 | $valueList = Arr::pluck($valueList, 'value'); |
||||||
| 119 | |||||||
| 120 | if (!in_array($value, $valueList)) { |
||||||
| 121 | return false; |
||||||
| 122 | } |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | switch ($this->format) { |
||||||
| 126 | case 'bool': |
||||||
| 127 | return in_array($value, [ |
||||||
| 128 | true, |
||||||
| 129 | false, |
||||||
| 130 | 1, |
||||||
| 131 | 0, |
||||||
| 132 | ]); |
||||||
| 133 | break; |
||||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||||||
| 134 | case 'uint8': |
||||||
| 135 | case 'uint16': |
||||||
| 136 | case 'uint32': |
||||||
| 137 | case 'int8': |
||||||
| 138 | case 'int16': |
||||||
| 139 | case 'int32': |
||||||
| 140 | case 'int64': |
||||||
| 141 | return is_int($value); |
||||||
| 142 | break; |
||||||
| 143 | case 'float': |
||||||
| 144 | return is_float($value); |
||||||
| 145 | break; |
||||||
| 146 | case 'string': |
||||||
| 147 | return is_string($value); |
||||||
| 148 | break; |
||||||
| 149 | } |
||||||
| 150 | } |
||||||
| 151 | |||||||
| 152 | public function canRead() |
||||||
| 153 | { |
||||||
| 154 | return in_array('read', $this->access); |
||||||
| 155 | } |
||||||
| 156 | |||||||
| 157 | public function canWrite() |
||||||
| 158 | { |
||||||
| 159 | return in_array('write', $this->access); |
||||||
| 160 | } |
||||||
| 161 | |||||||
| 162 | public function canNotify() |
||||||
| 163 | { |
||||||
| 164 | return in_array('notify', $this->access); |
||||||
| 165 | } |
||||||
| 166 | |||||||
| 167 | public function getSpecification() |
||||||
| 168 | { |
||||||
| 169 | return $this->specification; |
||||||
| 170 | } |
||||||
| 171 | |||||||
| 172 | public function getSpecificationContext() |
||||||
| 173 | { |
||||||
| 174 | return $this->specification->toContext(); |
||||||
| 175 | } |
||||||
| 176 | } |
||||||
| 177 |