Passed
Push — main ( 303b6c...b49db2 )
by Sammy
01:47 queued 15s
created

Insert::statement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace HexMakina\Crudites\Grammar\Query;
4
5
class Insert extends Query
6
{
7
    public function __construct(string $table, array $dat_ass)
8
    {
9
        // Check if the given data is a non-empty array, and throw an exception if it is not
10
        if (empty($dat_ass)) {
11
            throw new \InvalidArgumentException('EMPTY_DATA');
12
        }
13
        
14
        $this->table = $table;
15
        $this->addBindings($dat_ass);
16
    }
17
18
    /**
19
     * Generates the SQL INSERT statement
20
     *
21
     * @return string - The generated SQL INSERT statement
22
     */
23
    public function statement(): string
24
    {
25
        // Generate the INSERT statement with backticks around the field names
26
        $fields = $this->getBindingNames();
27
        $fields = array_keys($fields[$this->table]);
28
        
29
        $fields = '`' . implode('`, `', $fields) . '`';
30
        $bindings = implode(', ', array_keys($this->bindings()));
31
32
        return sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $this->table, $fields, $bindings);
33
    }
34
}
35