. */ namespace Volkszaehler\Model; use Volkszaehler\Util; /** * @author Steffen Vogel * @package default */ class PropertyDefinition extends Util\Definition { /** One of: string, numeric, multiple */ public $type; /** @var string regex pattern to match if type == string */ protected $pattern; /** * Minimal value if type == integer or type == float * Required string length if type == string * * @var integer|float */ protected $min; /** * Maximal value if type == integer or type == float * Allowed string length if type == string * * @var integer|float */ protected $max; /** * List of possible choices if type == multiple * (type as in javascript: 1.2 => float, 5 => integer, "test" => string) * * @var array */ protected $choices = array(); protected static $definitions = NULL; /** * File containing the JSON definitons * * @var string */ const FILE = '/share/properties.json'; /** * Validate value according to $this->type * * @param string|numeric $value * @return boolean */ public function validateValue($value) { switch ($this->type) { case 'string': $invalid = !is_string($value); $invalid |= isset($this->pattern) && !preg_match($this->pattern, $value); $invalid |= isset($this->min) && strlen($value) < $this->min; $invalid |= isset($this->max) && strlen($value) > $this->max; break; case 'integer': $invalid = !is_int($value); // TODO check for numeric string break; case 'float': $invalid = !is_float($value); // TODO check for numeric string break; case 'multiple': $invalid = !in_array($value, $this->choices, TRUE); break; default: throw new \Exception('unknown property type'); } if ($this->type == 'integer' || $this->type == 'float') { $invalid |= isset($this->min) && $value < $this->min; $invalid |= isset($this->max) && $value > $this->max; } return !$invalid; } } ?>