Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ConditionParser.php
Go to the documentation of this file.
1<?php
2namespace Ubiquity\orm\parser;
3
6
18
19 private $firstPart;
20
21 private $condition;
22
23 private $parts = [];
24
25 private $params;
26
27 private $invertedParams = true;
28
29 public function __construct($condition = null, $firstPart = null, $params = null) {
30 $this->condition = $condition;
31 $this->firstPart = $firstPart;
32 if (\is_array($params)) {
33 $this->setParams($params);
34 }
35 }
36
37 public function prepareKeys(array $keys){
38 $keyValues=\array_values($keys);
39 $retArray=[];
40 foreach ($keyValues as $key) {
41 $retArray[] = SqlUtils::$quote . $key . SqlUtils::$quote . ' = ?';
42 }
43 $this->condition = \implode(' AND ', $retArray);
44 }
45
46 public function addKeyValues($keyValues, $classname, $separator = ' AND ') {
47 if (! \is_array($keyValues)) {
48 $this->condition = $this->parseKey($keyValues, $classname);
49 } else {
50 if ((\array_keys($keyValues) === \range(0, \count($keyValues) - 1))) { // Not associative array
51 if (isset($classname)) {
52 $keys = OrmUtils::getKeyFields($classname);
53 if (\is_array($keys)) {
54 $keyValues = \array_combine($keys, $keyValues);
55 }
56 }
57 }
58 $retArray = array();
59 $this->invertedParams=false;
60 foreach ($keyValues as $key => $value) {
61 if ($value!=null) {
62 $this->params[] = $value;
63 $retArray[] = SqlUtils::$quote . $key . SqlUtils::$quote . ' = ?';
64 }
65 }
66 $this->condition = \implode($separator, $retArray);
67 }
68 }
69
70 public function setKeyValues($values) {
71 $this->invertedParams=false;
72 if (! \is_array($values)) {
73 return $this->params=[$values];
74 } else {
75 return $this->params = \array_values($values);
76 }
77 }
78
79 private function addParams($value) {
80 if ($value!=null && ! isset($this->params[$value])) {
81 return $this->params[$value] = true;
82 }
83 return false;
84 }
85
86 public function addPart($condition, $value) {
87 if ($this->addParams($value)) {
88 $this->parts[] = $condition;
89 return true;
90 }
91 return false;
92 }
93
94 public function addParts($condition, $values) {
95 foreach ($values as $value) {
96 if ($this->addParams($value)) {
97 $this->parts[] = $condition;
98 }
99 }
100 }
101
102 public function compileParts($separator = ' OR ') {
103 if ($separator == ' OR ' && \count($this->parts) > 3) {
104 $parts = $this->refactorParts();
105 $conditions = [];
106 foreach ($parts as $part => $values) {
107 $values[0] = 'SELECT ? as _id';
108 $conditions[] = ' INNER JOIN (' . \implode(' UNION ALL SELECT ', $values) . ') as _tmp ON ' . $part . '=(_tmp._id * 1)';
109 }
110 $this->condition = \implode(' ', $conditions);
111 } else {
112 $this->condition = \implode($separator, $this->parts);
113 }
114 }
115
116 private function refactorParts() {
117 $result = [];
118 foreach ($this->parts as $part) {
119 $part = \str_replace('= ?', '', $part);
120 $result[$part][] = '?';
121 }
122 return $result;
123 }
124
125 private function parseKey($keyValues, $className) {
126 $condition = $keyValues;
127 if (\strrpos($keyValues, '=') === false && \strrpos($keyValues, '>') === false && \strrpos($keyValues, '<') === false) {
128 if ($this->addParams($keyValues)) {
129 $condition = SqlUtils::$quote . OrmUtils::getFirstKey($className) . SqlUtils::$quote . '= ?';
130 }
131 }
132 return $condition;
133 }
134
139 public function getCondition() {
140 if ($this->firstPart == null)
141 return $this->condition;
142 $ret = $this->firstPart;
143 if (isset($this->condition)) {
144 $ret .= ' WHERE ' . $this->condition;
145 }
146 return $ret;
147 }
148
153 public function getParams() {
154 if (\is_array($this->params)) {
155 if ($this->invertedParams) {
156 return \array_keys($this->params);
157 }
158 return $this->params;
159 }
160 return;
161 }
162
167 public function hasParam($value) {
168 if (\is_array($this->params)) {
169 if ($this->invertedParams) {
170 return isset($this->params[$value]);
171 }
172 return \array_search($value, $this->params) !== false;
173 }
174 return false;
175 }
176
177 public function countParts() {
178 if (\is_array($this->params))
179 return \count($this->params);
180 return 0;
181 }
182
187 public function setCondition($condition) {
188 $this->condition = $condition;
189 return $this;
190 }
191
196 public function setParams($params) {
197 $this->params = $params;
198 $this->invertedParams = false;
199 return $this;
200 }
201
202 public function limitOne() {
203 $limit = '';
204 if (\stripos($this->condition, ' limit ') === false) {
205 $limit = ' limit 1';
206 }
207 $this->condition .= $limit;
208 }
209
210 public static function simple($condition, $params) {
211 $cParser = new ConditionParser($condition);
212 $cParser->addParams($params);
213 return $cParser;
214 }
215}
216
SQL utilities.
Definition SqlUtils.php:13
Object/relational mapping utilities.
Definition OrmUtils.php:17
Represents a query condition.
addKeyValues($keyValues, $classname, $separator=' AND ')
__construct($condition=null, $firstPart=null, $params=null)
static simple($condition, $params)
static getKeyFields($instance)
Return primary key fields from instance or model class.