Passed
Push — master ( 826643...4c916a )
by compolom
01:42
created

src/Parts/Insert.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types=1);
2
3
namespace Compolomus\LSQLQueryBuilder\Parts;
4
5
use Compolomus\LSQLQueryBuilder\System\Traits\{
6
    Helper,
7
    Caller,
8
    Placeholders
9
};
10
11
/**
12
 * @method string table()
13
 * @method void addPlaceholders($placeholders)
14
 */
15
class Insert
16
{
17
    use Caller, Placeholders, Helper;
18
19
    protected $fields = [];
20
21
    protected $values = [];
22
23
    public function __construct(array $args = [])
24
    {
25
        if (count($args) > 0) {
26
            if (is_string(key($args))) {
27
                $this->fields(array_keys($args));
28
            }
29
            $this->values(array_values($args));
30
        }
31
    }
32
33
    public function fields(array $fields): Insert
34
    {
35
        $this->fields = $fields;
36
        return $this;
37
    }
38
39
    public function values(array $values): Insert
40
    {
41
        $this->values[] = $this->set($values);
42
        return $this;
43
    }
44
45
    protected function set(array $values): string
46
    {
47
        $result = [];
48 View Code Duplication
        foreach ($values as $value) {
0 ignored issues
show
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...
49
            $key = $this->uid('i');
50
            $result[] = ':' . $key;
51
            $this->placeholders()->set($key, $value);
52
        }
53
        return '(' . implode(',', $result) . ')';
54
    }
55
56
    public function get(): string
57
    {
58
        $this->addPlaceholders($this->placeholders()->get());
59
        return 'INSERT INTO ' . $this->table() . ' '
60
            . (count($this->fields) ? '(' . implode(',', $this->escapeField($this->fields)) . ')' : '')
61
            . ' VALUES '
62
            . (count($this->values)
63
                ? implode(',', $this->values)
64
                : (count($this->fields) && !count($this->values)
65
                    ? '(' . implode(',', array_fill(0, count($this->fields), '?')) . ')'
66
                    : '')
67
            );
68
    }
69
}
70