diff --git a/backend/lib/Model/Definition.php b/backend/lib/Model/Definition.php index 2edd2c8..737b79b 100644 --- a/backend/lib/Model/Definition.php +++ b/backend/lib/Model/Definition.php @@ -1,7 +1,7 @@ . */ -namespace Volkszaehler\Util; +namespace Volkszaehler\Model; + +use Volkszaehler\Util; /** * @author Steffen Vogel - * @package util + * @package default */ abstract class Definition { /** @var string discriminator for database column */ protected $name; /** @var string title for UI */ - protected $title; + //protected $title; /** @var string description for UI */ - protected $description; + //protected $description; /** * Hide default constructor @@ -84,7 +86,7 @@ abstract class Definition { * Load JSON definitions from file (via lazy loading from get()) */ protected static function load() { - $json = JSON::decode(file_get_contents(VZ_DIR . static::FILE)); + $json = Util\JSON::decode(file_get_contents(VZ_DIR . static::FILE)); static::$definitions = array(); diff --git a/backend/lib/Model/EntityDefinition.php b/backend/lib/Model/EntityDefinition.php index 461d977..5901b33 100644 --- a/backend/lib/Model/EntityDefinition.php +++ b/backend/lib/Model/EntityDefinition.php @@ -27,7 +27,7 @@ namespace Volkszaehler; * @author Steffen Vogel * @package default */ -class EntityDefinition extends Util\Definition { +class EntityDefinition extends Definition { /** @var string File containing the JSON definitons */ const FILE = '/share/entities.json'; diff --git a/backend/lib/Model/Property.php b/backend/lib/Model/Property.php index a14f352..1a1fc3d 100644 --- a/backend/lib/Model/Property.php +++ b/backend/lib/Model/Property.php @@ -73,6 +73,17 @@ class Property { $this->entity = $entity; } + /** + * Cast property value according to $this->type + * + * @PostLoad + */ + public function castValue() { + if ($this->getDefinition()->getType() != 'multiple') { + settype($this->value, $this->getDefinition()->getType()); + } + } + /** * Validate property name & value * @@ -80,11 +91,15 @@ class Property { * * @PrePersist * @PreUpdate - * @PostLoad - * @todo to be implemented */ - function validate() { + public function validate() { + if (!PropertyDefinition::exists($this->name)) { + throw new \Exception('invalid property name: ' . $this->name); + } + if (!$this->getDefinition()->validateValue($this->value)) { + throw new \Exception('invalid property value: ' . $this->value); + } } /* @@ -94,20 +109,7 @@ class Property { public function getValue() { return $this->value; } public function getDefinition() { return PropertyDefinition::get($this->name); } - public function setValue($value) { - if (!$this->getDefinition()->validateValue($value)) { - throw new \Exception('invalid property value'); - } - - $this->value = $value; - } - - /** - * - * @param string $name - * @todo validation - */ - protected function setName($name) { $this->name = $name; } + public function setValue($value) { $this->value = $value; } } ?> diff --git a/backend/lib/Model/PropertyDefinition.php b/backend/lib/Model/PropertyDefinition.php index 773f086..ddaffed 100644 --- a/backend/lib/Model/PropertyDefinition.php +++ b/backend/lib/Model/PropertyDefinition.php @@ -23,15 +23,13 @@ namespace Volkszaehler\Model; -use Volkszaehler\Util; - /** * @author Steffen Vogel * @package default */ -class PropertyDefinition extends Util\Definition { - /** One of: string, numeric, multiple */ - public $type; +class PropertyDefinition extends Definition { + /** One of: string, integer, float, boolean, multiple */ + protected $type; /** @var string regex pattern to match if type == string */ protected $pattern; @@ -54,7 +52,7 @@ class PropertyDefinition extends Util\Definition { /** * List of possible choices if type == multiple - * (type as in javascript: 1.2 => float, 5 => integer, "test" => string) + * (type as in javascript: 1.2 => float, 5 => integer, true => boolean, "test" => string) * * @var array */ @@ -93,6 +91,10 @@ class PropertyDefinition extends Util\Definition { $invalid = !is_float($value); // TODO check for numeric string break; + case 'boolean': + $invalid = !is_bool($value); // TODO check for numeric string + break; + case 'multiple': $invalid = !in_array($value, $this->choices, TRUE); break; @@ -108,6 +110,11 @@ class PropertyDefinition extends Util\Definition { return !$invalid; } + + /* + * Setter & getter + */ + public function getType() { return $this->type; } } ?>