Total Complexity | 62 |
Total Lines | 310 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like MySQLBindings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MySQLBindings, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class MySQLBindings implements BindingsInterface, Constants |
||
12 | { |
||
13 | /** |
||
14 | * Bind a boolean value as bool, with NULL option or with integer option. |
||
15 | * |
||
16 | * @param string $name |
||
17 | * @param string|int|bool|null $value |
||
18 | * @param bool $null |
||
19 | * @param bool $int |
||
20 | * |
||
21 | * @return array |
||
22 | * @throws Exception |
||
23 | */ |
||
24 | public function bBool($value = null, bool $null = false, bool $int = false): array |
||
25 | { |
||
26 | // use NULL |
||
27 | if ($value === null && $null) { |
||
28 | return $this->bStr(null, true); |
||
29 | } |
||
30 | |||
31 | if ($value === null && $null === false) { |
||
32 | throw new DomainException('Can not bind NULL in boolean spot.'); |
||
33 | } |
||
34 | |||
35 | $value = (bool)$value; |
||
36 | $value = $int ? (int)$value : $value; |
||
37 | $type = $int ? PDO::PARAM_INT : PDO::PARAM_BOOL; |
||
38 | |||
39 | return [$value, $type]; |
||
40 | |||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Bind a date value as date or optional NULL. |
||
45 | * YYYY-MM-DD is the proper date format. |
||
46 | * |
||
47 | * @param string|null $value |
||
48 | * @param bool $null |
||
49 | * |
||
50 | * @return array |
||
51 | * @throws Exception |
||
52 | */ |
||
53 | public function bDate($value = null, bool $null = false): array |
||
54 | { |
||
55 | if ($value === null && !$null) { |
||
56 | throw new DomainException('Can not bind NULL in date spot.'); |
||
57 | } |
||
58 | |||
59 | $d = null; |
||
60 | |||
61 | if ($value !== null) { |
||
62 | $value = trim($value); |
||
63 | $d = preg_match(self::DATE_REGEX, $value); |
||
64 | } |
||
65 | |||
66 | // Use NULL? |
||
67 | if ($d === null && $null) { |
||
68 | return $this->bStr(null, true); |
||
69 | } |
||
70 | |||
71 | return $this->bStr($d ? $value : '1970-01-01'); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Bind a date value as date time or optional NULL. |
||
76 | * YYYY-MM-DD HH:MM:SS is the proper date format. |
||
77 | * |
||
78 | * @param string|null $value |
||
79 | * @param bool $null |
||
80 | * |
||
81 | * @return array |
||
82 | * @throws Exception |
||
83 | */ |
||
84 | public function bDateTime($value = null, bool $null = false): array |
||
85 | { |
||
86 | if ($value === null && !$null) { |
||
87 | throw new DomainException('Can not bind NULL in date time spot.'); |
||
88 | } |
||
89 | |||
90 | $dt = 0; |
||
91 | |||
92 | if ($value !== null) { |
||
93 | // Trim $value and see if it matches full date time string format. |
||
94 | $dt = preg_match(self::DATE_TIME_REGEX, trim($value)); |
||
95 | } |
||
96 | |||
97 | // Use NULL? |
||
98 | if ($dt === 0 && $null) { |
||
99 | return $this->bStr(null, true); |
||
100 | } |
||
101 | |||
102 | if ($dt === 0 && $value !== null) { |
||
103 | if (preg_match(self::DATE_REGEX, $value) === 0) { |
||
104 | // $value is not a valid date string, set to earliest date time available (GMT). |
||
105 | $value = '1970-01-01 00:00:00'; |
||
106 | } else { |
||
107 | // $value is a valid date string, add midnight time. |
||
108 | $value .= ' 00:00:00'; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | // DateTimes are really strings. |
||
113 | return $this->bStr($value); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Bind a float. |
||
118 | * |
||
119 | * @param string|int|float|null $value |
||
120 | * @param int $decimals |
||
121 | * @param bool $null |
||
122 | * |
||
123 | * @return array |
||
124 | * @throws Exception |
||
125 | */ |
||
126 | public function bFloat($value = null, $decimals = 3, $null = false): array |
||
127 | { |
||
128 | // Use NULL? |
||
129 | if ($value === null && $null) { |
||
130 | return $this->bRaw('NULL'); |
||
131 | } |
||
132 | |||
133 | if ($value === null && !$null) { |
||
134 | throw new DomainException('Can not bind NULL in float spot.'); |
||
135 | } |
||
136 | |||
137 | if (!is_numeric($value)) { |
||
138 | throw new DomainException('Can not bind "' . $value . '" in float spot.'); |
||
139 | } |
||
140 | |||
141 | $format = sprintf('%%0.%df', $decimals); |
||
142 | |||
143 | // Apparently using PDO::PARAM_STR makes this fail! |
||
144 | return $this->bRaw(sprintf($format, $value)); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Bind an integer with optional NULL. |
||
149 | * |
||
150 | * @param string|int|float|bool|null $value |
||
151 | * @param bool $null |
||
152 | * |
||
153 | * @return array |
||
154 | * @throws Exception |
||
155 | */ |
||
156 | public function bInt($value = null, bool $null = false): array |
||
157 | { |
||
158 | // Use NULL? |
||
159 | if ($value === null && $null) { |
||
160 | return $this->bStr(null, true); |
||
161 | } |
||
162 | |||
163 | if ($value === null && !$null) { |
||
164 | throw new DomainException('Can not bind NULL in integer spot.'); |
||
165 | } |
||
166 | |||
167 | if (!is_numeric($value)) { |
||
168 | throw new DomainException('Can not bind "' . $value . '" in integer spot.'); |
||
169 | } |
||
170 | |||
171 | $value = sprintf('%u', $value); |
||
172 | |||
173 | return [(int)$value, PDO::PARAM_INT]; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Convert array of integers to comma separated values. Uses %% |
||
178 | * Great for IN() statements. |
||
179 | * |
||
180 | * @param array $data |
||
181 | * @param int $default |
||
182 | * |
||
183 | * @return array |
||
184 | * @throws Exception |
||
185 | */ |
||
186 | public function bIntArray(array $data, int $default = 0): array |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Bind a object or JSON string to a string |
||
209 | * |
||
210 | * @param string|object|null $value |
||
211 | * @param bool $null |
||
212 | * |
||
213 | * @return array |
||
214 | * @throws \JsonException |
||
215 | */ |
||
216 | public function bJSON($value, bool $null = false): array |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Create and bind string for LIKE() statements. |
||
246 | * |
||
247 | * @param string $value |
||
248 | * @param bool $ends Ends with? |
||
249 | * @param bool $starts Starts with? |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | public function bLike(string $value, bool $ends = false, bool $starts = false): array |
||
254 | { |
||
255 | |||
256 | if ($starts && !$ends) { |
||
257 | // Starts with. |
||
258 | $value .= '%'; |
||
259 | } elseif (!$starts && $ends) { |
||
260 | // Ends with. |
||
261 | $value = '%' . $value; |
||
262 | } elseif (!$starts && !$ends) { |
||
263 | // Is somewhere... |
||
264 | $value = '%' . $value . '%'; |
||
265 | } |
||
266 | |||
267 | return [$value]; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * !!!DANGER!!! |
||
272 | * Bind a raw value. |
||
273 | * |
||
274 | * @param string|int|float|bool $value |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | public function bRaw($value): array |
||
279 | { |
||
280 | return [$value]; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Bind a string value. |
||
285 | * |
||
286 | * @param string|int|float|bool|null $value |
||
287 | * @param bool $null |
||
288 | * @param int $type |
||
289 | * |
||
290 | * @return array |
||
291 | * @throws Exception |
||
292 | */ |
||
293 | public function bStr($value, bool $null = false, int $type = PDO::PARAM_STR): array |
||
294 | { |
||
295 | //$name = $this->getNextName(); |
||
296 | |||
297 | if ($value === null && $null) { |
||
298 | $type = PDO::PARAM_NULL; |
||
299 | } elseif ($value === null && !$null) { |
||
300 | throw new DomainException('Can not bind NULL in string spot.'); |
||
301 | } |
||
302 | |||
303 | return [(string)$value, $type]; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Convert an array into a string and bind it. |
||
308 | * Great for IN() statements. |
||
309 | * |
||
310 | * @param array $values |
||
311 | * @param string|int|float|bool $default |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | public function bStrArr(array $values, $default = ''): array |
||
321 | } |
||
322 | } |