|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BfwSql\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
class Quoting |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @const QUOTE_ALL To automatic quote all string values. |
|
11
|
|
|
* Used by SqlInsert and SqlUpdate. |
|
12
|
|
|
*/ |
|
13
|
|
|
const QUOTE_ALL = 'all'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @const QUOTE_ALL To not automatic quote string values. |
|
17
|
|
|
* Used by SqlInsert and SqlUpdate. |
|
18
|
|
|
*/ |
|
19
|
|
|
const QUOTE_NONE = 'none'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @const QUOTE_ALL To automatic quote string values only for somes columns |
|
23
|
|
|
* Used by SqlInsert and SqlUpdate. |
|
24
|
|
|
*/ |
|
25
|
|
|
const QUOTE_PARTIALLY = 'partially'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @const PARTIALLY_MODE_QUOTE Used by automatic quote system when is equal |
|
29
|
|
|
* to QUOTE_PARTIALLY. Define default quote mode to quote all columns which |
|
30
|
|
|
* is not define to not be quoted. |
|
31
|
|
|
*/ |
|
32
|
|
|
const PARTIALLY_MODE_QUOTE = 'quote'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @const PARTIALLY_MODE_NOTQUOTE Used by automatic quote system when is |
|
36
|
|
|
* equal to QUOTE_PARTIALLY. Define default quote mode to not quote all |
|
37
|
|
|
* columns which is not define to be quoted. |
|
38
|
|
|
*/ |
|
39
|
|
|
const PARTIALLY_MODE_NOTQUOTE = 'not quote'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @const ERR_COLUMN_ALREADY_DEFINE_NOT_QUOTED Exception code if the user |
|
43
|
|
|
* try to declare a column to be quoted, but the column is already declared |
|
44
|
|
|
* to be not quoted. |
|
45
|
|
|
*/ |
|
46
|
|
|
const ERR_COLUMN_ALREADY_DEFINE_NOT_QUOTED = 2701005; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @const ERR_COLUMN_ALREADY_DEFINE_QUOTED Exception code if the user try |
|
50
|
|
|
* to declared a column to be not quoted, but the column is already |
|
51
|
|
|
* declared to be quoted. |
|
52
|
|
|
*/ |
|
53
|
|
|
const ERR_COLUMN_ALREADY_DEFINE_QUOTED = 2701006; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var \BfwSql\SqlConnect $sqlConnect SqlConnect object |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $sqlConnect; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string $quoteStatus The current automic quote status. |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $quoteStatus = self::QUOTE_ALL; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var string $partiallyPreferedMode The default mode to use on column |
|
67
|
|
|
* when quoteStatus is declared to be PARTIALLY. |
|
68
|
|
|
* Value is self::PARTIALLY_MODE_QUOTE or self::PARTIALLY_MODE_NOTQUOTE |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $partiallyPreferedMode = self::PARTIALLY_MODE_QUOTE; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var array $quotedColumns List of columns where value will be quoted if |
|
74
|
|
|
* is string. |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $quotedColumns = []; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var array $notQuotedColumns List of columns where value will not be |
|
80
|
|
|
* quoted if is string. |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $notQuotedColumns = []; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Construct |
|
86
|
|
|
* Define the quoting status and the sqlConnect object |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $status Globally status for the system |
|
89
|
|
|
* Values are class constants : |
|
90
|
|
|
* * QUOTE_ALL |
|
91
|
|
|
* * QUOTE_NONE |
|
92
|
|
|
* * QUOTE_PARTIALLY |
|
93
|
|
|
* @param \BfwSql\SqlConnect $sqlConnect The sqlConnect object |
|
94
|
|
|
*/ |
|
95
|
|
|
public function __construct(string $status, \BfwSql\SqlConnect $sqlConnect) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->quoteStatus = $status; |
|
98
|
|
|
$this->sqlConnect = $sqlConnect; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Getter accessor to property sqlConnect |
|
103
|
|
|
* |
|
104
|
|
|
* @return \BfwSql\SqlConnect |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getSqlConnect(): \BfwSql\SqlConnect |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->sqlConnect; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Getter to access to quoteStatus property |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getQuoteStatus(): string |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->quoteStatus; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Getter to access to partiallyPreferedMode property |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getPartiallyPreferedMode(): string |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->partiallyPreferedMode; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Getter to access to partiallyPreferedMode property |
|
133
|
|
|
* Value should be self::PARTIALLY_MODE_QUOTE or |
|
134
|
|
|
* self::PARTIALLY_MODE_NOTQUOTE |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $partiallyPreferedMode The new prefered mode |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setPartiallyPreferedMode(string$partiallyPreferedMode): self |
|
141
|
|
|
{ |
|
142
|
|
|
$this->partiallyPreferedMode = $partiallyPreferedMode; |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Getter to access to quotedColumns property |
|
149
|
|
|
* |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getQuotedColumns(): array |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->quotedColumns; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Getter to access to notQuotedColumns property |
|
159
|
|
|
* |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getNotQuotedColumns(): array |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->notQuotedColumns; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Declare columns should be automatic quoted if value is string. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string[] $columns Columns name |
|
172
|
|
|
* |
|
173
|
|
|
* @throws Exception If the column is already declared to be not quoted |
|
174
|
|
|
* |
|
175
|
|
|
* @return $this |
|
176
|
|
|
*/ |
|
177
|
|
|
public function addQuotedColumns(array $columns): self |
|
178
|
|
|
{ |
|
179
|
|
|
foreach ($columns as $columnName) { |
|
180
|
|
|
if (isset($this->notQuotedColumns[$columnName])) { |
|
181
|
|
|
throw new Exception( |
|
182
|
|
|
'The column '.$columnName.' is already declared to be a' |
|
183
|
|
|
.' not quoted value.', |
|
184
|
|
|
self::ERR_COLUMN_ALREADY_DEFINE_NOT_QUOTED |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->quotedColumns[$columnName] = true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Declare columns should not be automatic quoted if value is string. |
|
196
|
|
|
* |
|
197
|
|
|
* @param string[] $columns Columns name |
|
198
|
|
|
* |
|
199
|
|
|
* @throws Exception If the column is already declared to be quoted |
|
200
|
|
|
* |
|
201
|
|
|
* @return $this |
|
202
|
|
|
*/ |
|
203
|
|
|
public function addNotQuotedColumns(array $columns): self |
|
204
|
|
|
{ |
|
205
|
|
|
foreach ($columns as $columnName) { |
|
206
|
|
|
if (isset($this->quotedColumns[$columnName])) { |
|
207
|
|
|
throw new Exception( |
|
208
|
|
|
'The column '.$columnName.' is already declared to be a' |
|
209
|
|
|
.' quoted value.', |
|
210
|
|
|
self::ERR_COLUMN_ALREADY_DEFINE_QUOTED |
|
211
|
|
|
); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$this->notQuotedColumns[$columnName] = true; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $this; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Quote a value if need, else return the value passed in parameter |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $columnName The column corresponding to the value |
|
224
|
|
|
* @param mixed $value The value to quote |
|
225
|
|
|
* |
|
226
|
|
|
* @return mixed |
|
227
|
|
|
*/ |
|
228
|
|
|
public function quoteValue(string $columnName, $value) |
|
229
|
|
|
{ |
|
230
|
|
|
if ($this->quoteStatus === self::QUOTE_NONE) { |
|
231
|
|
|
return $value; |
|
232
|
|
|
} elseif ($this->quoteStatus === self::QUOTE_PARTIALLY) { |
|
233
|
|
|
if (array_key_exists($columnName, $this->notQuotedColumns)) { |
|
234
|
|
|
return $value; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
if ( |
|
238
|
|
|
$this->partiallyPreferedMode === self::PARTIALLY_MODE_NOTQUOTE && |
|
239
|
|
|
array_key_exists($columnName, $this->quotedColumns) === false |
|
240
|
|
|
) { |
|
241
|
|
|
return $value; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
if (!is_string($value)) { |
|
246
|
|
|
return $value; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return $this->sqlConnect->getPDO()->quote($value); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|