Skip to content

Commit 18408e8

Browse files
committed
Support for Literal class in Condition
1 parent a736355 commit 18408e8

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/PHPFUI/ORM/Condition.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class Condition implements \Countable, \Stringable
2121
*
2222
* Will try to parse FVO from string if $operator is null.
2323
*
24-
* @param ?string $field single name (no .) of a field existing the the table. Will try to parse FVO from string if $operator is null.
24+
* @param string|\PHPFUI\ORM\Literal|null $field single name (no .) of a field existing the the table. Will try to parse FVO from string if $operator is null.
2525
* @param mixed $value to test field against. Must be string for LIKE operators and an array for IN operators.
2626
* @param \PHPFUI\ORM\Operator $operator comparision of your choice
2727
*/
28-
public function __construct(?string $field = null, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal())
28+
public function __construct(string|\PHPFUI\ORM\Literal|null $field = null, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal())
2929
{
3030
if ($field)
3131
{
@@ -43,7 +43,8 @@ public function __toString() : string
4343

4444
foreach ($this->conditions as $parts)
4545
{
46-
if (! \is_object($parts[1]))
46+
47+
if (! \is_object($parts[1]) || $parts[1] instanceof \PHPFUI\ORM\Literal)
4748
{
4849
if ($parts[0])
4950
{
@@ -55,7 +56,14 @@ public function __toString() : string
5556

5657
if ($parts[2]->correctlyTyped($value))
5758
{
58-
$escapedField = '`' . \str_replace('.', '`.`', (string)$field) . '`';
59+
if ($field instanceof \PHPFUI\ORM\Literal)
60+
{
61+
$escapedField = (string)$field;
62+
}
63+
else
64+
{
65+
$escapedField = '`' . \str_replace('.', '`.`', (string)$field) . '`';
66+
}
5967
$retVal .= "{$first}{$escapedField}{$operator} ";
6068
$first = ' ';
6169

@@ -92,15 +100,15 @@ public function __toString() : string
92100
/**
93101
* Add logical AND between FVO tupples or Condition
94102
*/
95-
public function and(string | \PHPFUI\ORM\Condition $condition, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal()) : self
103+
public function and(string | \PHPFUI\ORM\Condition | \PHPFUI\ORM\Literal $condition, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal()) : self
96104
{
97105
return $this->add('AND', $condition, $operator, $value);
98106
}
99107

100108
/**
101109
* Add logical AND NOT between FVO tupples or Condition
102110
*/
103-
public function andNot(string | \PHPFUI\ORM\Condition $condition, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal()) : self
111+
public function andNot(string | \PHPFUI\ORM\Condition | \PHPFUI\ORM\Literal $condition, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal()) : self
104112
{
105113
return $this->add('AND NOT', $condition, $operator, $value);
106114
}
@@ -144,7 +152,7 @@ public function getInput() : array
144152

145153
foreach ($this->conditions as $parts)
146154
{
147-
if (! \is_object($parts[1]))
155+
if (! \is_object($parts[1]) || $parts[1] instanceof \PHPFUI\ORM\Literal)
148156
{
149157
$value = $parts[3];
150158

@@ -184,23 +192,23 @@ public function getJSON() : string
184192
/**
185193
* Add logical OR between FVO tupples or Condition
186194
*/
187-
public function or(string | \PHPFUI\ORM\Condition $condition, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal()) : self
195+
public function or(string | \PHPFUI\ORM\Condition | \PHPFUI\ORM\Literal $condition, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal()) : self
188196
{
189197
return $this->add('OR', $condition, $operator, $value);
190198
}
191199

192200
/**
193201
* Add logical OR NOT between FVO tupples or Condition
194202
*/
195-
public function orNot(string | \PHPFUI\ORM\Condition $condition, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal()) : self
203+
public function orNot(string | \PHPFUI\ORM\Condition | \PHPFUI\ORM\Literal $condition, mixed $value = null, \PHPFUI\ORM\Operator $operator = new \PHPFUI\ORM\Operator\Equal()) : self
196204
{
197205
return $this->add('OR NOT', $condition, $operator, $value);
198206
}
199207

200208
/**
201209
* Internal method to type check $condition parameter
202210
*/
203-
private function add(string $logical, string | \PHPFUI\ORM\Condition $condition, \PHPFUI\ORM\Operator $operator, mixed $value) : static
211+
private function add(string $logical, string | \PHPFUI\ORM\Condition | \PHPFUI\ORM\Literal $condition, \PHPFUI\ORM\Operator $operator, mixed $value) : static
204212
{
205213
if (null === $value && ! $operator->correctlyTyped($value))
206214
{
@@ -219,7 +227,7 @@ private function add(string $logical, string | \PHPFUI\ORM\Condition $condition,
219227
$logical = '';
220228
}
221229

222-
if ('string' == \gettype($condition))
230+
if ('string' == \gettype($condition) || \PHPFUI\ORM\Literal::class == $condition::class)
223231
{
224232
$this->conditions[] = [$logical, $condition, $operator, $value];
225233
}

0 commit comments

Comments
 (0)