Passed
Push — master ( ee99db...b62e1c )
by Anthony
03:15
created

Querybuilder::orderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
	namespace core\database;
3
	trait Querybuilder {
4
		protected $req_beginning;
5
		protected $champs = [];
6
		protected $value = [];
7
		protected $conditions = [];
8
		protected $table = [];
9
		protected $order_by;
10
11
		abstract public function query();
12
		abstract public function prepare();
13
14
		
15
		//-------------------------- QUERY BUILDER in construction no test have been done --------------------------------------------//
16
		/**
17
		 * @param string $champs
18
		 * @return $this
19
		 *
20
		 * pour initialisé une requete avec un select
21
		 */
22
		public function select($champs = "*") {
23
			$this->req_beginning = "SELECT ";
24
			$this->champs[] = $champs;
25
26
			return $this;
27
		}
28
29
		/**
30
		 * @param $champ
31
		 * @param $value
32
		 * @return $this
33
		 *
34
		 * fonction qui permet de préparer les champs et la valeur qui y sera associée
35
		 */
36
		public function insert($champ, $value) {
37
			$this->add($champ, $value);
38
39
			$this->req_beginning = "INSERT INTO ";
40
41
			return $this;
42
		}
43
44
		/**
45
		 * @param $champ
46
		 * @param $value
47
		 * @return $this
48
		 */
49
		public function update($champ, $value) {
50
			$this->add($champ, $value);
51
52
			$this->req_beginning = "UPDATE ";
53
54
			return $this;
55
		}
56
57
		/**
58
		 * @return $this
59
		 *
60
		 * fonction qui initialise un delete en base de donnée
61
		 */
62
		public function delete() {
63
			$this->req_beginning = "DELETE FROM ";
64
65
			return $this;
66
		}
67
68
		/**
69
		 * @param $champ
70
		 * @param $value
71
		 *
72
		 * fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs
73
		 * tableaux respectifs (appellée dans this->insert et this->update
74
		 */
75
		private function add($champ, $value) {
76
			if (($champ !== null) && ($value !== null)) {
77
				$this->champs[] = $champ;
78
				$this->value[] = $value;
79
			}
80
		}
81
82
		/**
83
		 * @param $table
84
		 * @return $this
85
		 *
86
		 * pour initialiser la les listes des tables ou il faudra aler chercher les données
87
		 */
88
		public function from($table) {
89
			$this->table[] = $table;
90
91
			return $this;
92
		}
93
94
		/**
95
		 * @param $table
96
		 *
97
		 * pour initialiser la table dans laquelle on va insérer les données
98
		 */
99
		public function into($table) {
100
			$this->table[] = $table;
101
102
			return $this;
103
		}
104
105
		/**
106
		 * @param $champ
107
		 * @param $cond
108
		 * @param $champ_test
109
		 * @param null $closure
110
		 * @return $this
111
		 *
112
		 * pour intialiser la ou les clauses where d'une requete
113
		 */
114
		public function where($champ, $cond, $champ_test, $closure = null) {
115
			if ( $closure === null) {
116
				$this->conditions[] = $champ.$cond.$champ_test;
117
			}
118
			else {
119
				$this->conditions[] = $champ.$cond.$champ_test." ".$closure;
120
			}
121
122
			return $this;
123
		}
124
125
		/**
126
		 * @param $order
127
		 */
128
		public function orderBy($order) {
129
			$this->order_by = " ORDER BY ".$order;
130
131
			return $this;
132
		}
133
134
		/**
135
		 * @return array
136
		 *
137
		 * fonction qui permet de récupérer un select fait sur une table
138
		 */
139
		public function get() {
140
			$requete = $this->req_beginning . implode(",", $this->champs) . " FROM " . implode(",", $this->table);
141
142
			if (!empty($this->conditions)) {
143
				$requete .= " WHERE ". implode(" ", $this->conditions);
144
			}
145
146
			if (!empty($this->order_by)) {
147
				$requete .= $this->order_by;
148
			}
149
150
			$this->unsetQueryBuilder();
151
			return $this->query($requete);
0 ignored issues
show
Unused Code introduced by
The call to Querybuilder::query() has too many arguments starting with $requete.

This check compares calls to functions or methods with their respective definitions. If the call has more 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
152
		}
153
154
		/**
155
		 * fonction utlisée pour terminer un insert ou un update dans la base de données
156
		 */
157
		public function set() {
158
			$values = array_combine($this->champs, $this->value);
159
160
			$datas = [];
161
			$count = count($this->champs);
162
			for ($i=0 ; $i<$count ; $i++) {
163
				$datas[] = $this->champs[$i]."=:".$this->champs[$i];
164
			}
165
166
			//si on a des conditions alors on sera dans un insert
167
			$requete = $this->req_beginning . implode(",", $this->table) . " SET " . implode(", ", $datas);
168
169
			if (!empty($this->conditions)) {
170
				$requete .= " WHERE " . implode(" ", $this->conditions);
171
			}
172
173
			$this->prepare($requete, $values);
0 ignored issues
show
Unused Code introduced by
The call to Querybuilder::prepare() has too many arguments starting with $requete.

This check compares calls to functions or methods with their respective definitions. If the call has more 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
174
			$this->unsetQueryBuilder();
175
		}
176
177
		/**
178
		 * fonction utilisée pour finir un delete
179
		 */
180
		public function del() {
181
			$requete = $this->req_beginning . implode(",", $this->table);
182
183
			if (!empty($this->conditions)) {
184
				$requete .= " WHERE " . implode(" ", $this->conditions);
185
			}
186
187
			$this->query($requete);
0 ignored issues
show
Unused Code introduced by
The call to Querybuilder::query() has too many arguments starting with $requete.

This check compares calls to functions or methods with their respective definitions. If the call has more 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
188
			$this->unsetQueryBuilder();
189
		}
190
191
		/**
192
		 * fonction qui détruit toutes les variables utilisées.
193
		 */
194
		private function unsetQueryBuilder() {
195
			$this->req_beginning;
196
			$this->champs = [];
197
			$this->value = [];
198
			$this->conditions = [];
199
			$this->table = [];
200
			$this->order_by = "";
201
		}
202
	}