Completed
Push — master ( 1e376d...b0060a )
by Changwan
05:50
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 1
    public function __construct($table, array $columns = [])
20
    {
21 1
        $this->table = $table;
22 1
        $this->columns = $columns;
23 1
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function toSql()
29
    {
30 1
        $columnSqlParts = [];
31 1
        foreach ($this->columns as $key => $column) {
32 1
            if ($column === '*') {
33
                $columnSqlParts[] = '*';
34
            } else {
35 1
                $columnSqlParts[] = "`{$column}`";
36
            }
37 1
        }
38 1
        $parts = ["SELECT ". implode(', ', $columnSqlParts) ." FROM `{$this->table}`"];
39 1
        if ($part = parent::toSql()) {
40 1
            $parts[] = $part;
41 1
        }
42 1
        return implode(' ', $parts);
43
    }
44
}
45