Ejemplo n.º 1
0
 /**
  * Set filter
  * @param array $filter
  * @return \BX\DB\Filter\SqlBuilder
  */
 public function filter(array $filter = [])
 {
     $filter_rule = [];
     foreach ($this->entity->getColumns() as $key => $column) {
         $filter_rule[$key] = $column->getFilterRule();
     }
     $block = new LogicBlock($this, $filter_rule);
     $sql = $block->toSql($filter);
     if ($this->string()->length($sql) > 0) {
         $this->filter_sql[] = $sql;
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * Get sql
  * @param array $filter
  * @return string
  * @throws \InvalidArgumentException
  */
 public function toSql(array $filter)
 {
     $return = [];
     $logic = 'AND';
     foreach ($filter as $key => $value) {
         if (is_numeric($key)) {
             $block = new LogicBlock($this->sql_builder, $this->filter_rule);
             $return[] = '(' . $block->toSql((array) $value) . ')';
         } elseif ($key === 'LOGIC') {
             if (in_array($value, ['OR', 'AND'])) {
                 $logic = $value;
             } else {
                 throw new \InvalidArgumentException("Logic must be AND or OR set `{$value}`");
             }
         } else {
             $return[] = $this->getType($key)->addCondition($key, $value);
         }
     }
     return implode(' ' . $logic . ' ', $return);
 }