Completed
Push — master ( e43f1a...57db71 )
by Arman
26s queued 12s
created

Criteria   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 45
c 1
b 0
f 0
dl 0
loc 116
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A criteria() 0 15 4
A scopedORCriteria() 0 22 4
A criterias() 0 15 3
A addCriteria() 0 6 4
A whereColumnsEqual() 0 3 1
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.4.0
13
 */
14
namespace Quantum\Libraries\Database\Statements;
15
16
/**
17
 * Trait Criteria
18
 * @package Quantum\Libraries\Database\Statements
19
 */
20
trait Criteria
21
{
22
23
    /**
24
     * Operators map
25
     * @var string[]
26
     */
27
    private $map = [
28
        '=' => 'where_equal',
29
        '!=' => 'where_not_equal',
30
        '>' => 'where_gt',
31
        '>=' => 'where_gte',
32
        '<' => 'where_lt',
33
        '<=' => 'where_lte',
34
        'IN' => 'where_in',
35
        'NOT IN' => 'where_not_in',
36
        'LIKE' => 'where_like',
37
        'NOT LIKE' => 'where_not_like',
38
        'NULL' => 'where_null',
39
        'NOT NULL' => 'where_not_null',
40
    ];
41
42
    /**
43
     * Adds a criteria to query
44
     * @inheritDoc
45
     */
46
    public function criteria(string $column, string $operator, $value = null): object
47
    {
48
49
        foreach ($this->map as $key => $method) {
50
            if ($operator == $key) {
51
                $this->addCriteria($column, $operator, $value, $method);
52
                break;
53
            }
54
        }
55
56
        if ($operator == '#=#') {
57
            $this->whereColumnsEqual($column, $value);
58
        }
59
60
        return $this->ormObject;
61
    }
62
63
    /**
64
     * Adds many where criteria
65
     * @param array ...$criterias
66
     * @return object
67
     */
68
    public function criterias(...$criterias): object
69
    {
70
        foreach ($criterias as $criteria) {
71
72
            if (is_array($criteria[0])) {
73
                $this->scopedORCriteria($criteria);
74
                continue;
75
            }
76
77
            $value = $criteria[2] ?? null;
78
79
            $this->criteria($criteria[0], $criteria[1], $value);
80
        }
81
82
        return $this->ormObject;
83
    }
84
85
    /**
86
     * Compares values from two columns
87
     * @param string $columnOne
88
     * @param string $columnTwo
89
     */
90
    protected function whereColumnsEqual(string $columnOne, string $columnTwo)
91
    {
92
        $this->ormObject->where_raw($columnOne . ' = ' . $columnTwo);
93
    }
94
95
    /**
96
     * Adds one or more OR criteria in brackets
97
     * @param array $criteria
98
     */
99
    protected function scopedORCriteria(array $criteria)
100
    {
101
        $clause = '';
102
        $params = [];
103
104
        foreach ($criteria as $index => $orCriteria) {
105
            if ($index == 0) {
106
                $clause .= '(';
107
            }
108
109
            $clause .= '`' . $orCriteria[0] . '` ' . $orCriteria[1] . ' ?';
110
111
            if ($index == count($criteria) - 1) {
112
                $clause .= ')';
113
            } else {
114
                $clause .= ' OR ';
115
            }
116
117
            array_push($params, $orCriteria[2]);
118
        }
119
120
        $this->ormObject->where_raw($clause, $params);
121
    }
122
123
    /**
124
     * Adds Criteria
125
     * @param string $column
126
     * @param string $operator
127
     * @param mixed $value
128
     * @param string $func
129
     */
130
    protected function addCriteria(string $column, string $operator, $value, string $func)
131
    {
132
        if (is_array($value) && count($value) == 1 && key($value) == 'fn') {
133
            $this->ormObject->where_raw($column . ' ' . $operator . ' ' . $value['fn']);
134
        } else {
135
            $this->ormObject->$func($column, $value);
136
        }
137
    }
138
139
}