Passed
Push — master ( 1638bc...46c53c )
by Agel_Nash
02:33
created

SupportTrait::withCommaSeparator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

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 2
eloc 3
nc 2
nop 1
crap 2
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
            $format = [
38 1
                'DATE' => 'Y-m-d',
39
                'TIME' => 'H:i:s',
40
                'YEAR' => 'Y',
41
                'DATETIME' => 'Y-m-d H:i:s',
42
            ];
43 1
            $use = isset($format[$fieldType]) ? $format[$fieldType] : 'Y-m-d H:i:s';
44 1
            $date = date($use, $timestamp);
45
        }
46
47 1
        return $date;
48
    }
49
50
    /**
51
     * @param string|array $data
52
     * @param bool $ignoreAlias
53
     * @return string
54
     */
55 9
    protected function prepareFields($data, $ignoreAlias = false)
56
    {
57 9
        if (\is_array($data)) {
58 5
            $tmp = [];
59 5
            foreach ($data as $alias => $field) {
60 4
                $tmp[] = ($alias !== $field && ! \is_int($alias) && $ignoreAlias === false) ?
61 4
                    ($field . ' as `' . $alias . '`') : $field;
62
            }
63
64 5
            $data = implode(',', $tmp);
65
        }
66 9
        if (empty($data)) {
67 1
            $data = '*';
68
        }
69
70 9
        return $data;
71
    }
72
73
    /**
74
     * @param string|null $value
75
     * @return string
76
     * @throws Exceptions\InvalidFieldException
77
     */
78 11
    protected function prepareNull($value)
79
    {
80
        switch (true) {
81 11
            case ($value === null || (\is_scalar($value) && strtolower($value) === 'null')):
82 3
                $value = 'NULL';
83 3
                break;
84 11
            case \is_scalar($value):
85 11
                $value = "'" . $value . "'";
86 11
                break;
87
            default:
88 1
                throw (new Exceptions\InvalidFieldException('NULL'))
89 1
                    ->setData($value);
90
        }
91
92 11
        return $value;
93
    }
94
95
    /**
96
     * @param string|array $data
97
     * @param int $level
98
     * @param bool $skipFieldNames
99
     * @return array|string
100
     * @throws Exceptions\InvalidFieldException
101
     * @throws Exceptions\TooManyLoopsException
102
     */
103 7
    protected function prepareValues($data, $level = 1, $skipFieldNames = false)
104
    {
105 7
        $fields = [];
106 7
        $values = [];
107 7
        $maxLevel = $level;
108 7
        $wrap = false;
109
110 7
        if (\is_array($data)) {
111 7
            foreach ($data as $key => $value) {
112 7
                if (\is_array($value)) {
113 1
                    if ($level > 2) {
114
                        throw new Exceptions\TooManyLoopsException('Values');
115
                    }
116 1
                    $maxLevel++;
117 1
                    $out = $this->prepareValues($value, $level + 1);
118 1
                    if (empty($fields)) {
119 1
                        $fields = $out['fields'];
120 1
                    } elseif ($fields !== $out['fields'] && $skipFieldNames === false) {
121
                        throw (new Exceptions\InvalidFieldException("Don't match field names"))
122
                            ->setData($data);
123
                    }
124 1
                    $wrap = true;
125 1
                    $values[] = $out['values'];
126
                } else {
127 7
                    $fields[] = $key;
128 7
                    $values[] = $this->prepareNull($value);
129
                }
130
            }
131 7
            $values = $this->withCommaSeparator($values);
132 7
            if ($wrap === false) {
133 7
                $values = '(' . $values . ')';
134
            }
135
        }
136
137 7
        if (! \is_scalar($values)) {
138
            throw (new Exceptions\InvalidFieldException('values'))
139
                ->setData($values);
140
        }
141
142 7
        if (($fields = $this->checkFields($fields, $maxLevel, $skipFieldNames)) === false) {
143
            throw (new Exceptions\InvalidFieldException('fields name'))
144
                ->setData($data);
145
        }
146
147 7
        if ($level === 2) {
148 1
            return compact('fields', 'values');
149
        }
150
151 7
        return (empty($fields) ? '' : $fields . ' VALUES ') . $values;
152
    }
