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

SqlQueryBuilder::getQueryString()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 24
rs 9.4555
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\Join;
18
use Valkyrja\Orm\Data\Where;
19
use Valkyrja\Orm\Data\WhereGroup;
20
use Valkyrja\Orm\QueryBuilder\Contract\QueryBuilder as Contract;
21
22
/**
23
 * Class SqlQueryBuilder.
24
 *
25
 * @author Melech Mizrachi
26
 */
27
abstract class SqlQueryBuilder implements Contract
28
{
29
    /** @var string */
30
    protected string $alias = '';
31
    /** @var Join[] */
32
    protected array $joins = [];
33
    /** @var array<Where|WhereGroup> */
34
    protected array $where = [];
35
36
    /**
37
     * @param non-empty-string $from The table
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
38
     */
39
    public function __construct(
40
        protected string $from,
41
    ) {
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function withFrom(string $table): static
48
    {
49
        $new = clone $this;
50
51
        $new->from = $table;
52
53
        return $new;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function withAlias(string $alias): static
60
    {
61
        $new = clone $this;
62
63
        $new->alias = $alias;
64
65
        return $new;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function withJoin(Join ...$joins): static
72
    {
73
        $new = clone $this;
74
75
        $new->joins = $joins;
76
77
        return $new;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function withAddedJoin(Join ...$joins): static
84
    {
85
        $new = clone $this;
86
87
        $new->joins = array_merge($new->joins, $joins);
88
89
        return $new;
90
    }
91
92
    public function withWhere(Where|WhereGroup ...$where): static
93
    {
94
        $new = clone $this;
95
96
        $new->where = $where;
97
98
        return $new;
99
    }
100
101
    public function withAddedWhere(Where|WhereGroup ...$where): static
102
    {
103
        $new = clone $this;
104
105
        $new->where = array_merge($new->where, $where);
106
107
        return $new;
108
    }
109
110
    /**
111
     * Get the alias of a query statement.
112
     *
113
     * @return string
114
     */
115
    protected function getAliasQuery(): string
116
    {
117
        return $this->alias === ''
118
            ? ''
119
            : " $this->alias";
120
    }
121
122
    /**
123
     * Get the where of a query statement.
124
     *
125
     * @return string
126
     */
127
    protected function getWhereQuery(): string
128
    {
129
        return empty($this->where)
130
            ? ''
131
            : ' ' . Statement::WHERE . ' ' . implode(' ', $this->where);
132
    }
133
134
    /**
135
     * Get the joins of a query statement.
136
     *
137
     * @return string
138
     */
139
    protected function getJoinQuery(): string
140
    {
141
        return empty($this->joins)
142
            ? ''
143
            : ' ' . implode(' ', $this->joins);
144
    }
145
}
146