Elgg  Version master
ComparisonClause.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Clauses;
4 
7 use Elgg\Values;
8 
12 class ComparisonClause extends Clause {
13 
17  public $x;
18 
22  public $comparison;
23 
27  public $y;
28 
32  public $type;
33 
38 
48  public function __construct($x, $comparison, $y = null, $type = null, $case_sensitive = null) {
49  $this->x = $x;
50  $this->comparison = $comparison;
51  $this->y = $y;
52  $this->type = $type;
53  $this->case_sensitive = $case_sensitive;
54  }
55 
61  public function prepare(QueryBuilder $qb, $table_alias = null) {
62  $x = $this->x;
63  $y = $this->y;
66 
67  $compare_with = function ($func, $boolean = 'OR') use ($x, $y, $type, $case_sensitive, $qb) {
68  if (!isset($y)) {
69  return;
70  }
71 
72  $y = is_array($y) ? $y : [$y];
73  $parts = [];
74  foreach ($y as $val) {
75  $val = $qb->param($val, $type);
77  $val = "BINARY {$val}";
78  }
79 
80  $parts[] = $qb->expr()->$func($x, $val);
81  }
82 
83  return $qb->merge($parts, $boolean);
84  };
85 
86  $match_expr = null;
87  $comparison = strtolower($this->comparison);
88  switch ($comparison) {
89  case '=':
90  case 'eq':
91  case 'in':
92  if ($this->case_sensitive && $this->type == ELGG_VALUE_STRING) {
93  $x = "CAST($x as BINARY)";
94  }
95 
96  if (is_array($y) || $comparison === 'in') {
97  if (!Values::isEmpty($y)) {
98  $param = isset($type) ? $qb->param($y, $type) : $y;
99  $match_expr = $qb->expr()->in($x, $param);
100  }
101  } elseif (isset($y)) {
102  $param = isset($type) ? $qb->param($y, $type) : $y;
103  $match_expr = $qb->expr()->eq($x, $param);
104  }
105  return $match_expr;
106 
107  case '!=':
108  case '<>':
109  case 'neq':
110  case 'not in':
111  if ($this->case_sensitive && $this->type == ELGG_VALUE_STRING) {
112  $x = "CAST($x as BINARY)";
113  }
114 
115  if (is_array($y) || $comparison === 'not in') {
116  if (!empty($y)) {
117  $param = isset($type) ? $qb->param($y, $type) : $y;
118  $match_expr = $qb->expr()->notIn($x, $param);
119  }
120  } elseif (isset($y)) {
121  $param = isset($type) ? $qb->param($y, $type) : $y;
122  $match_expr = $qb->expr()->neq($x, $param);
123  }
124  return $match_expr;
125 
126  case 'like':
127  return $compare_with('like');
128 
129  case 'not like':
130  return $compare_with('notLike', 'AND');
131 
132  case '>':
133  case 'gt':
134  return $compare_with('gt');
135 
136  case '<':
137  case 'lt':
138  return $compare_with('lt');
139 
140  case '>=':
141  case 'gte':
142  return $compare_with('gte');
143 
144  case '<=':
145  case 'lte':
146  return $compare_with('lte');
147 
148  case 'is null':
149  return $qb->expr()->isNull($x);
150 
151  case 'is not null':
152  return $qb->expr()->isNotNull($x);
153 
154  case 'exists':
155  return "EXISTS ($y)";
156 
157  case 'not exists':
158  return "NOT EXISTS ($y)";
159 
160  default:
161  throw new DomainException("'{$this->comparison}' is not a supported comparison operator");
162  }
163  }
164 }
Utility class for building composite comparison expression.
param($value, string $type=null, string $key=null)
Sets a new parameter assigning it a unique parameter key/name if none provided Returns the name of th...
Saves user notification settings.
Interface that allows resolving statements and/or extending query builder.
Definition: Clause.php:14
Exception thrown if a value does not adhere to a defined valid data domain.
Database abstraction query builder.
static isEmpty($value)
Check if a value isn&#39;t empty, but allow 0 and &#39;0&#39;.
Definition: Values.php:192
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
__construct($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Constructor.
prepare(QueryBuilder $qb, $table_alias=null)
{}
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
const ELGG_VALUE_STRING
Definition: constants.php:112
$qb
Definition: queue.php:12