Completed
Push — master ( e58eb5...5b23f9 )
by James Ekow Abaka
02:57
created

QueryParameters   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 2

Test Coverage

Coverage 82.95%

Importance

Changes 0
Metric Value
wmc 38
c 0
b 0
f 0
lcom 5
cbo 2
dl 0
loc 165
ccs 73
cts 88
cp 0.8295
rs 8.3999

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 9 2
A getEagerLoad() 0 3 1
A setFields() 0 4 1
A getTable() 0 3 1
A __construct() 0 3 1
A setTable() 0 4 1
A getLimit() 0 3 2
A getOffset() 0 3 2
B getWhereClause() 0 14 5
B getBoundData() 0 15 5
B setBoundData() 0 15 6
A getSorts() 0 3 2
A addFilter() 0 18 3
A setFilter() 0 7 1
A setFirstOnly() 0 4 1
A getFirstOnly() 0 3 1
A setLimit() 0 3 1
A setOffset() 0 3 1
A addSort() 0 3 1
1
<?php
2
3
namespace ntentan\nibii;
4
5
/**
6
 * Description of QueryParameters
7
 *
8
 * @author ekow
9
 */
10
class QueryParameters {
11
12
    private $whereClause;
13
    private $and = '';
14
    private $boundData = [];
15
    private $preparedBoundData = false;
16
    private $boundArrays = [];
17
    private $fields = [];
18
    private $table;
19
    private $firstOnly = false;
20
    private $eagerLoad = [];
21
    private $limit;
22
    private $offset;
23
    private $sorts = [];
24
25
    /**
26
     *
27
     * @param string $table The name of the table
28
     */
29 32
    public function __construct($table = null) {
30 32
        $this->table = $table;
31 32
    }
32
33 24
    public function getFields() {
34 24
        $fields = '*';
35
36 24
        if (count($this->fields) > 0) {
37 12
            $fields = implode(', ', $this->fields);
38
        }
39
40 24
        return $fields;
41
    }
42
43
    public function getEagerLoad() {
44
        return $this->eagerLoad;
45
    }
46
47 12
    public function setFields($fields) {
48 12
        $this->fields = $fields;
49 12
        return $this;
50
    }
51
52 32
    public function getTable() {
53 32
        return $this->table;
54
    }
55
    
56 12
    public function setTable($table) {
57 12
        $this->table = $table;
58 12
        return $this;
59
    }
60
61 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...
62 24
        return $this->limit > 0 ? " LIMIT {$this->limit}" : null;
63
    }
64
65 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...
66 24
        return $this->offset > 0 ? " OFFSET {$this->offset}" : null;
67
    }
68
69 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...
70 32
        if($this->whereClause) {
71 30
            foreach($this->boundArrays as $boundArray) {
72 8
                $where = "";
73 8
                $comma = "";
74 8
                for($i = 0; $i < count($this->boundData[$boundArray]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
75 8
                    $where .= "{$comma}:{$boundArray}_{$i}";
76 8
                    $comma = ', ';
77
                }
78 8
                $this->whereClause = str_replace("%{$boundArray}%", $where, $this->whereClause);
79
            }
80
        }
81 32
        return $this->whereClause ? " WHERE {$this->whereClause}" : '';
82
    }
83
84 32
    public function getBoundData() {
85 32
        if($this->preparedBoundData === false) {
86 32
            $this->preparedBoundData = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type boolean of property $preparedBoundData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87 32
            foreach($this->boundData as $key => $value) {
88 30
                if(in_array($key, $this->boundArrays)) {
89 8
                    foreach($value as $i => $v) {
90 8
                        $this->preparedBoundData["{$key}_{$i}"] = $v;
91
                    }
92
                } else {
93 30
                    $this->preparedBoundData[$key] = $value;
94
                }
95
            }
96
        }        
97 32
        return $this->preparedBoundData;
98
    }
99
100 4
    public function setBoundData($key, $value) {
101 4
        if(isset($this->boundData[$key])){
102 4
            $isArray = is_array($value);
103 4
            $boundArray = in_array($key, $this->boundArrays);
104 4
            if($isArray && !$boundArray) {
105
                throw new NibiiException("{$key} cannot be bound to an array");
106 4
            } else if (!$isArray && $boundArray) {
107
                throw new NibiiException("{$key} must be bound to an array");
108
            }
109 4
            $this->boundData[$key] = $value;
110 4
            $this->preparedBoundData = false;
111 4
            return $this;
112
        }
113
        throw new NibiiException("{$key} has not been bound to the current query");
114
    }
115
116 24
    public function getSorts() {
117 24
        return count($this->sorts) ? " ORDER BY " . implode(", ", $this->sorts) : null;
118
    }
119
120 24
    public function addFilter($field, $values = null) {
121 24
        $this->whereClause .= $this->and;
122
123 24
        if (is_array($values)) {
124 8
            $this->whereClause .= "{$field} IN (%{$field}%)";
125 8
            $this->boundArrays[] = $field;
126 8
            $this->boundData[$field] = $values;
127
        } else {
128 22
            if ($values === null) {
129
                $this->whereClause .= "{$field} is NULL";
130
            } else {
131 22
                $this->whereClause .= "{$field} = :$field";
132 22
                $this->boundData[$field] = $values;
133
            }
134
        }
135 24
        $this->and = ' AND ';
136 24
        return $this;
137
    }
138
139 6
    public function setFilter($filter, $values) {
140 6
        $filterCompiler = new FilterCompiler();
141 6
        $compiledFilter = $filterCompiler->compile($filter);
142 6
        $compiledValues = $filterCompiler->rewriteBoundData($values);
143 6
        $this->whereClause .= "{$this->and}$compiledFilter";
144 6
        $this->boundData += $compiledValues;
145 6
    }
146
147
    /**
148
     * @param boolean $firstOnly
149
     */
150 24
    public function setFirstOnly($firstOnly) {
151 24
        $this->firstOnly = $firstOnly;
152 24
        return $this;
153
    }
154
155 24
    public function getFirstOnly() {
156 24
        return $this->firstOnly;
157
    }
158
159
    public function setLimit($numItems) {
160
        $this->limit = $numItems;
161
    }
162
163
    public function setOffset($offset) {
164
        $this->offset = $offset;
165
    }
166
167
    /**
168
     * @param string $field
169
     */
170
    public function addSort($field, $direction = 'ASC') {
171
        $this->sorts[] = "$field $direction";
172
    }
173
174
}
175