00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBField implements IDBField {
00009
00010
00011
00012 const NOT_NULL = 1;
00013
00014
00015
00016 const INTERNAL = 8;
00017
00018
00019
00020
00021
00022
00023 protected $name = '';
00024
00025
00026
00027
00028
00029 protected $default_value = null;
00030
00031
00032
00033
00034
00035 protected $policy;
00036
00037
00038
00039
00040
00041 protected $connection;
00042
00043
00044
00045
00046
00047 protected $table;
00048
00049 public function __construct($name, $default_value = null, $policy = self::NONE, $connection = DB::DEFAULT_CONNECTION) {
00050 $this->name = $name;
00051 $this->default_value = $default_value;
00052 $this->policy = $policy;
00053 $this->connection = $connection;
00054 }
00055
00056
00057
00058
00059
00060
00061 public function get_field_name() {
00062 return $this->name;
00063 }
00064
00065
00066
00067
00068
00069
00070 public function get_field_default() {
00071 return $this->default_value;
00072 }
00073
00074
00075
00076
00077 public function has_default_value() {
00078 return !$this->is_null($this->default_value);
00079 }
00080
00081
00082
00083
00084
00085
00086 public function get_null_allowed() {
00087 return !Common::flag_is_set($this->policy, self::NOT_NULL);
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 public function validate($value) {
00097 $ret = new Status();
00098 if ($this->is_null($value) && !$this->has_default_value() && !$this->get_null_allowed()) {
00099 $field_tr = $this->get_field_name_translation();
00100 $msg = tr('%field may not be empty', 'core', array('%field' => $field_tr));
00101 $ret->append($msg);
00102 }
00103 return $ret;
00104 }
00105
00106
00107
00108
00109
00110
00111 protected function get_field_name_translation() {
00112 $translations = array('global');
00113 $table = $this->get_table();
00114 if ($table) {
00115 array_unshift($translations, $table->get_table_name());
00116 }
00117 return tr($this->get_field_name(), $translations);
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 public function format($value) {
00127 if ($this->is_null($value)) {
00128 return $this->do_format_null($value);
00129 }
00130 else {
00131 return $this->do_format_not_null($value);
00132 }
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 protected function do_format_null($value) {
00142 return 'NULL';
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 protected function do_format_not_null($value) {
00152 return $this->quote($value);
00153 }
00154
00155
00156
00157
00158
00159
00160
00161 public function format_where($value) {
00162 return $this->format($value);
00163 }
00164
00165
00166
00167
00168 public function format_select() {
00169 return $this->get_field_name();
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 public function convert_result($value) {
00179 return $value;
00180 }
00181
00182
00183
00184
00185 public function read_from_array($arr) {
00186 return Arr::get_item($arr, $this->get_field_name(), null);
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 public function set_connection($connection) {
00196 $this->connection = $connection;
00197 }
00198
00199
00200
00201
00202
00203 protected function get_connection() {
00204 return $this->connection;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 public function set_table($table) {
00216 $this->table = $table;
00217 }
00218
00219
00220
00221
00222
00223
00224 public function get_table() {
00225 return $this->table;
00226 }
00227
00228
00229
00230
00231
00232
00233 public function get_policy() {
00234 return $this->policy;
00235 }
00236
00237
00238
00239
00240
00241
00242 public function set_policy($policy) {
00243 $this->policy = $policy;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 public function has_policy($policy) {
00253 $ret = Common::flag_is_set($this->policy, $policy);
00254 return $ret;
00255 }
00256
00257
00258
00259
00260 protected function quote($value) {
00261 return DB::quote(Cast::string($value), $this->get_connection());
00262 }
00263
00264
00265
00266
00267 protected function is_null($value) {
00268 return is_null($value) || $value instanceof DBNull;
00269 }
00270 }