Elgg  Version 6.1
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 
23  public function __construct(
24  public string $x,
25  public string $comparison,
26  public mixed $y = null,
27  public ?string $type = null,
28  public ?bool $case_sensitive = null
29  ) {
30  }
31 
37  public function prepare(QueryBuilder $qb, $table_alias = null) {
38  $x = $this->x;
39  $y = $this->y;
41  $case_sensitive = $this->case_sensitive;
42 
43  $compare_with = function ($func, $boolean = 'OR') use ($x, $y, $type, $case_sensitive, $qb) {
44  if (!isset($y)) {
45  return null;
46  }
47 
48  $y = is_array($y) ? $y : [$y];
49  $parts = [];
50  foreach ($y as $val) {
51  $val = isset($type) ? $qb->param($val, $type) : $val;
52  if ($case_sensitive && $type === ELGG_VALUE_STRING) {
53  $val = "BINARY {$val}";
54  }
55 
56  $parts[] = $qb->expr()->$func($x, $val);
57  }
58 
59  return $qb->merge($parts, $boolean);
60  };
61 
62  $match_expr = null;
63  $comparison = strtolower($this->comparison);
64  switch ($comparison) {
65  case '=':
66  case 'eq':
67  case 'in':
68  if ($this->case_sensitive && $this->type == ELGG_VALUE_STRING) {
69  $x = "CAST($x as BINARY)";
70  }
71 
72  if (is_array($y) || $comparison === 'in') {
73  if (!Values::isEmpty($y)) {
74  $param = isset($type) ? $qb->param($y, $type) : $y;
75  $match_expr = $qb->expr()->in($x, $param);
76  }
77  } elseif (isset($y)) {
78  $param = isset($type) ? $qb->param($y, $type) : $y;
79  $match_expr = $qb->expr()->eq($x, $param);
80  }
81  return $match_expr;
82 
83  case '!=':
84  case '<>':
85  case 'neq':
86  case 'not in':
87  if ($this->case_sensitive && $this->type == ELGG_VALUE_STRING) {
88  $x = "CAST($x as BINARY)";
89  }
90 
91  if (is_array($y) || $comparison === 'not in') {
92  if (!Values::isEmpty($y)) {
93  $param = isset($type) ? $qb->param($y, $type) : $y;
94  $match_expr = $qb->expr()->notIn($x, $param);
95  }
96  } elseif (isset($y)) {
97  $param = isset($type) ? $qb->param($y, $type) : $y;
98  $match_expr = $qb->expr()->neq($x, $param);
99  }
100  return $match_expr;
101 
102  case 'like':
103  return $compare_with('like');
104 
105  case 'not like':
106  return $compare_with('notLike', 'AND');
107 
108  case '>':
109  case 'gt':
110  return $compare_with('gt');
111 
112  case '<':
113  case 'lt':
114  return $compare_with('lt');
115 
116  case '>=':
117  case 'gte':
118  return $compare_with('gte');
119 
120  case '<=':
121  case 'lte':
122  return $compare_with('lte');
123 
124  case 'is null':
125  return $qb->expr()->isNull($x);
126 
127  case 'is not null':
128  return $qb->expr()->isNotNull($x);
129 
130  case 'exists':
131  return "EXISTS ($y)";
132 
133  case 'not exists':
134  return "NOT EXISTS ($y)";
135 
136  default:
137  throw new DomainException("'{$this->comparison}' is not a supported comparison operator");
138  }
139  }
140 }
Utility class for building composite comparison expression.
param($value, string $type=ELGG_VALUE_STRING, 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.
$type
Definition: delete.php:21
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(public string $x, public string $comparison, public mixed $y=null, public?string $type=null, public?bool $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