. */ namespace Volkszaehler\Model; use Doctrine\Common\Collections; use Volkszaehler\Util; /** * Entity superclass for all objects referenced by a UUID * * @author Steffen Vogel * @package default * * @Entity * @Table(name="entities") * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="class", type="string") * @DiscriminatorMap({ * "channel" = "Channel", * "aggregator" = "Aggregator" * }) * @HasLifecycleCallbacks */ abstract class Entity { /** * @Id * @Column(type="smallint", nullable=false) * @GeneratedValue(strategy="AUTO") */ protected $id; /** @Column(type="string", length=36, nullable=false, unique=true) */ protected $uuid; /** @Column(type="string", nullable=false) */ protected $type; /** * @OneToMany(targetEntity="Token", mappedBy="entity", cascade={"remove", "persist"}) */ protected $tokens = NULL; /** * @OneToMany(targetEntity="Property", mappedBy="entity", cascade={"remove", "persist"}) * @OrderBy({"name" = "ASC"}) */ protected $properties = NULL; /** * Constructor * * @param string $type * @param array $properties of Model\Property */ public function __construct($type) { if (!EntityDefinition::exists($type)) { throw new \Exception('unknown entity type'); } $this->type = $type; $this->uuid = Util\UUID::mint(); $this->tokens = new Collections\ArrayCollection(); $this->properties = new Collections\ArrayCollection(); } /** * Checks for optional and required properties according to share/entities.json * * Throws an exception if something is incorrect * * @PrePersist * @PreUpdate * @PostLoad * @todo to be implemented */ protected function validate() { } /** * Get a property by name * * @param string $name * @return Model\Property */ public function getProperty($name) { return $this->properties->filter(function($property) use ($name) { return $property->getName() == $name; })->first(); } /** * Get all properties or properties by prefix * * @param string $prefix */ public function getProperties($prefix = NULL) { if (is_null($prefix)) { return $this->properties; } else { return $this->properties->filter(function($property) use ($prefix) { return substr($property->getName(), 0, strlen($prefix) + 1) == $prefix . ':'; }); } } /** * @param string $name of the property * @param string|integer|float $value of the property * @todo check if already set for this entity */ public function setProperty(Property $property) { $this->properties->add($property); } /** * @param string $name of the property * @todo to be implemented */ public function unsetProperty($name) { } /* * Setter & Getter */ public function getId() { return $this->id; } // read only public function getUuid() { return $this->uuid; } // read only public function getType() { return $this->type; } // read only public function getDefinition() { return EntityDefinition::get($this->type); } /** * Get interpreter to obtain data and statistical information for a given time interval * * @param Doctrine\ORM\EntityManager $em * @param integer $from timestamp in ms since 1970 * @param integer $to timestamp in ms since 1970 * @return Interpreter */ public function getInterpreter(\Doctrine\ORM\EntityManager $em, $from, $to) { $interpreterClassName = 'Volkszaehler\Interpreter\\' . $this->getDefinition()->getInterpreter(); return new $interpreterClassName($this, $em, $from, $to); } } ?>