|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HexMakina\Crudites\Grammar\Clause; |
|
4
|
|
|
|
|
5
|
|
|
use HexMakina\Crudites\Grammar\Predicate; |
|
6
|
|
|
|
|
7
|
|
|
class Where extends Clause |
|
8
|
|
|
{ |
|
9
|
|
|
protected array $and = []; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct(array $predicates = null) |
|
12
|
|
|
{ |
|
13
|
|
|
if ($predicates !== null){ |
|
14
|
|
|
foreach ($predicates as $predicate) { |
|
15
|
|
|
if (is_string($predicate)) |
|
16
|
|
|
$this->and($predicate); |
|
17
|
|
|
else |
|
18
|
|
|
$this->and($predicate, $predicate->bindings()); |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function __toString(): string |
|
24
|
|
|
{ |
|
25
|
|
|
if (empty($this->and)) { |
|
26
|
|
|
return ''; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return 'WHERE ' . implode(' AND ', $this->and); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function name(): string |
|
33
|
|
|
{ |
|
34
|
|
|
return self::WHERE; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function and(string $predicate, $bindings = []) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->and[] = $predicate; |
|
40
|
|
|
|
|
41
|
|
|
if (!empty($bindings)) { |
|
42
|
|
|
$this->bindings = array_merge($this->bindings, $bindings); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function andPredicate(Predicate $predicate) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->and($predicate->__toString(), $predicate->bindings()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
public function andIsNull($expression) |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->and(new Predicate($expression, 'IS NULL')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function andFields(array $assoc_data, $table_name = null, $operator = '=') |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($assoc_data as $field => $value) { |
|
62
|
|
|
$column = $table_name === null ? [$field] : [$table_name, $field]; |
|
63
|
|
|
$predicate = (new Predicate($column, $operator))->withValue($value, __FUNCTION__); |
|
64
|
|
|
|
|
65
|
|
|
$this->and($predicate, $predicate->bindings()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function andIn($expression, array $values) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->andPredicate((new Predicate($expression, 'IN'))->withValues($values, __FUNCTION__)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|