Passed
Push — main ( b4b05f...898b89 )
by Sammy
03:27 queued 01:33
created

Where   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 29
c 4
b 0
f 2
dl 0
loc 85
rs 10
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A __toString() 0 7 2
A __construct() 0 10 4
A andIn() 0 3 1
A andFields() 0 10 3
A andIsNull() 0 3 1
A andPredicate() 0 3 1
A andLike() 0 3 1
A and() 0 9 2
A andValue() 0 3 1
A add() 0 3 1
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