Passed
Branch 4.1.0 (271fee)
by Joao
03:47
created

Updatable::where()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace ByJG\MicroOrm;
4
5
use ByJG\AnyDataset\Db\DbFunctionsInterface;
6
use ByJG\MicroOrm\Exception\InvalidArgumentException;
7
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;
8
9
class Updatable
10
{
11
    protected $fields = [];
12
    protected $table = "";
13
    protected $where = [];
14
15
    public static function getInstance()
16
    {
17
        return new Updatable();
18
    }
19
20
    /**
21
     * Example:
22
     *   $query->fields(['name', 'price']);
23
     *
24
     * @param array $fields
25
     * @return $this
26
     */
27
    public function fields(array $fields)
28
    {
29
        $this->fields = array_merge($this->fields, (array)$fields);
30
        
31
        return $this;
32
    }
33
34
    /**
35
     * Example
36
     *    $query->table('product');
37
     *
38
     * @param string $table
39
     * @return $this
40
     */
41
    public function table($table)
42
    {
43
        $this->table = $table;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Example:
50
     *    $query->filter('price > [[amount]]', [ 'amount' => 1000] );
51
     *
52
     * @param string $filter
53
     * @param array $params
54
     * @return $this
55
     */
56
    public function where($filter, array $params = [])
57
    {
58
        $this->where[] = [ 'filter' => $filter, 'params' => $params  ];
59
        return $this;
60
    }
61
62
    protected function getFields()
63
    {
64
        if (empty($this->fields)) {
65
            return ' * ';
66
        }
67
68
        return ' ' . implode(', ', $this->fields) . ' ';
69
    }
70
    
71
    protected function getWhere()
72
    {
73
        $whereStr = [];
74
        $params = [];
75
76
        foreach ($this->where as $item) {
77
            $whereStr[] = $item['filter'];
78
            $params = array_merge($params, $item['params']);
79
        }
80
        
81
        if (empty($whereStr)) {
82
            return null;
83
        }
84
        
85
        return [ implode(' AND ', $whereStr), $params ];
86
    }
87
88
89
    /**
90
     * @param $params
91
     * @param \ByJG\AnyDataset\Db\DbFunctionsInterface|null $dbHelper
92
     * @return null|string|string[]
93
     * @throws \ByJG\MicroOrm\Exception\OrmInvalidFieldsException
94
     */
95
    public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null)
96
    {
97
        if (empty($this->fields)) {
98
            throw new OrmInvalidFieldsException('You must specifiy the fields for insert');
99
        }
100
101
        $fieldsStr = $this->fields;
102
        if (!is_null($dbHelper)) {
103
            $fieldsStr = $dbHelper->delimiterField($fieldsStr);
104
        }
105
106
        $tableStr = $this->table;
107
        if (!is_null($dbHelper)) {
108
            $tableStr = $dbHelper->delimiterTable($tableStr);
109
        }
110
111
        $sql = 'INSERT INTO '
112
            . $tableStr
113
            . '( ' . implode(', ', $fieldsStr) . ' ) '
114
            . ' values '
115
            . '( [[' . implode(']], [[', $this->fields) . ']] ) ';
116
117
        return ORMHelper::processLiteral($sql, $params);
118
    }
119
120
    /**
121
     * @param $params
122
     * @param \ByJG\AnyDataset\Db\DbFunctionsInterface|null $dbHelper
123
     * @return array
124
     * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
125
     */
126
    public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
127
    {
128
        if (empty($this->fields)) {
129
            throw new InvalidArgumentException('You must specifiy the fields for insert');
130
        }
131
        
132
        $fieldsStr = [];
133
        foreach ($this->fields as $field) {
134
            $fieldName = $field;
135
            if (!is_null($dbHelper)) {
136
                $fieldName = $dbHelper->delimiterField($fieldName);
137
            }
138
            $fieldsStr[] = "$fieldName = [[$field]] ";
139
        }
140
        
141
        $whereStr = $this->getWhere();
142
        if (is_null($whereStr)) {
143
            throw new InvalidArgumentException('You must specifiy a where clause');
144
        }
145
146
        $tableName = $this->table;
147
        if (!is_null($dbHelper)) {
148
            $tableName = $dbHelper->delimiterTable($tableName);
149
        }
150
151
        $sql = 'UPDATE ' . $tableName . ' SET '
152
            . implode(', ', $fieldsStr)
153
            . ' WHERE ' . $whereStr[0];
154
155
        $params = array_merge($params, $whereStr[1]);
156
157
        return ORMHelper::processLiteral($sql, $params);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ByJG\MicroOrm\ORM...sLiteral($sql, $params) returns the type string which is incompatible with the documented return type array.
Loading history...
158
    }
159
160
    /**
161
     * @param $params
162
     * @return array
163
     * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
164
     */
165
    public function buildDelete(&$params)
166
    {
167
        $whereStr = $this->getWhere();
168
        if (is_null($whereStr)) {
169
            throw new InvalidArgumentException('You must specifiy a where clause');
170
        }
171
172
        $sql = 'DELETE FROM ' . $this->table
173
            . ' WHERE ' . $whereStr[0];
174
175
        $params = array_merge($params, $whereStr[1]);
176
177
        return ORMHelper::processLiteral($sql, $params);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ByJG\MicroOrm\ORM...sLiteral($sql, $params) returns the type string which is incompatible with the documented return type array.
Loading history...
178
    }
179
}
180