Completed
Push — master ( 391fb0...1e66af )
by Arman
24s queued 16s
created

Criteria   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A criteria() 0 8 2
A having() 0 9 2
A criterias() 0 11 4
A isNotNull() 0 4 1
A addCriteria() 0 8 5
A isNull() 0 4 1
A orCriteria() 0 22 4
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.9.7
13
 */
14
15
namespace Quantum\Libraries\Database\Adapters\Idiorm\Statements;
16
17
use Quantum\Libraries\Database\Exceptions\DatabaseException;
18
use Quantum\Libraries\Database\Contracts\DbalInterface;
19
20
/**
21
 * Trait Criteria
22
 * @package Quantum\Libraries\Database
23
 */
24
trait Criteria
25
{
26
27
    /**
28
     * @inheritDoc
29
     * @throws DatabaseException
30
     */
31
    public function criteria(string $column, string $operator, $value = null): DbalInterface
32
    {
33
        if (!key_exists($operator, $this->operators)) {
34
            throw DatabaseException::operatorNotSupported($operator);
35
        }
36
37
        $this->addCriteria($column, $operator, $value, $this->operators[$operator]);
38
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...orm\Statements\Criteria which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
39
    }
40
41
    /**
42
     * @inheritDoc
43
     * @throws DatabaseException
44
     */
45
    public function criterias(...$criterias): DbalInterface
46
    {
47
        foreach ($criterias as $criteria) {
48
            if (isset($criteria[0]) && is_array($criteria[0])) {
49
                $this->orCriteria($criteria);
50
            } else {
51
                $this->criteria(...$criteria);
0 ignored issues
show
Bug introduced by
The call to Quantum\Libraries\Databa...ts\Criteria::criteria() has too few arguments starting with operator. ( Ignorable by Annotation )

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

51
                $this->/** @scrutinizer ignore-call */ 
52
                       criteria(...$criteria);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52
            }
53
        }
54
55
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...orm\Statements\Criteria which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
56
    }
57
58
    /**
59
     * @inheritDoc
60
     * @throws DatabaseException
61
     */
62
    public function having(string $column, string $operator, string $value = null): DbalInterface
63
    {
64
        if (!key_exists($operator, $this->operators)) {
65
            throw DatabaseException::operatorNotSupported($operator);
66
        }
67
68
        $func = $this->operators[$operator];
69
        $this->getOrmModel()->$func($column, $value);
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

69
        $this->/** @scrutinizer ignore-call */ 
70
               getOrmModel()->$func($column, $value);
Loading history...
70
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...orm\Statements\Criteria which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
71
    }
72
73
    /**
74
     * @inheritDoc
75
     * @throws DatabaseException
76
     */
77
    public function isNull(string $column): DbalInterface
78
    {
79
        $this->getOrmModel()->where_null($column);
80
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...orm\Statements\Criteria which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
81
    }
82
83
    /**
84
     * @inheritDoc
85
     * @throws DatabaseException
86
     */
87
    public function isNotNull(string $column): DbalInterface
88
    {
89
        $this->getOrmModel()->where_not_null($column);
90
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...orm\Statements\Criteria which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
91
    }
92
93
    /**
94
     * Adds Criteria
95
     * @param string $column
96
     * @param string $operator
97
     * @param mixed $value
98
     * @param string|null $func
99
     * @throws DatabaseException
100
     */
101
    protected function addCriteria(string $column, string $operator, $value, string $func = null)
102
    {
103
        if ($operator == '#=#') {
104
            $this->getOrmModel()->where_raw($column . ' = ' . $value);
105
        } else if (is_array($value) && count($value) == 1 && key($value) == 'fn') {
106
            $this->getOrmModel()->where_raw($column . ' ' . $operator . ' ' . $value['fn']);
107
        } else {
108
            $this->getOrmModel()->$func($column, $value);
109
        }
110
    }
111
112
    /**
113
     * Adds one or more OR criteria in brackets
114
     * @param array $orCriterias
115
     * @throws DatabaseException
116
     */
117
    protected function orCriteria(array $orCriterias)
118
    {
119
        $clause = '';
120
        $params = [];
121
122
        foreach ($orCriterias as $index => $criteria) {
123
            if ($index == 0) {
124
                $clause .= '(';
125
            }
126
127
            $clause .= '`' . $criteria[0] . '` ' . $criteria[1] . ' ?';
128
129
            if ($index == array_key_last($orCriterias)) {
130
                $clause .= ')';
131
            } else {
132
                $clause .= ' OR ';
133
            }
134
135
            $params[] = $criteria[2];
136
        }
137
138
        $this->getOrmModel()->where_raw($clause, $params);
139
    }
140
}