. */ 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", * "group" = "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") */ protected $tokens = NULL; /** * @OneToMany(targetEntity="Property", mappedBy="entity") * @OrderBy({"name" = "ASC"}) */ protected $properties = NULL; /** * Constructor * * @param string $type * @param array $properties of Model\Property */ public function __construct($type, $properties = NULL) { 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(); if (isset($properties)) { foreach($properties as $property) { $this->properies->add($property); } } } /** * 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 to be implemented */ public function setProperty($name, $value) { } /** * @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); } } class EntityDefinition extends Util\Definition { /** @var string File containing the JSON definitons */ const FILE = '/share/entities.json'; /** @var array list of required properties */ protected $required = array(); /** @var array list of optional properties */ protected $optional = array(); /** @var string classname of intepreter (see backend/lib/Interpreter/) */ protected $interpreter; /** @var string optional for Aggregator class entities */ protected $unit; /** * @todo url relative or absolute? * @var string */ protected $icon; /* * Setter & Getter */ public function getInterpreter() { return $this->interpreter; } public function getUnit() { return $this->unit; } } ?>