Completed
Pull Request — master (#6)
by
unknown
04:05
created

QueryBuilder::setParameter()   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 2
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 $name
118
     * @param string $value
119
     *
120
     * @return QueryBuilder
121
     */
122
    public function setParameter(string $name, string $value): QueryBuilder
123
    {
124
        $this->parameters[$name] = $value;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getQuery(): string
133
    {
134
        $result = $this->query;
135
136
        foreach ($this->parameters as $parameterKey => $parameterValue) {
137
            $result = str_replace(sprintf(':%s', $parameterKey), $parameterValue, $result);
138
        }
139
140
        return $result;
141
    }
142
143
    /**
144
     * @param string $fieldName
145
     * @param string $operator
146
     * @param string $value
147
     * @param bool   $quoted
148
     *
149
     * @return string
150
     */
151
    protected function setOperator(string $fieldName, string $operator, string $value, $quoted = true): string
152
    {
153
        $quoteText = $quoted ? '\'' : '';
154
155
        return sprintf('%s %s %s%s%s', $fieldName, $operator, $quoteText, $value, $quoteText);
156
    }
157
}
158