Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
Quote | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
10 | |
100.00% |
1 / 1 |
quoteStructureParts | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
10 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb\Query\Builder\Dialect; |
4 | |
5 | use BO\Zmsdb\Query\Builder\ExpressionInterface; |
6 | |
7 | /** |
8 | * Quote |
9 | * |
10 | * Abstracts away the quoting mechanism so that you can simply say |
11 | * which quote marks to use. This trait handles things like guarding |
12 | * against double-quoting things. |
13 | * |
14 | * @package BO\Zmsdb\Query\Builder\Dialect |
15 | * @author Alex Gisby<alex@solution10.com> |
16 | * @license MIT |
17 | */ |
18 | trait Quote |
19 | { |
20 | /** |
21 | * Takes a period-separated string and puts the appropriate quote |
22 | * marks around it, guarding against double quoting. |
23 | * |
24 | * @param string|ExpressionInterface $string |
25 | * @param string $quoteMark Mark to use for start and end of quotes |
26 | * @param array $unquotable Any strings that mustn't be quoted (ie *) |
27 | * @return string |
28 | */ |
29 | protected function quoteStructureParts($string, $quoteMark, array $unquotable = []) |
30 | { |
31 | if ($string instanceof ExpressionInterface) { |
32 | return $string; |
33 | } |
34 | |
35 | $string = trim($string); |
36 | if (strlen($string) == 0 || is_null($string)) { |
37 | return $string; |
38 | } |
39 | |
40 | $parts = explode('.', $string); |
41 | $rebuild = []; |
42 | foreach ($parts as $p) { |
43 | $p = trim($p); |
44 | if (!in_array($p, $unquotable) && $p != '') { |
45 | // quote at the front |
46 | if (strpos($p, $quoteMark) !== 0) { |
47 | $p = $quoteMark . $p; |
48 | } |
49 | // quote at the back: |
50 | if (strrpos($p, $quoteMark) != (strlen($p) - 1)) { |
51 | $p .= $quoteMark; |
52 | } |
53 | } |
54 | |
55 | if (trim($p) != '') { |
56 | $rebuild[] = $p; |
57 | } |
58 | } |
59 | |
60 | return implode('.', $rebuild); |
61 | } |
62 | } |