Passed
Pull Request — master (#48)
by Arman
03:56
created

Criteria::criteria()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 6
nop 3
dl 0
loc 14
rs 10
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.6.0
13
 */
14
15
namespace Quantum\Libraries\Database\Idiorm\Statements;
16
17
/**
18
 * Trait Criteria
19
 * @package Quantum\Libraries\Database\Idiorm\Statements
20
 */
21
trait Criteria
22
{
23
24
    /**
25
     * Operators map
26
     * @var string[]
27
     */
28
    private $operators = [
29
        '=' => 'where_equal',
30
        '!=' => 'where_not_equal',
31
        '>' => 'where_gt',
32
        '>=' => 'where_gte',
33
        '<' => 'where_lt',
34
        '<=' => 'where_lte',
35
        'IN' => 'where_in',
36
        'NOT IN' => 'where_not_in',
37
        'LIKE' => 'where_like',
38
        'NOT LIKE' => 'where_not_like',
39
        'NULL' => 'where_null',
40
        'NOT NULL' => 'where_not_null',
41
    ];
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function criteria(string $column, string $operator, $value = null): object
47
    {
48
        foreach ($this->operators as $key => $method) {
49
            if ($operator == $key) {
50
                $this->addCriteria($column, $operator, $value, $method);
51
                break;
52
            }
53
        }
54
55
        if ($operator == '#=#') {
56
            $this->whereColumnsEqual($column, $value);
57
        }
58
59
        return $this->getOrmModel();
0 ignored issues
show
Bug introduced by
It seems like getOrmModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        return $this->/** @scrutinizer ignore-call */ getOrmModel();
Loading history...
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function criterias(...$criterias): object
66
    {
67
        foreach ($criterias as $criteria) {
68
69
            if (is_array($criteria[0])) {
70
                $this->scopedORCriteria($criteria);
71
                continue;
72
            }
73
74
            $value = $criteria[2] ?? null;
75
76
            $this->criteria($criteria[0], $criteria[1], $value);
77
        }
78
79
        return $this->getOrmModel();
80
    }
81
82
    /**
83
     * Compares values from two columns
84
     * @param string $columnOne
85
     * @param string $columnTwo
86
     */
87
    protected function whereColumnsEqual(string $columnOne, string $columnTwo)
88
    {
89
        $this->getOrmModel()->where_raw($columnOne . ' = ' . $columnTwo);
90
    }
91
92
    /**
93
     * Adds one or more OR criteria in brackets
94
     * @param array $criteria
95
     */
96
    protected function scopedORCriteria(array $criteria)
97
    {
98
        $clause = '';
99
        $params = [];
100
101
        foreach ($criteria as $index => $orCriteria) {
102
            if ($index == 0) {
103
                $clause .= '(';
104
            }
105
106
            $clause .= '`' . $orCriteria[0] . '` ' . $orCriteria[1] . ' ?';
107
108
            if ($index == count($criteria) - 1) {
109
                $clause .= ')';
110
            } else {
111
                $clause .= ' OR ';
112
            }
113
114
            array_push($params, $orCriteria[2]);
115
        }
116
117
        $this->getOrmModel()->where_raw($clause, $params);
118
    }
119
120
    /**
121
     * Adds Criteria
122
     * @param string $column
123
     * @param string $operator
124
     * @param mixed $value
125
     * @param string $func
126
     */
127
    protected function addCriteria(string $column, string $operator, $value, string $func)
128
    {
129
        if (is_array($value) && count($value) == 1 && key($value) == 'fn') {
130
            $this->getOrmModel()->where_raw($column . ' ' . $operator . ' ' . $value['fn']);
131
        } else {
132
            $this->getOrmModel()->$func($column, $value);
133
        }
134
    }
135
136
}