Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| Quote | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
10 | |
100.00% |
1 / 1 |
| quoteStructureParts | |
100.00% |
16 / 16 |
|
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 | if ($string === null || strlen($string = trim($string)) === 0) { |
| 36 | return $string; |
| 37 | } |
| 38 | |
| 39 | $parts = explode('.', $string); |
| 40 | $rebuild = []; |
| 41 | foreach ($parts as $p) { |
| 42 | $p = trim($p); |
| 43 | if (!in_array($p, $unquotable) && $p != '') { |
| 44 | // quote at the front |
| 45 | if (strpos($p, $quoteMark) !== 0) { |
| 46 | $p = $quoteMark . $p; |
| 47 | } |
| 48 | // quote at the back: |
| 49 | if (strrpos($p, $quoteMark) != (strlen($p) - 1)) { |
| 50 | $p .= $quoteMark; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (trim($p) != '') { |
| 55 | $rebuild[] = $p; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return implode('.', $rebuild); |
| 60 | } |
| 61 | } |