| Total Complexity | 11 |
| Total Lines | 78 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | trait ReplacesBindings |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Replace bindings. |
||
| 11 | * |
||
| 12 | * @param string $sql |
||
| 13 | * @param array $bindings |
||
| 14 | * |
||
| 15 | * @return string |
||
| 16 | */ |
||
| 17 | protected function replaceBindings($sql, array $bindings) |
||
| 18 | { |
||
| 19 | $regex = $this->getRegex(); |
||
| 20 | |||
| 21 | foreach ($this->formatBindings($bindings) as $key => $binding) { |
||
| 22 | $value = is_numeric($binding) ? $binding : "'" . $binding . "'"; |
||
| 23 | $sql = preg_replace($regex, $value, $sql, 1); |
||
| 24 | } |
||
| 25 | |||
| 26 | return $sql; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Format bindings values. |
||
| 31 | * |
||
| 32 | * @param array $bindings |
||
| 33 | * |
||
| 34 | * @return array |
||
| 35 | */ |
||
| 36 | protected function formatBindings($bindings) |
||
| 37 | { |
||
| 38 | foreach ($bindings as $key => $binding) { |
||
| 39 | if ($binding instanceof DateTime) { |
||
| 40 | $bindings[$key] = $binding->format('Y-m-d H:i:s'); |
||
| 41 | } elseif (is_string($binding)) { |
||
| 42 | $bindings[$key] = str_replace("'", "\\'", $binding); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | return $bindings; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get regex to be used to replace bindings. |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | protected function getRegex() |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Wrap regex. |
||
| 60 | * |
||
| 61 | * @param string $regex |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | protected function wrapRegex($regex) |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Create partial regex to find given text not inside quotes. |
||
| 72 | * |
||
| 73 | * @param string $string |
||
| 74 | * @param bool $quote |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | protected function notInsideQuotes($string, $quote = true) |
||
| 87 |