Completed
Push — master ( ee6d65...c2027d )
by Woody
03:25
created

InsertQuery::paramLister()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Latitude\QueryBuilder;
5
6
use InvalidArgumentException;
7
use Iterator;
8
9
class InsertQuery implements Statement
10
{
11
    use Traits\CanConvertIteratorToString;
12
    use Traits\CanUseDefaultIdentifier;
13
14
    /**
15
     * Create a new insert query.
16
     */
17 14
    public static function make(string $table, array $map = []): InsertQuery
18
    {
19 14
        $query = new static();
20 14
        $query->table($table);
21 14
        if ($map) {
0 ignored issues
show
Bug Best Practice introduced by Woody Gilk
The expression $map of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
22 7
            $query->map($map);
23
        }
24 14
        return $query;
25
    }
26
27
    /**
28
     * Set the table to insert into.
29
     */
30 14
    public function table(string $table): self
31
    {
32 14
        $this->table = $table;
33 14
        return $this;
34
    }
35
36
    /**
37
     * Set the columns to insert.
38
     */
39 9
    public function columns(string ...$columns): self
40
    {
41 9
        $this->columns = $columns;
42 9
        return $this;
43
    }
44
45
    /**
46
     * Append values to insert.
47
     */
48 9
    public function values(...$values): self
49
    {
50 9
        if (\count($values) !== \count($this->columns)) {
51 1
            throw new InvalidArgumentException(sprintf(
52 1
                'Number of values (%d) does not match number of columns (%d)',
53 1
                \count($values),
54 1
                \count($this->columns)
55
            ));
56
        }
57 8
        $this->values[] = ValueList::make(...$values);
58 8
        return $this;
59
    }
60
61
    /**
62
     * Set the columns and values to insert.
63
     *
64
     * NOTE: Existing values will be replaced!
65
     */
66 7
    public function map(array $map): self
67
    {
68 7
        $this->values = [];
69 7
        $this->columns(...\array_keys($map));
70 7
        $this->values(...\array_values($map));
71 7
        return $this;
72
    }
73
74
    // Statement
75 8
    public function sql(Identifier $identifier = null): string
76
    {
77 8
        $identifier = $this->getDefaultIdentifier($identifier);
78
79 8
        return \sprintf(
80 8
            'INSERT INTO %s (%s) VALUES %s',
81 8
            $identifier->escape($this->table),
82 8
            \implode(', ', $identifier->all($this->columns)),
83 8
            $this->stringifyIterator($this->insertLines())
84
        );
85
    }
86
87
    // Statement
88 8
    public function params(): array
89
    {
90
        // [[a], [b], [c]] -> [a, b, c]
0 ignored issues
show
Unused Code Comprehensibility introduced by Woody Gilk
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91 8
        return \array_merge(...\array_map($this->paramLister(), $this->values));
92
    }
93
94
    /**
95
     * @var string
96
     */
97
    protected $table;
98
99
    /**
100
     * @var array
101
     */
102
    protected $columns = [];
103
104
    /**
105
     * @var ValueList[]
106
     */
107
    protected $values = [];
108
109
    /**
110
     * Generate a list of insert lines.
111
     */
112 8
    protected function insertLines(): Iterator
113
    {
114 8
        foreach ($this->values as $line) {
115 8
            yield "({$line->sql()})";
116
        }
117 8
    }
118
119
    /**
120
     * Convert all parameters to an array for flattening.
121
     */
122
    protected function paramLister(): callable
123
    {
124 8
        return function (ValueList $values): array {
125 8
            return $values->params();
126 8
        };
127
    }
128
}
129