Passed
Push — master ( a4f7f2...d3cc39 )
by Melech
04:09
created

SqlInsertQueryBuilder::withSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Orm\QueryBuilder;
15
16
use Valkyrja\Orm\Constant\Statement;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Orm\Constant\Statement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Orm\Data\Value;
18
use Valkyrja\Orm\QueryBuilder\Contract\InsertQueryBuilder as Contract;
19
20
/**
21
 * Class SqlInsertQueryBuilder.
22
 *
23
 * @author Melech Mizrachi
24
 */
25
class SqlInsertQueryBuilder extends SqlQueryBuilder implements Contract
26
{
27
    /** @var Value[] */
28
    protected array $values = [];
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function withSet(Value ...$values): static
34
    {
35
        $new = clone $this;
36
37
        $new->values = $values;
38
39
        return $new;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function withAddedSet(Value ...$values): static
46
    {
47
        $new = clone $this;
48
49
        $new->values = array_merge($new->values, $values);
50
51
        return $new;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function __toString(): string
58
    {
59
        $query = Statement::INSERT
60
            . ' ' . Statement::INTO
61
            . " $this->from"
62
            . $this->getAliasQuery();
63
64
        $columns = [];
65
        $values  = [];
66
67
        foreach ($this->values as $value) {
68
            $columns[] = $value->name;
69
            $values[]  = (string) $value;
70
        }
71
72
        $columns = implode(', ', $columns);
73
        $values  = implode(', ', $values);
74
75
        return $query
76
            . " ($columns)"
77
            . ' ' . Statement::VALUES
78
            . " ($values)"
79
            . $this->getWhereQuery()
80
            . $this->getJoinQuery();
81
    }
82
}
83