153
154
    /**
155
     * @param mixed $fields
156
     * @param int $level
157
     * @param bool $skipFieldNames
158
     * @return bool|string
159
     */
160 7
    protected function checkFields($fields, $level = 1, $skipFieldNames = false)
161
    {
162 7
        if (\is_array($fields) && $skipFieldNames === false) {
163 7
            if ($this->arrayOnlyNumeric($fields) === true) {
164 1
                $fields = ($level === 2) ? false : '';
165
            } else {
166 7
                $fields = '(`' . implode('`, `', $fields) . '`)';
167
            }
168
        }
169
170 7
        return $fields;
171
    }
172
173
    /**
174
     * @param array $data
175
     * @return bool
176
     */
177 10
    protected function arrayOnlyNumeric(array $data)
178
    {
179 10
        $onlyNumbers = true;
180 10
        foreach ($data as $value) {
181 10
            if (! \is_numeric($value)) {
182 8
                $onlyNumbers = false;
183 10
                break;
184
            }
185
        }
186
187 10
        return $onlyNumbers;
188
    }
189
190
    /**
191
     * @param string|array $data
192
     * @return string
193
     * @throws Exceptions\InvalidFieldException
194
     */
195 5
    protected function prepareValuesSet($data)
196
    {
197 5
        if (\is_array($data)) {
198 5
            foreach ($data as $key => $value) {
199 5
                $data[$key] = $this->prepareNull($value);
200
            }
201
202 5
            foreach ($data as $key => $value) {
203 5
                $data[$key] = "`{$key}` = " . $value;
204
            }
205
        }
206
207 5
        return $this->withCommaSeparator($data);
208
    }
209
210
    /**
211
     * @param string|array $data
212
     * @param bool $hasArray
213
     * @return string
214
     * @throws Exceptions\TableNotDefinedException
215
     */
216 16
    protected function prepareFrom($data, $hasArray = false)
217
    {
218 16
        if (\is_array($data) && $hasArray === true) {
219 3
            $tmp = [];
220 3
            foreach ($data as $table) {
221 3
                $tmp[] = $table;
222
            }
223 3
            $data = implode(' ', $tmp);
224
        }
225 16
        if (! is_scalar($data) || empty($data)) {
226 1
            throw new Exceptions\TableNotDefinedException($data);
227
        }
228
229 16
        return $data;
230
    }
231
232
    /**
233
     * @param array|string $data
234
     * @return string
235
     * @throws Exceptions\InvalidFieldException
236
     */
237 13
    protected function prepareWhere($data)
238
    {
239 13
        if (\is_array($data)) {
240 3
            if ($this->arrayOnlyNumeric(array_keys($data)) === true) {
241 3
                $data = implode(' ', $data);
242
            } else {
243 1
                throw (new Exceptions\InvalidFieldException('WHERE'))
244 1
                    ->setData($data);
245
            }
246
        }
247 13
        $data = trim($data);
248 13
        if (! empty($data) && stripos($data, 'WHERE') !== 0) {
249 11
            $data = "WHERE {$data}";
250
        }
251
252 13
        return $data;
253
    }
254
255
    /**
256
     * @param string $data
257
     * @return string
258
     */
259 9
    protected function prepareOrder($data)
260
    {
261 9
        $data = trim($data);
262 9
        if (! empty($data) && stripos($data, 'ORDER') !== 0) {
263 5
            $data = "ORDER BY {$data}";
264
        }
265
266 9
        return $data;
267
    }
268
269
    /**
270
     * @param string $data
271
     * @return string
272
     */
273 11
    protected function prepareLimit($data)
274
    {
275 11
        $data = trim($data);
276 11
        if (! empty($data) && stripos($data, 'LIMIT') !== 0) {
277 5
            $data = "LIMIT {$data}";
278
        }
279
280 11
        return $data;
281
    }
282
283
    /**
284
     * @param $values
285
     * @return string
286
     */
287 10
    protected function withCommaSeparator($values)
288
    {
289 10
        if (\is_array($values)) {
290 10
            $values = implode(', ', $values);
291
        }
292
293 10
        return trim($values);
294
    }
295
}
296