. */ namespace Volkszaehler\Model; use Volkszaehler\Definition; use Volkszaehler\Util; use Volkszaehler\Model; /** * Property entity * * @author Steffen Vogel * @package default * * @Entity * @Table( * name="properties", * uniqueConstraints={ * @UniqueConstraint(name="unique_keys", columns={"entity_id", "`key`"}) * } * ) * @HasLifecycleCallbacks */ class Property { /** * @Id * @Column(type="smallint", nullable=false) * @GeneratedValue(strategy="AUTO") * * @todo wait until DDC-117 is fixed (PKs on FKs) */ protected $id; /** @Column(name="`key`", type="string", nullable=false) */ protected $key; /** @Column(type="string", nullable=false) */ protected $value; /** @ManyToOne(targetEntity="Entity", inversedBy="properties") */ protected $entity; /** * Constructor * * @param string $key * @param string $value */ public function __construct(Model\Entity $entity, $key, $value) { $this->entity = $entity; $this->key = $key; $this->value = $value; } /** * Cast property value according to $this->type * * @PostLoad */ public function cast() { $type = $this->getDefinition()->getType(); if ($type == 'multiple') { // TODO } elseif ($type == 'text') { settype($this->value, 'string'); } else { settype($this->value, $type); } } /** * Validate property key & value * * Throws an exception if something is incorrect * * @PrePersist * @PreUpdate */ public function validate() { if (!Definition\PropertyDefinition::exists($this->key)) { throw new \Exception('Invalid property key: ' . $this->key); } $this->cast(); // TODO not safe if (!$this->getDefinition()->validateValue($this->value)) { throw new \Exception('Invalid property value: ' . $this->value); } } /** * @PreRemove */ public function checkRemove() { if (in_array($this->key, $this->entity->getDefinition()->getRequiredProperties())) { throw new \Exception($this->key . ' is a required property for the entity: ' . $this->entity->getType()); } } /** * @PrePersist */ public function checkPersist() { if (!in_array($this->key, $this->entity->getDefinition()->getValidProperties())) { throw new \Exception($this->key . ' is not a valid property for the entity: ' . $this->entity->getType()); } } /* * Setter & Getter */ public function getKey() { return $this->key; } public function getValue() { return $this->value; } public function getDefinition() { return Definition\PropertyDefinition::get($this->key); } public function setValue($value) { $this->value = $value; } } ?>