Columns::addColumn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDbDump/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDbDump\Db\Dump\Platform\Mysql\Table;
12
13
use ArrayObject;
14
use WebinoDbDump\Db\Dump\Table\AbstractColumns;
15
use WebinoDbDump\Db\Dump\Table\ColumnsInterface;
16
17
/**
18
 * Mysql database dump utility platform columns
19
 */
20
class Columns extends AbstractColumns implements ColumnsInterface
21
{
22
    /**
23
     * Matched against column type to decide is numeric
24
     */
25
    const NUMERIC_FIELD_PATTERN = '~^[^(]*(BYTE|COUNTER|SERIAL|INT|LONG|CURRENCY|REAL
26
                                   |MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER)~ix';
27
28
    /**
29
     * @param ArrayObject $column
30
     * @return $this
31
     */
32
    public function addColumn(ArrayObject $column)
33
    {
34
        $this->columns[$column['Field']] = [
35
            'numeric' => $this->resolveIsTableColumnNumeric($column),
36
        ];
37
38
        return $this;
39
    }
40
41
    /**
42
     * @param ArrayObject $row
43
     * @return string
44
     */
45
    public function tableRowValues(ArrayObject $row)
46
    {
47
        $values = [];
48
        foreach ($row as $col => $value) {
49
            if (null === $value) {
50
                $values[] = 'NULL';
51
            } elseif ($this->columns[$col]['numeric']) {
52
                $values[] = $value;
53
            } else {
54
                $values[] = $this->platform->quoteValue($value);
55
            }
56
        }
57
58
        return '(' . join(', ', $values) . ')';
59
    }
60
61
    /**
62
     * @param ArrayObject $column
63
     * @return bool
64
     */
65
    private function resolveIsTableColumnNumeric(ArrayObject $column)
66
    {
67
        return (bool) preg_match(self::NUMERIC_FIELD_PATTERN, $column['Type']);
68
    }
69
}
70