1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Minerva\FrontQL\Adapter\Select; |
4
|
|
|
|
5
|
|
|
use Minerva\FrontQL\Adapter\Select\Basis\Exception\InvalidColumnNameException; |
6
|
|
|
use Minerva\FrontQL\Adapter\Where\WhereAdapter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SelectPayload |
10
|
|
|
* |
11
|
|
|
* @author Lucas A. de Araújo <[email protected]> |
12
|
|
|
* @package Minerva\FrontQL\Adapter\Select |
13
|
|
|
*/ |
14
|
|
|
class SelectPayload |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Payload recebido do front-end |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $payload; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constrói o payload |
25
|
|
|
* |
26
|
|
|
* @param array $payload |
27
|
|
|
*/ |
28
|
1 |
|
public function __construct(array $payload) |
29
|
|
|
{ |
30
|
1 |
|
$this->payload = $payload; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Retorna as colunas filtradas no select |
35
|
|
|
* @return array |
36
|
|
|
* @throws InvalidColumnNameException |
37
|
|
|
*/ |
38
|
1 |
|
public function getColumns() |
39
|
|
|
{ |
40
|
|
|
// Se nenhuma coluna foi definida todas serão retornadas |
41
|
1 |
|
if(!isset($this->payload['columns']) || !is_array($this->payload['columns'])) |
42
|
1 |
|
return ['*']; |
43
|
|
|
|
44
|
1 |
|
$regex = '/^[a-zA-Z_][a-zA-Z0-9_]*$/'; |
45
|
1 |
|
$columns = array(); |
46
|
|
|
|
47
|
1 |
|
foreach ($this->payload['columns'] as $column){ |
48
|
|
|
// Valida o nome da coluna |
49
|
1 |
|
if(!is_string($column) || !preg_match($regex, $column)) |
50
|
1 |
|
throw new InvalidColumnNameException(); |
51
|
|
|
|
52
|
1 |
|
$columns[] = $column; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
if(count($columns) == 0) |
56
|
1 |
|
return ['*']; |
57
|
|
|
|
58
|
1 |
|
return $columns; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retorna o limite de linhas |
63
|
|
|
* |
64
|
|
|
* @return int|null |
65
|
|
|
*/ |
66
|
1 |
|
public function getLimit() |
67
|
|
|
{ |
68
|
1 |
|
if(!isset($this->payload['limit'])) |
69
|
1 |
|
return null; |
70
|
|
|
|
71
|
1 |
|
if(!is_numeric($this->payload['limit'])) |
72
|
1 |
|
return null; |
73
|
|
|
|
74
|
1 |
|
return (int) $this->payload['limit']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retorna o offset de linhas |
79
|
|
|
* |
80
|
|
|
* @return int|null |
81
|
|
|
*/ |
82
|
1 |
|
public function getOffset() |
83
|
|
|
{ |
84
|
1 |
|
if(!isset($this->payload['offset'])) |
85
|
1 |
|
return 1; |
86
|
|
|
|
87
|
|
|
if(!is_numeric($this->payload['offset'])) |
88
|
|
|
return 1; |
89
|
|
|
|
90
|
|
|
return (int) $this->payload['offset']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retorna a ordem definida pelo usuário em string |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
1 |
|
public function getOrder() |
99
|
|
|
{ |
100
|
1 |
|
$allowed = ['ASC', 'DESC']; |
101
|
|
|
|
102
|
1 |
|
if(!isset($this->payload['order']) || !is_array($this->payload['order'])) |
103
|
1 |
|
return null; |
104
|
|
|
|
105
|
1 |
|
$orders = array(); |
106
|
|
|
|
107
|
1 |
|
foreach ($this->payload['order'] as $order) |
108
|
|
|
{ |
109
|
1 |
|
$columns = implode(', ', $order[0]); |
110
|
1 |
|
$command = in_array($order[1], $allowed) ? $order[1] : 'ASC'; |
111
|
1 |
|
$orders[] = "{$columns} {$command}"; |
112
|
1 |
|
} |
113
|
|
|
|
114
|
1 |
|
return implode(', ', $orders); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Retorna o where do payload de seleção |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
1 |
|
public function getWhere() |
123
|
|
|
{ |
124
|
1 |
|
if(!is_array($this->payload['where'])) |
125
|
1 |
|
return []; |
126
|
|
|
|
127
|
1 |
|
$adapter = new WhereAdapter(); |
128
|
1 |
|
$where = $adapter->fromArray($this->payload['where']); |
129
|
|
|
|
130
|
1 |
|
return $where; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|