Completed
Push — master ( b80e24...3b2851 )
by Marcin
04:11
created

SqlQuery::raw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Mnabialek\LaravelSqlLogger\Objects;
4
5
use DateTime;
6
7
class SqlQuery
8
{
9
    /**
10
     * @var int
11
     */
12
    private $number;
13
14
    /**
15
     * @var string
16
     */
17
    private $sql;
18
19
    /**
20
     * @var array
21
     */
22
    private $bindings;
23
24
    /**
25
     * @var int
26
     */
27
    private $time;
28
29
    /**
30
     * SqlQuery constructor.
31
     *
32
     * @param int $number
33
     * @param string $sql
34
     * @param array $bindings
35
     * @param int $time
36
     */
37
    public function __construct($number, $sql, array $bindings, $time)
38
    {
39
        $this->sql = $sql;
40
        $this->bindings = $bindings;
41
        $this->time = $time;
42
        $this->number = $number;
43
    }
44
45
    /**
46
     * Get number of query.
47
     * 
48
     * @return int
49
     */
50
    public function number()
51
    {
52
        return $this->number;
53
    }
54
55
    /**
56
     * Get raw SQL (without bindings).
57
     * 
58
     * @return string
59
     */
60
    public function raw()
61
    {
62
        return $this->sql;
63
    }
64
65
    /**
66
     * Get bindings.
67
     * 
68
     * @return array
69
     */
70
    public function bindings()
71
    {
72
        return $this->bindings;
73
    }
74
75
    /**
76
     * Get time.
77
     * 
78
     * @return int
79
     */
80
    public function time()
81
    {
82
        return $this->time;
83
    }
84
85
    /**
86
     * Get full query with values from bindings inserted.
87
     *
88
     * @return string
89
     */
90
    public function get()
91
    {
92
        return vsprintf(str_replace(['%', '?', "\n"], ['%%', "'%s'", ' '], $this->sql),
93
            $this->formatBindings($this->bindings));
94
    }
95
96
    /**
97
     * Format bindings values.
98
     * 
99
     * @param array $bindings
100
     *
101
     * @return array
102
     */
103
    protected function formatBindings($bindings)
104
    {
105 View Code Duplication
        foreach ($bindings as $key => $binding) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            if ($binding instanceof DateTime) {
107
                $bindings[$key] = $binding->format('Y-m-d H:i:s');
108
            } elseif (is_string($binding)) {
109
                $bindings[$key] = str_replace("'", "\\'", $binding);
110
            }
111
        }
112
113
        return $bindings;
114
    }
115
}
116