1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 6/4/14 |
5
|
|
|
* Time: 12:02 AM. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PlaceholderWriter. |
15
|
|
|
*/ |
16
|
|
|
class PlaceholderWriter |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $counter = 1; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $placeholders = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function get() |
32
|
|
|
{ |
33
|
|
|
return $this->placeholders; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function reset() |
40
|
|
|
{ |
41
|
|
|
$this->counter = 1; |
42
|
|
|
$this->placeholders = []; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $value |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function add($value) |
53
|
|
|
{ |
54
|
|
|
$placeholderKey = ':v'.$this->counter; |
55
|
|
|
$this->placeholders[$placeholderKey] = $this->setValidSqlValue($value); |
56
|
|
|
|
57
|
|
|
++$this->counter; |
58
|
|
|
|
59
|
|
|
return $placeholderKey; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $value |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function setValidSqlValue($value) |
68
|
|
|
{ |
69
|
|
|
$value = $this->writeNullSqlString($value); |
70
|
|
|
$value = $this->writeStringAsSqlString($value); |
71
|
|
|
$value = $this->writeBooleanSqlString($value); |
72
|
|
|
|
73
|
|
|
return $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $value |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function writeNullSqlString($value) |
82
|
|
|
{ |
83
|
|
|
if (\is_null($value) || (\is_string($value) && empty($value))) { |
84
|
|
|
$value = $this->writeNull(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function writeNull() |
94
|
|
|
{ |
95
|
|
|
return 'NULL'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $value |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected function writeStringAsSqlString($value) |
104
|
|
|
{ |
105
|
|
|
if (\is_string($value)) { |
106
|
|
|
$value = $this->writeString($value); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $value |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function writeString($value) |
118
|
|
|
{ |
119
|
|
|
return $value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $value |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function writeBooleanSqlString($value) |
128
|
|
|
{ |
129
|
|
|
if (\is_bool($value)) { |
130
|
|
|
$value = $this->writeBoolean($value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $value; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param bool $value |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function writeBoolean($value) |
142
|
|
|
{ |
143
|
|
|
$value = \filter_var($value, FILTER_VALIDATE_BOOLEAN); |
144
|
|
|
|
145
|
|
|
return ($value) ? '1' : '0'; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|