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
|
|
|
use Quantum\Libraries\Database\DbalInterface; |
18
|
|
|
use Quantum\Exceptions\DatabaseException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait Criteria |
22
|
|
|
* @package Quantum\Libraries\Database\Idiorm\Statements |
23
|
|
|
*/ |
24
|
|
|
trait Criteria |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* @throws \Quantum\Exceptions\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; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
44
|
|
|
*/ |
45
|
|
|
public function criterias(...$criterias): DbalInterface |
46
|
|
|
{ |
47
|
|
|
foreach ($criterias as $criteria) { |
48
|
|
|
|
49
|
|
|
if (is_array($criteria[0])) { |
50
|
|
|
$this->orCriteria($criteria); |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->criteria(...$criteria); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
63
|
|
|
*/ |
64
|
|
|
public function having(string $column, string $operator, string $value = null): DbalInterface |
65
|
|
|
{ |
66
|
|
|
if (!key_exists($operator, $this->operators)) { |
67
|
|
|
throw DatabaseException::operatorNotSupported($operator); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$func = $this->operators[$operator]; |
71
|
|
|
$this->getOrmModel()->$func($column, $value); |
|
|
|
|
72
|
|
|
return $this; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds Criteria |
77
|
|
|
* @param string $column |
78
|
|
|
* @param string $operator |
79
|
|
|
* @param mixed $value |
80
|
|
|
* @param string|null $func |
81
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
82
|
|
|
*/ |
83
|
|
|
protected function addCriteria(string $column, string $operator, $value, string $func = null) |
84
|
|
|
{ |
85
|
|
|
if ($operator == '#=#') { |
86
|
|
|
$this->getOrmModel()->where_raw($column . ' = ' . $value); |
87
|
|
|
} else if (is_array($value) && count($value) == 1 && key($value) == 'fn') { |
88
|
|
|
$this->getOrmModel()->where_raw($column . ' ' . $operator . ' ' . $value['fn']); |
89
|
|
|
} else { |
90
|
|
|
$this->getOrmModel()->$func($column, $value); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds one or more OR criteria in brackets |
96
|
|
|
* @param array $orCriterias |
97
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
98
|
|
|
*/ |
99
|
|
|
protected function orCriteria(array $orCriterias) |
100
|
|
|
{ |
101
|
|
|
$clause = ''; |
102
|
|
|
$params = []; |
103
|
|
|
|
104
|
|
|
foreach ($orCriterias as $index => $criteria) { |
105
|
|
|
if ($index == 0) { |
106
|
|
|
$clause .= '('; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$clause .= '`' . $criteria[0] . '` ' . $criteria[1] . ' ?'; |
110
|
|
|
|
111
|
|
|
if ($index == array_key_last($orCriterias)) { |
112
|
|
|
$clause .= ')'; |
113
|
|
|
} else { |
114
|
|
|
$clause .= ' OR '; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
array_push($params, $criteria[2]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->getOrmModel()->where_raw($clause, $params); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |