hfw /
db
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Helix\DB\Fluent; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use Helix\DB; |
||
| 7 | use Helix\DB\EntityInterface; |
||
| 8 | use Helix\DB\Select; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * A logical expression that evaluates to a boolean. |
||
| 12 | */ |
||
| 13 | class Predicate extends Expression implements ValueInterface |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Null-safe equality {@link Predicate} from mixed arguments. |
||
| 18 | * |
||
| 19 | * If `$a` is an integer (enumerated item), returns `$b` as a {@link Predicate} |
||
| 20 | * |
||
| 21 | * If `$b` is a closure, returns from `$b($a, DB $this)` |
||
| 22 | * |
||
| 23 | * If `$b` is an {@link EntityInterface}, the ID is used. |
||
| 24 | * |
||
| 25 | * If `$b` is an array, returns `$a IN (...quoted $b)` |
||
| 26 | * |
||
| 27 | * If `$b` is a {@link Select}, returns `$a IN ($b->toSql())` |
||
| 28 | * |
||
| 29 | * Otherwise predicates upon `$a = quoted $b` |
||
| 30 | * |
||
| 31 | * @param scalar $a |
||
| 32 | * @param null|scalar|array|Closure|EntityInterface|Select|ValueInterface $b |
||
| 33 | * @return static |
||
| 34 | */ |
||
| 35 | public static function match(DB $db, $a, $b): Predicate |
||
| 36 | { |
||
| 37 | if ($b instanceof Closure) { |
||
| 38 | return $b->__invoke($a, $db); |
||
| 39 | } |
||
| 40 | if (is_int($a)) { |
||
| 41 | return static::factory($db, $b); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 42 | } |
||
| 43 | if (is_array($b)) { |
||
| 44 | return static::factory($db, "{$a} IN ({$db->quoteList($b)})"); |
||
|
0 ignored issues
–
show
|
|||
| 45 | } |
||
| 46 | if ($b instanceof Select) { |
||
| 47 | return static::factory($db, "{$a} IN ({$b->toSql()})"); |
||
|
0 ignored issues
–
show
|
|||
| 48 | } |
||
| 49 | if ($b === null) { |
||
| 50 | return static::factory($db, "{$a} IS NULL"); |
||
|
0 ignored issues
–
show
|
|||
| 51 | } |
||
| 52 | return static::factory($db, "{$a} = {$db->quote($b)}"); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Logical `AND` |
||
| 57 | * |
||
| 58 | * `($this AND ...)` |
||
| 59 | * |
||
| 60 | * @param string ...$predicates |
||
| 61 | * @return Predicate |
||
| 62 | */ |
||
| 63 | public function lAnd(string ...$predicates) |
||
| 64 | { |
||
| 65 | array_unshift($predicates, $this); |
||
| 66 | return static::factory($this->db, sprintf('(%s)', implode(' AND ', $predicates))); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Logical `NOT` |
||
| 71 | * |
||
| 72 | * `NOT($this)` |
||
| 73 | * |
||
| 74 | * @return static |
||
| 75 | */ |
||
| 76 | public function lNot() |
||
| 77 | { |
||
| 78 | return static::factory($this->db, "NOT({$this})"); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Logical `OR` |
||
| 83 | * |
||
| 84 | * `($this OR ...)` |
||
| 85 | * |
||
| 86 | * @param string ...$predicates |
||
| 87 | * @return Predicate |
||
| 88 | */ |
||
| 89 | public function lOr(string ...$predicates) |
||
| 90 | { |
||
| 91 | array_unshift($predicates, $this); |
||
| 92 | return static::factory($this->db, sprintf('(%s)', implode(' OR ', $predicates))); |
||
| 93 | } |
||
| 94 | |||
| 95 | } |
||
| 96 |