Test Failed
Push — master ( 564013...623d43 )
by
unknown
01:34
created

SelectAdapter::setProtectedColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
    public function __construct(array $payload)
29
    {
30
        $this->payload = $payload;
31
    }
32
33
    /**
34
     * Retorna as colunas filtradas no select
35 1
     * @return array
36
     * @throws InvalidColumnNameException
37 1
     */
38 View Code Duplication
    public function getColumns()
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...
39
    {
40
        // Se nenhuma coluna foi definida todas serão retornadas
41
        if(!isset($this->payload['columns']) || !is_array($this->payload['columns']))
42
            return ['*'];
43
44
        $regex = '/^[a-zA-Z_][a-zA-Z0-9_]*$/';
45 1
        $columns = array();
46
47 1
        foreach ($this->payload['columns'] as $column){
48 1
            // Valida o nome da coluna
49
            if(!is_string($column) || !preg_match($regex, $column))
50
                throw new InvalidColumnNameException();
51
52
            $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 1
     * @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 1
    {
68
        if(!isset($this->payload['limit']))
69
            return 100;
70
71
        if(!is_numeric($this->payload['limit']))
72
            return 100;
73
74
        return (int) $this->payload['limit'];
75 1
    }
76
    
77
    /**
78 1
     * Retorna o offset de linhas
79
     *
80
     * @return int|null
81 1
     */
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 1
    {
84 1
        if(!isset($this->payload['offset']))
85 1
            return 1;
86 1
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 View Code Duplication
    public function getOrder()
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...
99
    {
100
        $allowed = ['ASC', 'DESC'];
101
102
        if(!isset($this->payload['order']) || !is_array($this->payload['order']))
103
            return '';
104
105
        $orders = array();
106
107
        foreach ($this->payload['order'] as $order)
108
        {
109
            $columns  = implode(', ', $order[0]);
110
            $command  = in_array($order[1], $allowed) ? $order[1] : 'ASC';
111
            $orders[] = "{$columns} {$command}";
112
        }
113
114
        return implode(', ', $orders);
115
    }
116
117
    /**
118
     * Retorna o where do payload de seleção
119
     *
120
     * @return array
121
     */
122 View Code Duplication
    public function getWhere()
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...
123
    {
124
        if(!is_array($this->payload['where']))
125
            return [];
126
127
        $adapter = new WhereAdapter();
128
        $where = $adapter->fromArray($this->payload['where']);
129
130
        return $where;
131
    }
132
}
133