. */ 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", "pkey"}) * } * ) * @HasLifecycleCallbacks */ class Property { /** * @Id * @Column(type="integer", nullable=false) * @GeneratedValue(strategy="AUTO") * * @todo wait until DDC-117 is fixed (PKs on FKs) */ protected $id; /** * @Column(name="pkey", type="string", nullable=false) * HINT: column name "key" is reserved by mysql */ protected $key; /** @Column(type="text", 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; $this->validate(); } /** * 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'); } $this->cast(); // TODO not safe if (!$this->getDefinition()->validateValue($this->value)) { throw new \Exception('Invalid property value'); } } /* * 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; } } ?>