$maxValue or $defaultValue > $maxValue or $defaultValue < $minValue){ throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue"); } return self::$attributes[$id] = new Attribute($id, $name, $minValue, $maxValue, $defaultValue, $shouldSend); } /** * @param int $id * * @return Attribute|null */ public static function getAttribute(int $id){ return isset(self::$attributes[$id]) ? clone self::$attributes[$id] : null; } /** * @param string $name * * @return Attribute|null */ public static function getAttributeByName(string $name){ foreach(self::$attributes as $a){ if($a->getName() === $name){ return clone $a; } } return null; } private function __construct(int $id, string $name, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true){ $this->id = $id; $this->name = $name; $this->minValue = $minValue; $this->maxValue = $maxValue; $this->defaultValue = $defaultValue; $this->shouldSend = $shouldSend; $this->currentValue = $this->defaultValue; } public function getMinValue() : float{ return $this->minValue; } public function setMinValue(float $minValue){ if($minValue > $this->getMaxValue()){ throw new \InvalidArgumentException("Value $minValue is bigger than the maxValue!"); } if($this->minValue != $minValue){ $this->desynchronized = true; $this->minValue = $minValue; } return $this; } public function getMaxValue() : float{ return $this->maxValue; } public function setMaxValue(float $maxValue){ if($maxValue < $this->getMinValue()){ throw new \InvalidArgumentException("Value $maxValue is bigger than the minValue!"); } if($this->maxValue != $maxValue){ $this->desynchronized = true; $this->maxValue = $maxValue; } return $this; } public function getDefaultValue() : float{ return $this->defaultValue; } public function setDefaultValue(float $defaultValue){ if($defaultValue > $this->getMaxValue() or $defaultValue < $this->getMinValue()){ throw new \InvalidArgumentException("Value $defaultValue exceeds the range!"); } if($this->defaultValue !== $defaultValue){ $this->desynchronized = true; $this->defaultValue = $defaultValue; } return $this; } public function resetToDefault(){ $this->setValue($this->getDefaultValue()); } public function getValue() : float{ return $this->currentValue; } /** * @param float $value * @param bool $fit * @param bool $forceSend * * @return $this */ public function setValue(float $value, bool $fit = false, bool $forceSend = false){ if($value > $this->getMaxValue() or $value < $this->getMinValue()){ if(!$fit){ throw new \InvalidArgumentException("Value $value exceeds the range!"); } $value = min(max($value, $this->getMinValue()), $this->getMaxValue()); } if($this->currentValue != $value){ $this->desynchronized = true; $this->currentValue = $value; }elseif($forceSend){ $this->desynchronized = true; } return $this; } public function getName() : string{ return $this->name; } public function getId() : int{ return $this->id; } public function isSyncable() : bool{ return $this->shouldSend; } public function isDesynchronized() : bool{ return $this->shouldSend and $this->desynchronized; } public function markSynchronized(bool $synced = true){ $this->desynchronized = !$synced; } }