. */ namespace Volkszaehler\Model; use Volkszaehler\Util; /** * @author Steffen Vogel * @package default */ abstract class Definition { /** @var string discriminator for database column */ protected $name; /** @var string title for UI */ //protected $title; /** @var string description for UI */ //protected $description; /** * Hide default constructor * * @param array $object to cast from */ protected function __construct($object) { foreach (get_object_vars($object) as $name => $value) { if (property_exists(get_class($this), $name)) { $this->$name = $value; } else { throw new \Exception('Unknown definition syntax: ' . $name); } } } /** * Factory method for creating new instances * * @param string $name * @return Util\Definition */ public static function get($name) { if (is_null(static::$definitions)) { static::load(); } if (!static::exists($name)) { throw new \Exception('Unknown definition'); } return static::$definitions[$name]; } /** * Checks if $name is defined * @param string $name */ public static function exists($name) { if (is_null(static::$definitions)) { static::load(); } return isset(static::$definitions[$name]); } /** * Load JSON definitions from file (via lazy loading from get()) */ protected static function load() { $json = Util\JSON::decode(file_get_contents(VZ_DIR . static::FILE)); static::$definitions = array(); foreach ($json as $property) { static::$definitions[$property->name] = new static($property); } } } ?>