Passed
Push — master ( 1c0e8d...8b3112 )
by Agel_Nash
02:39
created

SupportTrait::convertValue()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 3
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php namespace AgelxNash\Modx\Evo\Database\Traits;
2
3
use AgelxNash\Modx\Evo\Database\Exceptions;
4
5
trait SupportTrait
6
{
7
    /**
8
     * @param $value
9
     * @return mixed
10
     */
11 13
    protected function convertValue($value)
12
    {
13
        switch (true) {
14 13
            case (\is_numeric($value) && ! \is_float(1 * $value)):
15 7
                $value = (int)$value;
16 7
                break;
17 11
            case \is_numeric($value) && \is_float(1*$value):
18 1
                $value = (float)$value;
19 1
                break;
20
            default:
21 11
                break;
22
        }
23
24 13
        return $value;
25
    }
26
27
    /**
28
     * @param int $timestamp
29
     * @param string $fieldType
30
     * @return bool|false|string
31
     * @deprecated
32
     */
33 1
    public function convertDate($timestamp, $fieldType = 'DATETIME')
34
    {
35 1
        $date = false;
36 1
        if (! empty($timestamp) && $timestamp > 0) {
37
            switch ($fieldType) {
38 1
                case 'DATE':
39 1
                    $date = date('Y-m-d', $timestamp);
40 1
                    break;
41 1
                case 'TIME':
42 1
                    $date = date('H:i:s', $timestamp);
43 1
                    break;
44 1
                case 'YEAR':
45 1
                    $date = date('Y', $timestamp);
46 1
                    break;
47 1
                case 'DATETIME':
48
                default:
49 1
                    $date = date('Y-m-d H:i:s', $timestamp);
50 1
                    break;
51
            }
52
        }
53
54 1
        return $date;
55
    }
56
57
    /**
58
     * @param string|array $data
59
     * @param bool $ignoreAlias
60
     * @return string
61
     */
62 9
    protected function prepareFields($data, $ignoreAlias = false)
63
    {
64 9
        if (\is_array($data)) {
65 5
            $tmp = [];
66 5
            foreach ($data as $alias => $field) {
67 4
                $tmp[] = ($alias !== $field && ! \is_int($alias) && $ignoreAlias === false) ?
68 4
                    ($field . ' as `' . $alias . '`') : $field;
69
            }
70
71 5
            $data = implode(',', $tmp);
72
        }
73 9
        if (empty($data)) {
74 1
            $data = '*';
75
        }
76
77 9
        return $data;
78
    }
79
80
    /**
81
     * @param string|null $value
82
     * @return string
83
     * @throws Exceptions\InvalidFieldException
84
     */
85 11
    protected function prepareNull($value)
86
    {
87
        switch (true) {
88 11
            case ($value === null || (\is_scalar($value) && strtolower($value) === 'null')):
89 3
                $value = 'NULL';
90 3
                break;
91 11
            case \is_scalar($value):
92 11
                $value = "'" . $value . "'";
93 11
                break;
94
            default:
95 1
                throw (new Exceptions\InvalidFieldException('NULL'))
96 1
                    ->setData($value);
97
        }
98
99 11
        return $value;
100
    }
101
102
    /**
103
     * @param string|array $data
104
     * @param int $level
105
     * @param bool $skipFieldNames
106
     * @return array|string
107
     * @throws Exceptions\InvalidFieldException
108
     * @throws Exceptions\TooManyLoopsException
109
     */
110 7
    protected function prepareValues($data, $level = 1, $skipFieldNames = false)
111
    {
112 7
        $fields = [];
113 7
        $values = [];
114 7
        $maxLevel = $level;
115 7
        $wrap = false;
116
117 7
        if (\is_array($data)) {
118 7
            foreach ($data as $key => $value) {
119 7
                if (\is_array($value)) {
120 1
                    if ($level > 2) {
121
                        throw new Exceptions\TooManyLoopsException('Values');
122
                    }
123 1
                    $maxLevel++;
124 1
                    $out = $this->prepareValues($value, $level + 1);
125 1
                    if (empty($fields)) {
126 1
                        $fields = $out['fields'];
127 1
                    } elseif ($fields !== $out['fields'] && $skipFieldNames === false) {
128
                        throw (new Exceptions\InvalidFieldException("Don't match field names"))
129
                            ->setData($data);
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type string expected by parameter $data of AgelxNash\Modx\Evo\Datab...eldException::setData(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
                            ->setData(/** @scrutinizer ignore-type */ $data);
Loading history...
130
                    }
131 1
                    $wrap = true;
132 1
                    $values[] = $out['values'];
133
                } else {
134 7
                    $fields[] = $key;
135 7
                    $values[] = $this->prepareNull($value);
136
                }
137
            }
138 7
            if (\is_array($values)) {
139 7
                $values = implode(', ', $values);
140
            }
141 7
            if ($wrap === false) {
142 7
                $values = '(' . $values . ')';
143
            }
144
        }
145
146 7
        if (! \is_scalar($values)) {
147
            throw (new Exceptions\InvalidFieldException('values'))
148
                ->setData($values);
149
        }
150
151 7
        if (($fields = $this->checkFields($fields, $maxLevel, $skipFieldNames)) === false) {
152
            throw (new Exceptions\InvalidFieldException('fields name'))
153
                ->setData($data);
154
        }
155
156 7
        if ($level === 2) {
157 1
            return compact('fields', 'values');
158
        }
159
160 7
        return (empty($fields) ? '' : $fields . ' VALUES ') . $values;
161
    }
162
163
    /**
164
     * @param mixed $fields
165
     * @param int $level
166
     * @param bool $skipFieldNames
167
     * @return bool|string
168
     */
169 7
    protected function checkFields($fields, $level = 1, $skipFieldNames = false)
170
    {
171 7
        if (\is_array($fields) && $skipFieldNames === false) {
172 7
            if ($this->arrayOnlyNumeric($fields) === true) {
173 1
                $fields = ($level === 2) ? false : '';
174
            } else {
175 7
                $fields = '(`' . implode('`, `', $fields) . '`)';
176
            }
177
        }
178
179 7
        return $fields;
180
    }
181
182
    /**
183
     * @param array $data
184
     * @return bool
185
     */
186 10
    protected function arrayOnlyNumeric(array $data)
187
    {
188 10
        $onlyNumbers = true;
189 10
        foreach ($data as $value) {
190 10
            if (! \is_numeric($value)) {
191 8
                $onlyNumbers = false;
192 10
                break;
193
            }
194
        }
195
196 10
        return $onlyNumbers;
197
    }
198
199
    /**
200
     * @param string|array $data
201
     * @return string
202
     * @throws Exceptions\InvalidFieldException
203
     */
204 5
    protected function prepareValuesSet($data)
205
    {
206 5
        if (\is_array($data)) {
207 5
            foreach ($data as $key => $value) {
208 5
                $data[$key] = $this->prepareNull($value);
209
            }
210
211 5
            foreach ($data as $key => $value) {
212 5
                $data[$key] = "`{$key}` = " . $value;
213
            }
214 5
            $data = implode(', ', $data);
215
        }
216
217 5
        return trim($data);
218
    }
219
220
    /**
221
     * @param string|array $data
222
     * @param bool $hasArray
223
     * @return string
224
     * @throws Exceptions\TableNotDefinedException
225
     */
226 16
    protected function prepareFrom($data, $hasArray = false)
227
    {
228 16
        if (\is_array($data) && $hasArray === true) {
229 3
            $tmp = [];
230 3
            foreach ($data as $table) {
231 3
                $tmp[] = $table;
232
            }
233 3
            $data = implode(' ', $tmp);
234
        }
235 16
        if (! is_scalar($data) || empty($data)) {
236 1
            throw new Exceptions\TableNotDefinedException($data);
237
        }
238
239 16
        return $data;
240
    }
241
242
    /**
243
     * @param array|string $data
244
     * @return string
245
     * @throws Exceptions\InvalidFieldException
246
     */
247 13
    protected function prepareWhere($data)
248
    {
249 13
        if (\is_array($data)) {
250 3
            if ($this->arrayOnlyNumeric(array_keys($data)) === true) {
251 3
                $data = implode(' ', $data);
252
            } else {
253 1
                throw (new Exceptions\InvalidFieldException('WHERE'))
254 1
                    ->setData($data);
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type string expected by parameter $data of AgelxNash\Modx\Evo\Datab...eldException::setData(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

254
                    ->setData(/** @scrutinizer ignore-type */ $data);
Loading history...
255
            }
256
        }
257 13
        $data = trim($data);
258 13
        if (! empty($data) && stripos($data, 'WHERE') !== 0) {
259 11
            $data = "WHERE {$data}";
260
        }
261
262 13
        return $data;
263
    }
264
265
    /**
266
     * @param string $data
267
     * @return string
268
     */
269 9
    protected function prepareOrder($data)
270
    {
271 9
        $data = trim($data);
272 9
        if (! empty($data) && stripos($data, 'ORDER') !== 0) {
273 5
            $data = "ORDER BY {$data}";
274
        }
275
276 9
        return $data;
277
    }
278
279
    /**
280
     * @param string $data
281
     * @return string
282
     */
283 11
    protected function prepareLimit($data)
284
    {
285 11
        $data = trim($data);
286 11
        if (! empty($data) && stripos($data, 'LIMIT') !== 0) {
287 5
            $data = "LIMIT {$data}";
288
        }
289
290 11
        return $data;
291
    }
292
}
293