. */ namespace Volkszaehler\Util; /** * @author Steffen Vogel * @package util */ abstract class JSONDefinition { /** * Cached json definitions * * @var array */ protected static $definitions = NULL; /** Discriminator for database column */ protected $name; /** * Hide default constructor * * @param array $name */ 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: ' . $name); } } } /** * Factory method for creating new instances * * @param string $name * @return Model\PropertyDefinition */ public static function get($name) { if (is_null(self::$definitions)) { self::load(); } if (!isset(self::$definitions[$name])) { throw new \Exception('unknown definition'); } return self::$definitions[$name]; } /** * Load JSON definitions from file (via lazy loading from get()) */ protected static function load() { $json = file_get_contents(VZ_DIR . static::FILE); $json = JSON::strip($json); $json = json_decode($json); // TODO move to Util\JSON class if (!is_array($json) || count($json) == 0) { throw new \Exception('syntax error in definition'); } self::$definitions = array(); foreach ($json as $property) { self::$definitions[$property->name] = new static($property); } } } ?>