/** * This constructor initializes the class. * * @access public * @param DB_ORM_Model $model a reference to the implementing model * @param array $metadata the field's metadata * @throws Throwable_Validation_Exception indicates that the specified value does * not validate */ public function __construct(DB_ORM_Model $model, array $metadata = array()) { parent::__construct($model, 'string'); if (isset($metadata['savable'])) { $this->metadata['savable'] = (bool) $metadata['savable']; } if (isset($metadata['nullable'])) { $this->metadata['nullable'] = (bool) $metadata['nullable']; } if (isset($metadata['filter'])) { $this->metadata['filter'] = (string) $metadata['filter']; } if (isset($metadata['callback'])) { $this->metadata['callback'] = (string) $metadata['callback']; } if (isset($metadata['enum'])) { $this->metadata['enum'] = (array) $metadata['enum']; } if (isset($metadata['control'])) { $this->metadata['control'] = (string) $metadata['control']; } if (isset($metadata['label'])) { $this->metadata['label'] = (string) $metadata['label']; } if (isset($metadata['default'])) { $default = $metadata['default']; } else { if (isset($this->metadata['enum'])) { $default = $this->metadata['enum'][0]; } else { if (!$this->metadata['nullable']) { $default = isset($this->metadata['enum']) ? $this->metadata['enum'][0] : '0000-00-00'; } else { $default = (isset($this->metadata['enum']) and !in_array(NULL, $this->metadata['enum'])) ? $this->metadata['enum'][0] : NULL; } } } if (!$default instanceof DB_SQL_Expression) { if ($default !== NULL) { if (is_integer($default)) { $default = date('Y-m-d', $default); } else { $date = date_parse((string) $default); $default = date('Y-m-d', mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year'])); } } if (!$this->validate($default)) { throw new Throwable_Validation_Exception('Message: Unable to set default value for field. Reason: Value :value failed to pass validation constraints.', array(':value' => $default)); } } $this->metadata['default'] = $default; $this->value = $default; }
/** * This constructor initializes the class. * * @access public * @param DB_ORM_Model $model a reference to the implementing model * @param array $metadata the field's metadata * @throws Throwable_Validation_Exception indicates that the specified value does * not validate */ public function __construct(DB_ORM_Model $model, array $metadata = array()) { parent::__construct($model, 'boolean'); if (isset($metadata['savable'])) { $this->metadata['savable'] = (bool) $metadata['savable']; } if (isset($metadata['nullable'])) { $this->metadata['nullable'] = (bool) $metadata['nullable']; } if (isset($metadata['filter'])) { $this->metadata['filter'] = (string) $metadata['filter']; } if (isset($metadata['callback'])) { $this->metadata['callback'] = (string) $metadata['callback']; } $this->metadata['control'] = 'checkbox'; if (isset($metadata['label'])) { $this->metadata['label'] = (string) $metadata['label']; } if (isset($metadata['default'])) { $default = $metadata['default']; } else { if (!$this->metadata['nullable']) { $default = FALSE; } else { $default = NULL; } } if (!$default instanceof DB_SQL_Expression) { if ($default !== NULL) { if (is_string($default)) { $default = strtolower($default); if (in_array($default, array('true', 't', 'yes', 'y', '1'))) { $default = TRUE; } else { if (in_array($default, array('false', 'f', 'no', 'n', '0'))) { $default = FALSE; } } } settype($default, $this->metadata['type']); } if (!$this->validate($default)) { throw new Throwable_Validation_Exception('Message: Unable to set default value for field. Reason: Value :value failed to pass validation constraints.', array(':value' => $default)); } } $this->metadata['default'] = $default; $this->value = $default; }
/** * This constructor initializes the class. * * @access public * @param DB_ORM_Model $model a reference to the implementing model * @param array $metadata the field's metadata * @throws Throwable_Validation_Exception indicates that the specified value does * not validate */ public function __construct(DB_ORM_Model $model, array $metadata = array()) { parent::__construct($model, 'string'); if (isset($metadata['savable'])) { $this->metadata['savable'] = (bool) $metadata['savable']; } if (isset($metadata['nullable'])) { $this->metadata['nullable'] = (bool) $metadata['nullable']; } if (isset($metadata['filter'])) { $this->metadata['filter'] = (string) $metadata['filter']; } if (isset($metadata['callback'])) { $this->metadata['callback'] = (string) $metadata['callback']; } $this->metadata['control'] = 'textarea'; if (isset($metadata['label'])) { $this->metadata['label'] = (string) $metadata['label']; } if (isset($metadata['default'])) { $default = $metadata['default']; } else { if (!$this->metadata['nullable']) { $default = ''; } else { $default = NULL; } } if (!$default instanceof DB_SQL_Expression) { if ($default !== NULL) { settype($default, $this->metadata['type']); } if (!$this->validate($default)) { throw new Throwable_Validation_Exception('Message: Unable to set default value for field. Reason: Value :value failed to pass validation constraints.', array(':value' => $default)); } } $this->metadata['default'] = $default; $this->value = $default; }
/** * This function validates the specified value against any constraints. * * @access protected * @override * @param mixed $value the value to be validated * @return boolean whether the specified value validates */ protected function validate($value) { if ($value !== NULL) { if ($this->metadata['unsigned'] and $value < 0.0) { return FALSE; } else { if (isset($this->metadata['range'])) { if ($value < $this->metadata['range']['lower_bound'] or $value > $this->metadata['range']['upper_bound']) { return FALSE; } } } if (isset($this->metadata['max_digits'])) { $parts = preg_split('/\\./', "{$value}"); $digits = strlen("{$parts[0]}"); if (isset($this->metadata['max_decimals']) and count($parts) > 1) { $decimals = strlen("{$parts[1]}"); if ($decimals > $this->metadata['max_decimals']) { return FALSE; } $digits += $decimals; } if ($digits > $this->metadata['max_digits']) { return FALSE; } } } return parent::validate($value); }
/** * This function validates the specified value against any constraints. * * @access protected * @override * @param mixed $value the value to be validated * @return boolean whether the specified value validates */ protected function validate($value) { if ($value !== NULL) { if (strlen($value) > $this->metadata['max_length']) { return FALSE; } else { if (isset($this->metadata['regex']) and !preg_match($this->metadata['regex'], $value)) { return FALSE; } } } return parent::validate($value); }
/** * This function validates the specified value against any constraints. * * @access protected * @override * @param mixed $value the value to be validated * @return boolean whether the specified value validates */ protected function validate($value) { if ($value !== NULL) { if (strlen("{$value}") > $this->metadata['precision']) { return FALSE; } } return parent::validate($value); }
/** * This function validates the specified value against any constraints. * * @access protected * @override * @param mixed $value the value to be validated * @return boolean whether the specified value validates */ protected function validate($value) { if ($value !== NULL) { if (!$value instanceof $this->metadata['type']) { return FALSE; } if (!$value->has_pattern($this->metadata['pattern'])) { return FALSE; } } return parent::validate($value); }
/** * This function validates the specified value against any constraints. * * @access protected * @override * @param mixed $value the value to be validated * @return boolean whether the specified value validates */ protected function validate($value) { if ($value !== NULL) { if (strlen($value) > $this->metadata['max_length'] and !preg_match('/^(0|1)*$/', $value)) { return FALSE; } } return parent::validate($value); }
/** * This function validates the specified value against any constraints. * * @access protected * @override * @param mixed $value the value to be validated * @return boolean whether the specified value validates */ protected function validate($value) { if ($value !== NULL) { if (isset($this->metadata['max_length']) and strlen(strval($value)) > $this->metadata['max_length']) { return FALSE; } if (isset($this->metadata['int8fix'])) { if (!preg_match('/^-?[0-9]+$/D', strval($value)) or bccomp(strval($value), strval($this->metadata['range']['lower_bound'])) === -1 or bccomp(strval($value), strval($this->metadata['range']['upper_bound'])) === 1) { return FALSE; } } else { if ($value < $this->metadata['range']['lower_bound'] or $value > $this->metadata['range']['upper_bound']) { return FALSE; } } } return parent::validate($value); }