reorganized definition classes
This commit is contained in:
parent
f9a0ff56e0
commit
5a916e5d46
4 changed files with 41 additions and 30 deletions
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @package util
|
||||
* @package default
|
||||
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
|
||||
*/
|
||||
/*
|
||||
|
@ -21,21 +21,23 @@
|
|||
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Volkszaehler\Util;
|
||||
namespace Volkszaehler\Model;
|
||||
|
||||
use Volkszaehler\Util;
|
||||
|
||||
/**
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
* @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();
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace Volkszaehler;
|
|||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
* @package default
|
||||
*/
|
||||
class EntityDefinition extends Util\Definition {
|
||||
class EntityDefinition extends Definition {
|
||||
/** @var string File containing the JSON definitons */
|
||||
const FILE = '/share/entities.json';
|
||||
|
||||
|
|
|
@ -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; }
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -23,15 +23,13 @@
|
|||
|
||||
namespace Volkszaehler\Model;
|
||||
|
||||
use Volkszaehler\Util;
|
||||
|
||||
/**
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
* @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; }
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Add table
Reference in a new issue