<?php
namespace MiotApi\Contract\Specification;
use MiotApi\Exception\SpecificationErrorException;
class PropertySpecification extends Specification
{
protected $format;
protected $formatMap = [
'bool',
'uint8',
'uint16',
'uint32',
'int8',
'int16',
'int32',
'int64',
'float',
'string',
];
protected $access;
protected $accessMap = [
'read',
'write',
'notify',
];
protected $valueRange;
protected $valueList;
protected $unit;
protected $unitMap = [
'percentage',
'celsius',
'senconds',
'minutes',
'hours',
'days',
'kelvin',
'pascal',
'arcdegrees',
'rgb',
];
public function init()
{
parent::init();
if ($this->has('format')) {
$format = $this->__get('format');
if (!in_array($format, $this->formatMap)) {
throw new SpecificationErrorException('属性-》数据格式(format)的取值不在合法范围内');
}
$this->format = $format;
}
if ($this->has('access')) {
$access = $this->__get('access');
if (!empty($access)) {
foreach ($access as $item) {
if (!in_array($item, $this->accessMap)) {
throw new SpecificationErrorException('属性-》访问方式(access)的取值不在合法范围内');
}
}
$this->access = $access;
} else {
$this->access = [];
}
}
if ($this->has('value-range')) {
$valueRange = $this->__get('value-range');
$this->valueRange = $valueRange;
}
if ($this->has('value-list')) {
$valueList = $this->__get('value-list');
$this->valueList = $valueList;
}
if ($this->has('unit')) {
$unit = $this->__get('unit');
if (!in_array($unit, $this->unitMap)) {
throw new SpecificationErrorException('属性-》单位(unit)的取值不在合法范围内');
}
$this->unit = $unit;
}
}
public function getFormat()
{
return $this->format;
}
public function getAccess()
{
return $this->access;
}
public function getValueRange()
{
return $this->valueRange;
}
public function getValueList()
{
return $this->valueList;
}
public function getUnit()
{
return $this->unit;
}
}