Test Failed
Push — master ( 5055ab...22d9a4 )
by
unknown
01:52
created

SelectPayload::getOrder()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 10
nc 4
nop 0
crap 5
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 View Code Duplication
    public function getLimit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 1
        if(!isset($this->payload['limit']))
69 1
            return null;
70
71 1
        if(!is_int($this->payload['limit']))
72 1
            return null;
73
74 1
        return $this->payload['limit'];
75
    }
76
    
77
    /**
78
     * Retorna o offset de linhas
79
     *
80
     * @return int|null
81
     */
82 1 View Code Duplication
    public function getOffset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 1
        if(!isset($this->payload['offset']))
85 1
            return null;
86
87
        if(!is_int($this->payload['offset']))
88
            return null;
89
90
        return $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 '';
104
105 1
        $orders = array();
106
107 1
        foreach ($this->payload['order'] as $order){
108 1
            $columns  = implode(', ', $order[0]);
109 1
            $command  = in_array($order[1], $allowed) ? $order[1] : 'ASC';
110 1
            $orders[] = "{$columns}, {$command}";
111 1
        }
112
113 1
        return implode(', ', $orders);
114
    }
115
116
    /**
117
     * Retorna o where do payload de seleção
118
     *
119
     * @return array
120
     */
121 1
    public function getWhere()
122
    {
123 1
        if(!is_array($this->payload['where']))
124 1
            return [];
125
126 1
        $adapter = new WhereAdapter();
127 1
        $where = $adapter->fromArray($this->payload['where']);
128
129 1
        return $where;
130
    }
131
}
132