コード例 #1
0
ファイル: Query.php プロジェクト: icanboogie/activerecord
 /**
  * Parse the conditions for the {@link where()} and {@link having()} methods.
  *
  * {@link \DateTimeInterface} conditions are converted to strings.
  *
  * @return array An array made of the condition string and its arguments.
  */
 private function deferred_parse_conditions()
 {
     $args = debug_backtrace(0, 2)[1]['args'];
     $conditions = array_shift($args);
     if (is_array($conditions)) {
         $c = '';
         $conditions_args = [];
         foreach ($conditions as $column => $arg) {
             if (is_array($arg) || $arg instanceof self) {
                 $joined = '';
                 if (is_array($arg)) {
                     foreach ($arg as $value) {
                         $joined .= ',' . (is_numeric($value) ? $value : $this->model->quote($value));
                     }
                     $joined = substr($joined, 1);
                 } else {
                     $joined = (string) $arg;
                     $conditions_args = array_merge($conditions_args, $arg->args);
                 }
                 $c .= ' AND `' . ($column[0] == '!' ? substr($column, 1) . '` NOT' : $column . '`') . ' IN(' . $joined . ')';
             } else {
                 $conditions_args[] = $arg;
                 $c .= ' AND `' . ($column[0] == '!' ? substr($column, 1) . '` !' : $column . '` ') . '= ?';
             }
         }
         $conditions = substr($c, 5);
     } else {
         $conditions_args = [];
         if ($args) {
             if (is_array($args[0])) {
                 $conditions_args = $args[0];
             } else {
                 #
                 # We dereference values otherwise the caller would get a corrupted array.
                 #
                 foreach ($args as $key => $value) {
                     $conditions_args[$key] = $value;
                 }
             }
         }
     }
     foreach ($conditions_args as &$value) {
         if ($value instanceof \DateTimeInterface) {
             $value = DateTime::from($value)->utc->as_db;
         }
     }
     return [$conditions ? '(' . $conditions . ')' : null, $conditions_args];
 }