Completed
Pull Request — master (#10)
by
unknown
02:57
created

QueryBuilder::where()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Akeneo\SalesForce\Query;
4
5
/**
6
 * @author Anael Chardan <[email protected]>
7
 */
8
class QueryBuilder
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $parameters = [];
14
15
    /**
16
     * @var string
17
     */
18
    protected $query = '';
19
20
    /**
21
     * @param string $field
22
     *
23
     * @return QueryBuilder
24
     */
25
    public function select(string $field): QueryBuilder
26
    {
27
        $this->query = sprintf('SELECT %s', $field);
28
29
        return $this;
30
    }
31
32
    /**
33
     * @param string $field
34
     *
35
     * @return QueryBuilder
36
     */
37
    public function addSelect(string $field): QueryBuilder
38
    {
39
        $this->query = sprintf('%s, %s', $this->query, $field);
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param string $table
46
     *
47
     * @return QueryBuilder
48
     */
49
    public function from(string $table): QueryBuilder
50
    {
51
        $this->query = sprintf('%s FROM %s', $this->query, $table);
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param string $condition
58
     *
59
     * @return QueryBuilder
60
     */
61
    public function where(string $condition): QueryBuilder
62
    {
63
        $this->query = sprintf('%s WHERE %s', $this->query, $condition);
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $fieldName
70
     * @param string $value
71
     * @param bool   $quoted
72
     *
73
     * @return string
74
     */
75
    public function getEqualCondition(string $fieldName, string $value, bool $quoted = true): string
76
    {
77
        return $this->setOperator($fieldName, '=', $value, $quoted);
78
    }
79
80
    /**
81
     * @param string $fieldName
82
     * @param string $value
83
     * @param bool   $quoted
84
     *
85
     * @return string
86
     */
87
    public function getNotEqualCondition(string $fieldName, string $value, bool $quoted = true): string
88
    {
89
        return $this->setOperator($fieldName, '!=', $value, $quoted);
90
    }
91
92
    /**
93
     * @param string $fieldName
94
     * @param string $value
95
     * @param bool   $quoted
96
     *
97
     * @return string
98
     */
99
    public function getLikeCondition(string $fieldName, string $value, bool $quoted = true): string
100
    {
101
        return $this->setOperator($fieldName, 'LIKE', $value, $quoted);
102
    }
103
104
    /**
105
     * @param string $condition
106
     *
107
     * @return QueryBuilder
108
     */
109
    public function andWhere(string $condition): QueryBuilder
110
    {
111
        $this->query = sprintf('%s AND %s', $this->query, $condition);
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param string $condition
118
     *
119
     * @return QueryBuilder
120
     */
121
    public function orWhere(string $condition): QueryBuilder
122
    {
123
        $this->query = sprintf('%s OR %s', $this->query, $condition);
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param string $condition
0 ignored issues
show
Bug introduced by
There is no parameter named $condition. 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...
130
     *
131
     * @return QueryBuilder
132
     */
133
    public function orderBy(string $fieldName): QueryBuilder
134
    {
135
        $this->query = sprintf('%s ORDER BY %s', $this->query, $fieldName);
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param string $name
142
     * @param string $value
143
     *
144
     * @return QueryBuilder
145
     */
146
    public function setParameter(string $name, string $value): QueryBuilder
147
    {
148
        $this->parameters[$name] = $value;
149
150
        return $this;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getQuery(): string
157
    {
158
        $result = $this->query;
159
160
        foreach ($this->parameters as $parameterKey => $parameterValue) {
161
            $result = str_replace(sprintf(':%s', $parameterKey), $parameterValue, $result);
162
        }
163
164
        return $result;
165
    }
166
167
    /**
168
     * @param string $fieldName
169
     * @param string $operator
170
     * @param string $value
171
     * @param bool   $quoted
172
     *
173
     * @return string
174
     */
175
    protected function setOperator(string $fieldName, string $operator, string $value, $quoted = true): string
176
    {
177
        $quoteText = $quoted ? '\'' : '';
178
179
        return sprintf('%s %s %s%s%s', $fieldName, $operator, $quoteText, $value, $quoteText);
180
    }
181
}
182