gyro/core/model/base/fields/dbfield.int.cls.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * A integer field in DB 00004 * 00005 * @author Gerd Riesselmann 00006 * @ingroup Model 00007 */ 00008 class DBFieldInt extends DBField { 00009 /** 00010 * This field is autoincremented 00011 */ 00012 const AUTOINCREMENT = 4; 00013 /** 00014 * This field is unsigned 00015 */ 00016 const UNSIGNED = 2; 00017 00018 /** 00019 * Convenience declaration: Combines AUTOINCREMENT, UNSIGNED, and NOT_NULL 00020 */ 00021 const PRIMARY_KEY = 7; // self::AUTOINCREMENT | self::UNSIGNED | self::NOT_NULL 00022 /** 00023 * Convenience declaration: Combines UNSIGNED, and NOT_NULL 00024 */ 00025 const FOREIGN_KEY = 3; // self::UNSIGNED | self::NOT_NULL 00026 00027 public function __construct($name, $default_value = 0, $policy = self::NOT_NULL) { 00028 parent::__construct($name, $default_value, $policy); 00029 } 00030 00031 /** 00032 * Returns the default value for this field 00033 * 00034 * @return mixed 00035 */ 00036 public function get_field_default() { 00037 if ($this->has_policy(self::AUTOINCREMENT)) { 00038 return null; 00039 } 00040 return parent::get_field_default(); 00041 } 00042 00043 /** 00044 * Returns true, if the value passed fits the fields restrictions 00045 * 00046 * @param string $value 00047 * @return Status 00048 */ 00049 public function validate($value) { 00050 if ($value === '') { 00051 $value = null; 00052 } 00053 00054 $ret = parent::validate($value); 00055 if ($ret->is_ok() && !is_null($value)) { 00056 if ($this->has_policy(self::UNSIGNED)) { 00057 if (!Validation::is_int($value, 0)) { 00058 $ret->append(tr( 00059 '%field must be a positive integer', 00060 'core', 00061 array( 00062 '%field' => $this->get_field_name_translation() 00063 ) 00064 )); 00065 } 00066 } 00067 else if (!Validation::is_int($value)) { 00068 $ret->append(tr( 00069 '%field must be an integer', 00070 'core', 00071 array( 00072 '%field' => $this->get_field_name_translation() 00073 ) 00074 )); 00075 } 00076 } 00077 00078 return $ret; 00079 } 00080 00081 /** 00082 * Returns true, if field has default value 00083 */ 00084 public function has_default_value() { 00085 $ret = $this->has_policy(self::AUTOINCREMENT) || parent::has_default_value(); 00086 return $ret; 00087 } 00088 00089 /** 00090 * Format values that are not NULL 00091 * 00092 * @param mixed $value 00093 * @return string 00094 */ 00095 protected function do_format_not_null($value) { 00096 return Cast::int($value); 00097 } 00098 00099 }