Passed
Push — master ( 1faf28...95951e )
by Anthony
02:22
created

Querybuilder::getWhereConditions()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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