Passed
Push — main ( 72ed59...7b3af7 )
by Sammy
01:55
created

Where   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

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