Esempio n. 1
0
 protected static function validateForeachable($param)
 {
     if (!is_array($param) && !$param instanceof \Traversable) {
         throw new ForeachableException(Strings::getClassOrType($param));
     }
 }
Esempio n. 2
0
 /**
  * Format search filters for a native query
  *
  * @param array $options filters to apply - only following keys are supported: search, globalSearch, globalSearchColumns
  * @retuen array
  * @throws \Exception
  * @author Yohann Marillet
  */
 protected function getNativeSearchFilters($options = [])
 {
     $return['where'] = '';
     $i = 0;
     if (isset($options['search']) && !empty($options['search'])) {
         foreach ($options['search'] as $k => $e) {
             $qbt = '';
             if (!is_array($e) || isset($e['value'])) {
                 $e = array($e);
             }
             foreach ($e as $v) {
                 if (!isset($v['value'])) {
                     if (is_scalar($v)) {
                         $v = ['value' => $v];
                     } else {
                         throw new \Exception(sprintf('$v is expected to be scalar. Given: %s', Strings::getClassOrType($v)));
                     }
                 }
                 if (!isset($v['type'])) {
                     $v['type'] = 'scalar';
                 }
                 $condition = '';
                 if ($v['type'] == 'scalar') {
                     $condition = $k . " LIKE " . $this->getEntityManager()->getConnection()->quote('%' . str_replace('%', '', $v['value']) . '%');
                 } elseif ($v['type'] == 'array') {
                     array_walk($v['value'], [$this->getEntityManager()->getConnection(), 'quote']);
                     $values = implode(',', $v['value']);
                     $condition = $k . ' IN (' . $values . ')';
                 } elseif ($v['type'] == 'callback') {
                     $condition = $this->{$k}($v['value']);
                 }
                 $qbt .= (!empty($qbt) ? ' AND ' : '') . '(' . $condition . ')';
             }
             $return['where'] .= 'AND (' . $qbt . ')';
         }
     }
     if (isset($options['globalSearch']) && !empty($options['globalSearch']) && isset($options['globalSearchColumns']) && !empty($options['globalSearchColumns'])) {
         $qbt = '';
         $globalSearchValue = $this->getEntityManager()->getConnection()->quote('%' . str_replace('%', '', $options['globalSearch']) . '%');
         foreach ($options['globalSearchColumns'] as $e) {
             $qbt .= (!empty($qbt) ? ' OR ' : '') . "({$e} LIKE {$globalSearchValue})";
         }
         $return['where'] .= 'AND (' . $qbt . ')';
     }
     return $return;
 }