Completed
Push — master ( 28b954...ada6cf )
by Marcin
06:16
created

ReplacesBindings::getNamedParameterRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Mnabialek\LaravelSqlLogger\Objects\Concerns;
4
5
use DateTime;
6
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
        $generalRegex = $this->getRegex();
20
21
        foreach ($this->formatBindings($bindings) as $key => $binding) {
22
            $value = is_numeric($binding) ? $binding : "'" . $binding . "'";
23
            $regex = is_numeric($key) ? $generalRegex : $this->getNamedParameterRegex($key);
24
            $sql = preg_replace($regex, $value, $sql, 1);
25
        }
26
27
        return $sql;
28
    }
29
30
    /**
31
     * Get regex to be used for named parameter with given name.
32
     *
33
     * @param string $name
34
     *
35
     * @return string
36
     */
37
    protected function getNamedParameterRegex($name)
38
    {
39
        if (mb_substr($name, 0, 1) == ':') {
40
            $name = mb_substr($name, 1);
41
        }
42
43
        return $this->wrapRegex($this->notInsideQuotes('\:' . preg_quote($name), false));
44
    }
45
46
    /**
47
     * Format bindings values.
48
     *
49
     * @param array $bindings
50
     *
51
     * @return array
52
     */
53
    protected function formatBindings($bindings)
54
    {
55
        foreach ($bindings as $key => $binding) {
56
            if ($binding instanceof DateTime) {
57
                $bindings[$key] = $binding->format('Y-m-d H:i:s');
58
            } elseif (is_string($binding)) {
59
                $bindings[$key] = str_replace("'", "\\'", $binding);
60
            }
61
        }
62
63
        return $bindings;
64
    }
65
66
    /**
67
     * Get regex to be used to replace bindings.
68
     *
69
     * @return string
70
     */
71
    protected function getRegex()
72
    {
73
        return $this->wrapRegex(
74
            $this->notInsideQuotes('?')
75
            . '|' .
76
            $this->notInsideQuotes('\:\w+', false)
77
        );
78
    }
79
80
    /**
81
     * Wrap regex.
82
     *
83
     * @param string $regex
84
     *
85
     * @return string
86
     */
87
    protected function wrapRegex($regex)
88
    {
89
        return '#' . $regex . '#ms';
90
    }
91
92
    /**
93
     * Create partial regex to find given text not inside quotes.
94
     *
95
     * @param string $string
96
     * @param bool $quote
97
     *
98
     * @return string
99
     */
100
    protected function notInsideQuotes($string, $quote = true)
101
    {
102
        if ($quote) {
103
            $string = preg_quote($string);
104
        }
105
106
        return
107
            // double quotes - ignore "" and everything inside quotes for example " abc \"err "
108
            '(?:""|"(?:[^"]|\\")*?[^\\\]")(*SKIP)(*F)|' . $string .
109
            '|' .
110
            // single quotes - ignore '' and everything inside quotes for example ' abc \'err '
111
            '(?:\\\'\\\'|\'(?:[^\']|\\\')*?[^\\\]\')(*SKIP)(*F)|' . $string;
112
    }
113
}
114