Skip to content

Commit

Permalink
Parent child setter&getter for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Feb 10, 2022
1 parent fac0908 commit 5502633
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 33 deletions.
4 changes: 2 additions & 2 deletions fastybird_devices_module/repositories/state.py
Expand Up @@ -171,7 +171,7 @@ def get_by_id(self, property_id: uuid.UUID) -> Optional[IDevicePropertyState]:
raise AttributeError("Device property was not found in registry")

if device_property.parent is not None:
raise AttributeError("Child property can't have state")
return self.__state_repository.get_by_id(property_id=device_property.parent.id)

return self.__state_repository.get_by_id(property_id=property_id)

Expand Down Expand Up @@ -217,6 +217,6 @@ def get_by_id(self, property_id: uuid.UUID) -> Optional[IChannelPropertyState]:
raise AttributeError("Channel property was not found in registry")

if channel_property.parent is not None:
raise AttributeError("Child property can't have state")
return self.__state_repository.get_by_id(property_id=channel_property.parent.id)

return self.__state_repository.get_by_id(property_id=property_id)
194 changes: 194 additions & 0 deletions src/Entities/Channels/Properties/Property.php
Expand Up @@ -18,6 +18,8 @@
use Doctrine\Common;
use Doctrine\ORM\Mapping as ORM;
use FastyBird\DevicesModule\Entities;
use FastyBird\DevicesModule\Exceptions;
use FastyBird\Metadata\Types as MetadataTypes;
use IPub\DoctrineCrud\Mapping\Annotation as IPubDoctrine;
use Ramsey\Uuid;
use Throwable;
Expand Down Expand Up @@ -178,6 +180,198 @@ public function getChannel(): Entities\Channels\IChannel
return $this->channel;
}

/**
* {@inheritDoc}
*/
public function isSettable(): bool
{
if ($this->getParent() !== null) {
return $this->getParent()->isSettable();
}

return parent::isSettable();
}

/**
* {@inheritDoc}
*/
public function setSettable(bool $settable): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Settable setter is allowed only for parent');
}

parent::setSettable($settable);
}

/**
* {@inheritDoc}
*/
public function isQueryable(): bool
{
if ($this->getParent() !== null) {
return $this->getParent()->isQueryable();
}

return parent::isQueryable();
}

/**
* {@inheritDoc}
*/
public function setQueryable(bool $queryable): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Queryable setter is allowed only for parent');
}

parent::setQueryable($queryable);
}

/**
* {@inheritDoc}
*/
public function getDataType(): MetadataTypes\DataTypeType
{
if ($this->getParent() !== null) {
return $this->getParent()->getDataType();
}

return parent::getDataType();
}

/**
* {@inheritDoc}
*/
public function setDataType(MetadataTypes\DataTypeType $dataType): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Data type setter is allowed only for parent');
}

parent::setDataType($dataType);
}

/**
* {@inheritDoc}
*/
public function getUnit(): ?string
{
if ($this->getParent() !== null) {
return $this->getParent()->getUnit();
}

return parent::getUnit();
}

/**
* {@inheritDoc}
*/
public function setUnit(?string $unit): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Value unit setter is allowed only for parent');
}

parent::setUnit($unit);
}

/**
* {@inheritDoc}
*/
public function getFormat(): ?array
{
if ($this->getParent() !== null) {
return $this->getParent()->getFormat();
}

return parent::getFormat();
}

/**
* {@inheritDoc}
*/
public function setFormat($format): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Value format setter is allowed only for parent');
}

parent::setFormat($format);
}

/**
* {@inheritDoc}
*/
public function getInvalid()
{
if ($this->getParent() !== null) {
return $this->getParent()->getInvalid();
}

return parent::getInvalid();
}

/**
* {@inheritDoc}
*/
public function setInvalid(?string $invalid): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Invalid value setter is allowed only for parent');
}

parent::setInvalid($invalid);
}

/**
* {@inheritDoc}
*/
public function getNumberOfDecimals(): ?int
{
if ($this->getParent() !== null) {
return $this->getParent()->getNumberOfDecimals();
}

return parent::getNumberOfDecimals();
}

/**
* {@inheritDoc}
*/
public function setNumberOfDecimals(?int $numberOfDecimals): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Number of decimals setter is allowed only for parent');
}

parent::setNumberOfDecimals($numberOfDecimals);
}

/**
* {@inheritDoc}
*/
public function getDefault()
{
if ($this->getParent() !== null) {
return $this->getParent()->getDefault();
}

return parent::getDefault();
}

/**
* {@inheritDoc}
*/
public function setDefault(?string $default): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Default value setter is allowed only for parent');
}

parent::setDefault($default);
}

/**
* {@inheritDoc}
*/
Expand Down
25 changes: 25 additions & 0 deletions src/Entities/Channels/Properties/StaticProperty.php
Expand Up @@ -16,6 +16,7 @@
namespace FastyBird\DevicesModule\Entities\Channels\Properties;

use Doctrine\ORM\Mapping as ORM;
use FastyBird\DevicesModule\Exceptions;
use FastyBird\Metadata\Types as MetadataTypes;

/**
Expand All @@ -32,4 +33,28 @@ public function getType(): MetadataTypes\PropertyTypeType
return MetadataTypes\PropertyTypeType::get(MetadataTypes\PropertyTypeType::TYPE_STATIC);
}

/**
* {@inheritDoc}
*/
public function getValue()
{
if ($this->getParent() !== null) {
return $this->getParent()->getValue();
}

return parent::getValue();
}

/**
* {@inheritDoc}
*/
public function setValue(?string $value): void
{
if ($this->getParent() !== null) {
throw new Exceptions\InvalidStateException('Value setter is allowed only for parent');
}

parent::setValue($value);
}

}

0 comments on commit 5502633

Please sign in to comment.