Completed
Push — master ( 145f77...f73370 )
by James Ekow Abaka
04:01
created

QueryParameters::setRawFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace ntentan\nibii;
4
5
6
/**
7
 * Description of QueryParameters
8
 *
9
 * @author ekow
10
 */
11
class QueryParameters
12
{
13
    private $whereClause;
14
    private $and = '';
15
    private $boundData = [];
16
    private $fields = [];
17
    private $table;
18
    private $db;
19
    private $firstOnly = false;
20
    private $eagerLoad = [];
21
    private $limit;
22
    private $offset;
23
    private $sorts = [];
24
25
    /**
26
     *
27
     * @param \ $model
0 ignored issues
show
Bug introduced by
There is no parameter named $model. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
28
     */
29 32
    public function __construct($wrapper)
30
    {
31 32
        $this->db = DriverAdapter::getDefaultInstance();
32 32
        $this->table = $wrapper->getTable();
33 32
    }
34
35 24
    public function getFields()
36
    {
37 24
        $fields = '*';
38
39 24
        if (count($this->fields) > 0) {
40 12
            $fields = implode(', ', $this->fields);
41
        }
42
43 24
        return $fields;
44
    }
45
46
    public function getEagerLoad()
47
    {
48
        return $this->eagerLoad;
49
    }
50
51 12
    public function setFields($fields)
52
    {
53 12
        $this->fields = $fields;
54 12
        return $this;
55
    }
56
57 32
    public function getTable()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
58
    {
59 32
        return $this->table;
60
    }
61
    
62 24
    public function getLimit()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
63
    {
64 24
        return $this->limit > 0 ? " LIMIT {$this->limit}" : null;
65
    }
66
    
67 24
    public function getOffset()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69 24
        return $this->offset > 0 ? " OFFSET {$this->offset}" : null;
70
    }
71
72 32
    public function getWhereClause()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
73
    {
74 32
        return $this->whereClause ? " WHERE {$this->whereClause}" : '';
75
    }
76
77 32
    public function getBoundData()
78
    {
79 32
        return $this->boundData;
80
    }
81
    
82 24
    public function getSorts()
83
    {
84 24
        return count($this->sorts) ? " ORDER BY " . implode(", ", $this->sorts) : null;
85
    }
86
87 24
    public function addFilter($field, $values = [])
88
    {
89 24
        $this->whereClause .= $this->and;
90 24
        $numValues = count($values);
91 24
        $startIndex = count($this->boundData);
92
93 24
        if ($numValues === 1) {
94 22
            $key = "filter_{$startIndex}";
95 22
            if($values[0] === null) {
96
               $this->whereClause .= "{$field} is NULL";
97
            } else {
98 22
               $this->whereClause .= "{$field} = :$key";
99 22
               $this->boundData[$key] = reset($values);
100
            }
101
        } else {
102 8
            $this->whereClause .= "{$field} IN (";
103 8
            $comma = '';
104 8
            for ($i = 0; $i < $numValues; $i++) {
105 8
                $key = "filter_" . ($startIndex + $i);
106 8
                $this->whereClause .= "$comma:$key";
107 8
                $this->boundData[$key] = $values[$i];
108 8
                $comma = ' ,';
109
            }
110 8
            $this->whereClause .= ")";
111
        }
112 24
        $this->and = ' AND ';
113 24
        return $this;
114
    }
115
116 6
    public function setRawFilter($filter, $values)
117
    {
118 6
        $this->whereClause .= "{$this->and}$filter";
119 6
        $this->boundData += $values;
120 6
    }
121
122 24
    public function setFirstOnly($firstOnly)
123
    {
124 24
        $this->firstOnly = $firstOnly;
125 24
        return $this;
126
    }
127
128 24
    public function getFirstOnly()
129
    {
130 24
        return $this->firstOnly;
131
    }
132
    
133
    public function setLimit($numItems)
134
    {
135
        $this->limit = $numItems;
136
    }
137
    
138
    public function setOffset($offset)
139
    {
140
        $this->offset = $offset;
141
    }
142
    
143
    public function addSort($field, $direction = 'ASC')
144
    {
145
        $this->sorts[] = "$field $direction";
146
    }
147
}
148