Passed
Branch v1.4.0 (c6dc92)
by Wanderson
01:17
created

Orm::orderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Database;
4
5
use Win\Contracts\Database\Orm\Model;
6
use Win\Database\Sql\Delete;
7
use Win\Database\Sql\Insert;
8
use Win\Database\Sql\Select;
9
use Win\Database\Sql\Update;
10
use Win\Singleton\SingletonTrait;
11
12
/**
13
 * Object Relational Mapping
14
 */
15
abstract class Orm
16
{
17
	/** @var string */
18
	protected $table;
19
20
	/** @var Model */
21
	protected $model;
22
23
	/** @var bool */
24
	protected $debug;
25
26
	/** @var Connection */
27
	protected static $db;
28
29
	use SingletonTrait {
30
		__construct as finalConstruct;
31
	}
32
33
	/** @var Select */
34
	private $query;
35
36
	public function __construct()
37
	{
38
		$this->query = new Select($this);
39
	}
40
41
	/** @param Connection $db */
42
	public static function setConnection(Connection $db)
43
	{
44
		static::$db = $db;
45
	}
46
47
	/** @return Connection */
48
	public static function getConnection()
49
	{
50
		return static::$db;
51
	}
52
53
	/**
54
	 * @param mixed[] $row
55
	 * @return Model
56
	 */
57
	abstract public function mapModel($row);
58
59
	/**
60
	 * @param Model $model
61
	 * @return mixed[]
62
	 */
63
	abstract public function mapRow($model);
64
65
	/** @return mixed[] */
66
	public function getRowValues()
67
	{
68
		return $this->mapRow($this->getModel());
69
	}
70
71
	/** Liga o debug */
72
	public function debugOn()
73
	{
74
		$this->debug = true;
75
	}
76
77
	/** Desliga o debug */
78
	public function debugOff()
79
	{
80
		$this->debug = false;
81
	}
82
83
	/** @return string */
84
	public function getTable()
85
	{
86
		return $this->table;
87
	}
88
89
	/** @return Model */
90
	public function getModel()
91
	{
92
		return $this->model;
93
	}
94
95
	/** @return bool */
96
	public function getDebugMode()
97
	{
98
		return $this->debug;
99
	}
100
101
	/** @return bool */
102
	public function modelExists()
103
	{
104
		return $this->model->getId() > 0;
105
	}
106
107
	/**
108
	 * Retorna o primeiro resultado da consulta
109
	 */
110
	public function one()
111
	{
112
		$rows = $this->query->execute();
113
114
		return $this->mapModel($rows[0]);
115
	}
116
117
	/**
118
	 * Retorna todos os resultado da consulta
119
	 */
120
	public function all()
121
	{
122
		$rows = $this->query->execute();
123
		$all = [];
124
		foreach ($rows as $row) {
125
			$all[] = $this->mapModel($row);
126
		}
127
128
		return $all;
129
	}
130
131
	/** @return int */
132
	public function numRows()
133
	{
134
		$count = $this->query->count();
135
136
		return $count;
137
	}
138
139
	/**
140
	 * Define as colunas do resultado
141
	 * @param string[] $columns
142
	 * @return static
143
	 */
144
	public function setColumns($columns)
145
	{
146
		$this->query->columns = $columns;
147
148
		return $this;
149
	}
150
151
	/**
152
	 * Adiciona uma coluna do resultado
153
	 * @param string $column
154
	 * @return static
155
	 */
156
	public function addColumn($column)
157
	{
158
		$this->query->columns[] = $column;
159
160
		return $this;
161
	}
162
163
	/**
164
	 * Filtra pelo id
165
	 * @param int $id
166
	 * @return static
167
	 */
168
	public function find($id)
169
	{
170
		$this->filterBy('id', $id);
171
172
		return $this;
173
	}
174
175
	/**
176
	 * Filtra pelo campo
177
	 * @param string $column
178
	 * @param mixed $value
179
	 * @return static
180
	 */
181
	public function filterBy($column, $value)
182
	{
183
		$this->filter($column, '=', $value);
184
185
		return $this;
186
	}
187
188
	/**
189
	 * Adiciona filtros para busca
190
	 * @return static
191
	 */
192
	public function filter($column, $operator, $value)
193
	{
194
		$this->query->where->add($column, $operator, $value);
195
196
		return $this;
197
	}
198
199
	/**
200
	 * Ordena por um campo
201
	 * @param string $orderBy
202
	 * @return static
203
	 */
204
	public function orderBy($orderBy)
205
	{
206
		$this->query->orderBy->set($orderBy);
207
208
		return $this;
209
	}
210
211
	/**
212
	 * Limita os resultados
213
	 * @param int $limit
214
	 * @return static
215
	 */
216
	public function limit($limit)
217
	{
218
		$this->query->limit->set($limit);
219
220
		return $this;
221
	}
222
223
	/**
224
	 * Ordena pelos mais novos
225
	 * @return static
226
	 */
227
	public function newer()
228
	{
229
		$this->orderBy('id DESC');
230
231
		return $this;
232
	}
233
234
	/**
235
	 * Ordena pelos mais antigos
236
	 * @return static
237
	 */
238
	public function older()
239
	{
240
		$this->orderBy('id ASC');
241
242
		return $this;
243
	}
244
245
	/**
246
	 * @param Model $model
247
	 * @return bool
248
	 */
249
	public function save(Model $model)
250
	{
251
		$this->model = $model;
252
253
		return $this->insertOrUpdate();
254
	}
255
256
	/** @return bool */
257
	private function insertOrUpdate()
258
	{
259
		if (!$this->modelExists()) {
260
			$success = $this->insert();
261
		} else {
262
			$success = $this->update();
263
		}
264
265
		return $success;
266
	}
267
268
	/** @return bool */
269
	private function insert()
270
	{
271
		$query = new Insert($this);
272
		$success = $query->execute();
273
		$this->model->setId(static::$db->getLastInsertId());
0 ignored issues
show
Bug introduced by
static::db->getLastInsertId() of type string is incompatible with the type integer expected by parameter $id of Win\Contracts\Database\Orm\Model::setId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

273
		$this->model->setId(/** @scrutinizer ignore-type */ static::$db->getLastInsertId());
Loading history...
274
275
		return $success;
276
	}
277
278
	/** @return bool */
279
	public function update()
280
	{
281
		$query = new Update($this);
282
283
		return $query->execute();
284
	}
285
286
	/**
287
	 * Remove o registro do banco
288
	 * @param Model $model
289
	 * @return bool
290
	 */
291
	public function delete(Model $model)
292
	{
293
		$query = new Delete($this);
294
		$query->where->add('id', '=', $model->getId());
295
296
		return $query->execute();
297
	}
298
}
299