Completed
Push — master ( 3deaa5...4c5cb4 )
by Changwan
06:31
created

SelectQuery::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database\Query;
3
4
use Wandu\Database\Contracts\QueryInterface;
5
use Wandu\Database\Query\Expression\HasWhereExpression;
6
7
class SelectQuery extends HasWhereExpression implements QueryInterface
8
{
9
    /** @var string */
10
    protected $table;
11
    
12
    /** @var array */
13
    protected $columns = ['*'];
14
    
15
    /**
16
     * @param string $table
17
     * @param array $columns
18
     */
19 6
    public function __construct($table, array $columns = [])
20
    {
21 6
        $this->table = $table;
22 6
        $this->columns = $columns;
23 6
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 6
    public function toSql()
29
    {
30 6
        $columnSqlParts = [];
31 6
        foreach ($this->columns as $key => $column) {
32 6
            if ($column === '*') {
33 5
                $columnSqlParts[] = '*';
34 5
            } else {
35 1
                $columnSqlParts[] = "`{$column}`";
36
            }
37 6
        }
38 6
        $parts = ["SELECT ". implode(', ', $columnSqlParts) ." FROM `{$this->table}`"];
39 6
        if ($part = parent::toSql()) {
40 6
            $parts[] = $part;
41 6
        }
42 6
        return implode(' ', $parts);
43
    }
44
}
45