Querybuilder::getWhereConditions()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 12
nc 4
nop 0
1
<?php
2
	namespace core\database;
3
	trait Querybuilder {
4
		protected $req_beginning;
5
		protected $select_champ = [];
6
		protected $champs = [];
7
		protected $value = [];
8
		protected $champs_where = [];
9
		protected $value_where = [];
10
		protected $conditions = [];
11
		protected $conditions_table = [];
12
		protected $closure = [];
13
		protected $table = [];
14
		protected $values = [];
15
		protected $order_by;
16
		protected $group_by;
17
		protected $limit;
18
		
19
		abstract public function prepare($req, $value);
20
		
21
		
22
		//-------------------------- QUERY BUILDER --------------------------------------------//
23
		/**
24
		 * @param string $champs
25
		 * @return $this
26
		 *
27
		 * pour initialisé une requete avec un select
28
		 */
29
		public function select($champs = "*") {
30
			$this->req_beginning = "SELECT ";
31
			$this->select_champ[] = $champs;
32
			
33
			return $this;
34
		}
35
		
36
		/**
37
		 * @param $champ
38
		 * @param $value
39
		 * @return $this
40
		 *
41
		 * fonction qui permet de préparer les champs et la valeur qui y sera associée
42
		 */
43
		public function insert($champ, $value) {
44
			$this->add($champ, $value);
45
			
46
			$this->req_beginning = "INSERT INTO ";
47
			
48
			return $this;
49
		}
50
		
51
		/**
52
		 * @param $champ
53
		 * @param $value
54
		 * @return $this
55
		 */
56
		public function update($champ, $value) {
57
			$this->add($champ, $value);
58
			
59
			$this->req_beginning = "UPDATE ";
60
			
61
			return $this;
62
		}
63
		
64
		/**
65
		 * @return $this
66
		 *
67
		 * fonction qui initialise un delete en base de donnée
68
		 */
69
		public function delete() {
70
			$this->req_beginning = "DELETE FROM ";
71
			
72
			return $this;
73
		}
74
		
75
		/**
76
		 * @param string $table
77
		 * @return $this
78
		 *
79
		 * pour initialiser la les listes des tables ou il faudra aler chercher les données
80
		 */
81
		public function from($table) {
82
			$this->table[] = $table;
83
			
84
			return $this;
85
		}
86
		
87
		/**
88
		 * @param string $table
89
		 *
90
		 * pour initialiser la table dans laquelle on va insérer les données
91
		 */
92
		public function into($table) {
93
			$this->table[] = $table;
94
			
95
			return $this;
96
		}
97
		
98
		/**
99
		 * @param $champ
100
		 * @param string $cond
101
		 * @param $champ_test
102
		 * @param string $closure
103
		 * @param bool $no_bind
104
		 * @return $this
105
		 * pour intialiser la ou les clauses where d'une requete
106
		 */
107
		public function where($champ, $cond, $champ_test, $closure = "", $no_bind = false) {
108
			$this->closure[] = $closure;
109
			
110
			if ($no_bind === true) {
111
				$this->conditions_table[] = $champ.$cond.$champ_test." ".$closure;
112
			}
113
			else {
114
				$this->conditions[] = $cond;
115
				$this->addWhere($champ, $champ_test);
116
			}
117
			
118
			return $this;
119
		}
120
		
121
		/**
122
		 * @param string $order
123
		 * @param string $type
124
		 */
125
		public function orderBy($order, $type = null) {
126
			if ($type === null) {
127
				$type = "ASC";
128
			}
129
			
130
			$this->order_by = " ORDER BY ".$order." ".$type." ";
131
			
132
			return $this;
133
		}
134
		
135
		/**
136
		 * @param integer $debut
137
		 * @param integer $fin
138
		 */
139
		public function limit($debut, $fin = "no") {
140
			if ($fin == "no") {
141
				$this->limit = " LIMIT ".$debut." ";
142
			}
143
			else {
144
				$this->limit = " LIMIT ".$debut.", ".$fin." ";
145
			}
146
			
147
			
148
			return $this;
149
		}
150
		
151
		public function groupBy($name) {
152
			$this->group_by = " GROUP BY ".$name." ";
153
			
154
			return $this;
155
		}
156
		
157
		/**
158
		 * @return array
159
		 *
160
		 * fonction qui permet de récupérer un select fait sur une table
161
		 */
162
		public function get() {
163
			$values = [];
164
			$requete = $this->req_beginning.implode(",", $this->select_champ)." FROM ".implode(",", $this->table);
165
			if ((!empty($this->conditions)) || (!empty($this->conditions_table))) {
166
				$requete .= $this->getWhereConditions()[0];
167
				$values = $this->getWhereConditions()[1];
168
			}
169
			
170
			$requete .= $this->group_by;
171
			
172
			$requete .= $this->order_by;
173
			
174
			$requete .= $this->limit;
175
			
176
			$this->unsetQueryBuilder();
177
			return $this->prepare($requete, $values);
178
		}
179
		
180
		/**
181
		 * fonction utlisée pour terminer un insert ou un update dans la base de données
182
		 */
183
		public function set() {
184
			$this->values = array_combine($this->champs, $this->value);
185
			$datas = [];
186
			$count = count($this->champs);
187
			for ($i = 0; $i < $count; $i++) {
188
				$datas[] = $this->champs[$i]."=:".$this->champs[$i];
189
			}
190
			
191
			//si on a des conditions alors on sera dans un insert
192
			$requete = $this->req_beginning.implode(",", $this->table)." SET ".implode(", ", $datas);
193
			
194
			if ((!empty($this->conditions)) || (!empty($this->conditions_table))) {
195
				$requete .= $this->getWhereConditions()[0];
196
				$this->setValues();
197
			}
198
			
199
			$requete .= $this->limit;
200
			
201
			$this->prepare($requete, $this->values);
202
			$this->unsetQueryBuilder();
203
		}
204
		
205
		/**
206
		 * fonction utilisée pour finir un delete
207
		 */
208
		public function del() {
209
			$requete = $this->req_beginning.implode(",", $this->table);
210
			
211
			if (!empty($this->conditions)) {
212
				$requete .= $this->getWhereConditions()[0];
213
				$this->setValues();
214
			}
215
			
216
			$requete .= $this->order_by;
217
			
218
			$requete .= $this->limit;
219
			
220
			$this->prepare($requete, $this->values);
221
			$this->unsetQueryBuilder();
222
		}
223
		
224
		
225
		
226
		//-------------------------- PRIVATE FUNCTIONS --------------------------------------------//
227
		/**
228
		 * @param $champ
229
		 * @param $value
230
		 *
231
		 * fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs
232
		 * tableaux respectifs (appellée dans this->insert et this->update
233
		 */
234
		private function add($champ, $value) {
235
			if (($champ !== null) && ($value !== null)) {
236
				$this->champs[] = $champ;
237
				$this->value[] = $value;
238
			}
239
		}
240
		
241
		/**
242
		 * @param $champ
243
		 * @param $value
244
		 *
245
		 * fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs
246
		 * tableaux respectifs (appellée dans this->insert et this->update
247
		 */
248
		private function addWhere($champ, $value) {
249
			if (($champ !== null) && ($value !== null)) {
250
				$this->champs_where[] = $champ;
251
				$this->value_where[] = $value;
252
			}
253
		}
254
		
255
		/**
256
		 * @return array
257
		 * crée les tableau et renvoi la clause where
258
		 */
259
		private function getWhereConditions() {
260
			$values = [];
261
			$datas = [];
262
			
263
			if ((!empty($this->conditions))) {
264
				$values = array_combine(str_replace(".", "", $this->champs_where), $this->value_where);
265
				
266
				$count = count($this->champs_where);
267
				
268
				for ($i = 0; $i < $count; $i++) {
269
					$datas[] = $this->champs_where[$i]." ".$this->conditions[$i]." :".str_replace(".", "", $this->champs_where[$i])." ".$this->closure[$i]." ";
270
				}
271
			}
272
			
273
			if ((!empty($this->conditions_table))) {
274
				foreach ($this->conditions_table as $cond) {
275
					$datas[] = $cond;
276
				}
277
			}
278
			
279
			return [" WHERE ".implode(" ", $datas), $values];
280
		}
281
		
282
		/**
283
		 * function that set values for insert update and delete
284
		 */
285
		private function setValues() {
286
			$this->values = array_merge($this->values, $this->getWhereConditions()[1]);
287
		}
288
		
289
		/**
290
		 * fonction qui détruit toutes les variables utilisées.
291
		 */
292
		private function unsetQueryBuilder() {
293
			$this->req_beginning;
294
			$this->select_champ = [];
295
			$this->champs = [];
296
			$this->value = [];
297
			$this->values = [];
298
			$this->champs_where = [];
299
			$this->value_where = [];
300
			$this->conditions = [];
301
			$this->conditions_table = [];
302
			$this->closure = [];
303
			$this->table = [];
304
			$this->order_by = "";
305
			$this->group_by = "";
306
			$this->limit = "";
307
		}
308
	}