|
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
|
|
|
parent::__construct(); |
|
14
|
|
|
|
|
15
|
|
|
if ($predicates !== null) { |
|
16
|
|
|
foreach ($predicates as $predicate) { |
|
17
|
|
|
if (is_string($predicate)) |
|
18
|
|
|
$this->and($predicate); |
|
19
|
|
|
else |
|
20
|
|
|
$this->and($predicate, $predicate->bindings()); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function __toString(): string |
|
26
|
|
|
{ |
|
27
|
|
|
if (empty($this->and)) { |
|
28
|
|
|
return ''; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return 'WHERE ' . implode(' AND ', $this->and); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function name(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return self::WHERE; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function add($predicate): self |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->andPredicate($predicate); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
public function and(string $predicate, $bindings = []): self |
|
46
|
|
|
{ |
|
47
|
|
|
$this->and[] = $predicate; |
|
48
|
|
|
|
|
49
|
|
|
if (!empty($bindings)) { |
|
50
|
|
|
$this->bindings = array_merge($this->bindings, $bindings); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function andPredicate(Predicate $predicate) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->and($predicate->__toString(), $predicate->bindings()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function andIsNull($expression) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->and(new Predicate($expression, 'IS NULL')); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function andFields(array $assoc_data, $table_name = null, $operator = '=') |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($assoc_data as $field => $value) { |
|
70
|
|
|
$column = $table_name === null ? [$field] : [$table_name, $field]; |
|
71
|
|
|
$predicate = (new Predicate($column, $operator))->withValue($value, __FUNCTION__ .'_'.implode('_', $column)); |
|
72
|
|
|
|
|
73
|
|
|
$this->and($predicate, $predicate->bindings()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function andValue($expression, $operator, $value, $bind_label = null) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->andPredicate((new Predicate($expression, $operator))->withValue($value, $bind_label)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function andIn($expression, array $values) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->andPredicate((new Predicate($expression, 'IN'))->withValues($values, __FUNCTION__)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function andLike($expression, string $value) |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->andPredicate((new Predicate($expression, 'LIKE'))->withValue($value, __FUNCTION__)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|