Completed
Pull Request — master (#2)
by René
04:27 queued 02:19
created

SQLCommand::insertInto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Zortje\MVC\Model;
5
6
use Zortje\MVC\Model\Table\Entity\Exception\InvalidEntityPropertyException;
7
8
/**
9
 * Class SQLCommand
10
 *
11
 * @package Zortje\MVC\Model
12
 */
13
class SQLCommand
14
{
15
16
    /**
17
     * @var String Table name
18
     */
19
    private $tableName;
20
21
    /**
22
     * @var String[] Table columns
23
     */
24
    private $columns;
25
26
    /**
27
     * @param string   $tableName
28
     * @param string[] $columns
29
     */
30 1
    public function __construct(string $tableName, array $columns)
31
    {
32 1
        $this->tableName = $tableName;
33 1
        $this->columns   = $columns;
34 1
    }
35
36
    /**
37
     * Get table name
38
     *
39
     * @return String Table name
40
     */
41 1
    public function getTableName(): string
42
    {
43 1
        return $this->tableName;
44
    }
45
46
    /**
47
     * Get table columns
48
     *
49
     * @return \String[] Columns
50
     */
51 1
    public function getColumns(): array
52
    {
53 1
        return $this->columns;
54
    }
55
56
    /**
57
     * Create INSERT INTO command
58
     *
59
     * @return string INSERT INTO query
60
     */
61 1
    public function insertInto(): string
62
    {
63 1
        $columnNames  = $this->getColumnNames($this->columns);
64 1
        $columnValues = $this->getColumnValues($this->columns);
65
66 1
        return "INSERT INTO `$this->tableName` ($columnNames) VALUES ($columnValues);";
67
    }
68
69
    /**
70
     * Create UPDATE SET command with WHERE for updating a single row with ID
71
     *
72
     * @param array $columns Columns to use in SET condition
73
     *
74
     * @return string
75
     */
76 1
    public function updateSetWhere(array $columns): string
77
    {
78 1
        $set   = $this->getEqualFromColumns(', ', $columns);
79 1
        $where = $this->getEqualFromColumns(' AND ', ['id']);
80
81 1
        return "UPDATE `$this->tableName` SET $set WHERE $where;";
82
    }
83
84
    /**
85
     * Create SELECT FROM command
86
     *
87
     * @return string SELECT FROM query
88
     */
89 1
    public function selectFrom(): string
90
    {
91 1
        $tableColumnNames = $this->getColumnNames($this->columns);
92
93 1
        return "SELECT $tableColumnNames FROM `$this->tableName`;";
94
    }
95
96
    /**
97
     * Create SELECT FROM command with WHERE
98
     *
99
     * @param string[] $columns Columns to use in WHERE condition
100
     *
101
     * @return string SELECT FROM query
102
     */
103 1
    public function selectFromWhere($columns): string
104
    {
105 1
        $tableColumnNames = $this->getColumnNames($this->columns);
106
107 1
        $where = $this->getEqualFromColumns(' AND ', $columns);
108
109 1
        return "SELECT $tableColumnNames FROM `$this->tableName` WHERE $where;";
110
    }
111
112
    /**
113
     * Get columns names for SQL command
114
     *
115
     * @param String[] $columns
116
     *
117
     * @return string Column names for column list
118
     */
119 1
    protected function getColumnNames(array $columns): string
120
    {
121 1
        $tableColumnNames = implode('`, `', array_keys($columns));
122
123 1
        return "`{$tableColumnNames}`";
124
    }
125
126
    /**
127
     * Get columns values for SQL command
128
     *
129
     * @param String[] $columns
130
     *
131
     * @return string Column names for column values
132
     */
133 1
    protected function getColumnValues(array $columns): string
134
    {
135 1
        $tableColumnValues = implode(', :', array_keys($columns));
136
137 1
        return ":{$tableColumnValues}";
138
    }
139
140
    /**
141
     * Get equal string for columns with glue
142
     *
143
     * @param string $glue    String glue between columns
144
     * @param array  $columns Columns to use
145
     *
146
     * @return string
147
     *
148
     * @throws InvalidEntityPropertyException If provided with columns which the entity dosnt have
149
     */
150 2
    protected function getEqualFromColumns(string $glue, array $columns): string
151
    {
152 2
        $equal = [];
153
154 2
        foreach ($columns as $column) {
155 2
            if (!isset($this->columns[$column])) {
156 1
                throw new InvalidEntityPropertyException([$this->tableName, $column]);
157
            }
158
159 1
            $equal[] = "`$column` = :$column";
160
        }
161
162 1
        return implode($glue, $equal);
163
    }
164
}
165