. */ namespace Volkszaehler\Definition; /** * @author Steffen Vogel * @package default */ class PropertyDefinition extends Definition { /** * File containing the JSON definitons * * @var string */ const FILE = '/lib/Definition/PropertyDefinition.json'; /** * One of: string, integer, float, boolean, multiple, text * * @var string */ protected $type; /** * Regex pattern to match if type == string * * @var 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, true => boolean, "test" => string) * * @var array */ protected $options = array(); /** * @var array holds definitions */ protected static $definitions = NULL; /** * Validate value according to $this->type * * @param string|numeric $value * @return boolean */ public function validateValue($value) { switch ($this->type) { case 'string': case 'text': $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); break; case 'float': $invalid = !is_float($value); break; case 'boolean': $invalid = !is_bool($value); break; case 'multiple': $invalid = !in_array($value, $this->options, TRUE); break; default: throw new \Exception('Unknown property type: ' . $type); } if ($this->type == 'integer' || $this->type == 'float') { $invalid |= isset($this->min) && $value < $this->min; $invalid |= isset($this->max) && $value > $this->max; } return !$invalid; } /* * Setter & getter */ public function getType() { return $this->type; } } ?>