Пример #1
0
 /**
  * optimize sql table, fields and value
  * @param  [string] $table [set table name]
  * @param  [string] $field [set field name]
  * @param  [string] $value [set value]
  * @return [string]        [optimize of string]
  * @example
  * 	oSting(users)			return #users#
  * 	oSting(users, id)		return #users.id#
  * 	oSting(users, id, 150)	return #users.id 150#
  */
 public function oString($table, $field = null, $value = null, $checkCondition = true)
 {
     if ($value !== null) {
         $cInt = false;
         // for insert or update multiple row
         if (is_array($value)) {
         } elseif (preg_match("/^#(.*)\$/", $value, $v)) {
             $value = $v[1];
             $cInt = true;
         } elseif (substr($value, 0, 1) == '#') {
             $value = substr($value, 1);
             $cInt = true;
         } else {
             $sTable = "get" . ucfirst(dbconnection::get_db_name());
             $cTable = sql\table::$sTable($table);
             if (isset($cTable->{$field})) {
                 $type = $cTable->{$field}->type;
                 $int = array("int", "tinyint", "smallint", "decimal");
                 preg_match("/^([^@]*)@/", $type, $tp);
                 if (preg_grep("/^" . $tp[1] . "\$/", $int)) {
                     $cInt = true;
                 }
                 if ($this->auto_validate) {
                     $status = $this->auto_validate($field, $cTable->{$field}, $value);
                     if (!is_bool($status)) {
                         \lib\debug::error($status, $field, 'form');
                     }
                 }
             }
             if (isset($cTable->{$field}->closure) && $checkCondition) {
                 $gTable = $cTable->{$field}->closure;
                 $value = preg_replace("/^\\\\#/", "#", $value);
                 $v = new validator(array($field, $value), $gTable->validate, 'form');
                 $value = $v->compile();
                 $value = $value == '' && is_string($value) && $value === false ? "NULL" : $value;
             }
             // switch by type of field and encode data if needed
             // var_dump($cTable->$field->type);
             if (isset($cTable->{$field}->type)) {
                 $atPos = strpos($cTable->{$field}->type, '@');
             } else {
                 // return false;
                 \lib\error::page("Field {$field} does not exist!");
             }
             if ($atPos !== false) {
                 switch (substr($cTable->{$field}->type, 0, $atPos)) {
                     // if the type of field is int do nothing
                     case 'tinyint':
                     case 'smallint':
                     case 'mediumint':
                     case 'int':
                     case 'bigint':
                     case 'decimal':
                     case 'float':
                         break;
                         // else doing entities
                     // else doing entities
                     case 'tinytext':
                     case 'text':
                     case 'mediumtext':
                     case 'longtext':
                     default:
                         // if does not contain meta doing nothing and encode value
                         if (strpos($field, '_meta') === false) {
                             $value = htmlentities($value, ENT_QUOTES, "UTF-8");
                         }
                         break;
                 }
             }
             // if(!$cInt)
             // {
             // 	$value = htmlentities($value, ENT_QUOTES, "UTF-8");
             // }
         }
         if (is_array($value)) {
             $optimize = $value;
         } else {
             $optimize = $cInt ? "{$value}" : "'{$value}'";
         }
     } else {
         $optimize = "`{$table}`";
         if ($field) {
             if (preg_match("/^#/", $field)) {
                 $optimize = preg_replace("/^#/", "", $field);
             } else {
                 // $optimize .= $field ? ($field === "*") ? ".$field" : ".`$field`" : "";
                 if ($field) {
                     if ($field === "*") {
                         $optimize .= ".{$field}";
                     } else {
                         $optimize .= ".`{$field}`";
                     }
                 } else {
                     $optimize .= "";
                 }
             }
         }
     }
     return $optimize;
 }