Passed
Push — master ( 92ac41...9b2544 )
by Dāvis
26:15
created

QuickInsertFunctions::numeric()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 3
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Repository;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Sludio\HelperBundle\Script\Utils\Helper;
7
8
abstract class QuickInsertFunctions
9
{
10
    protected static $mock = [];
11
    protected static $metadata = [];
12
    protected static $tableName;
13
    protected static $identifier;
14
15
    public static $entityManager;
16
    public static $connection;
17
18
    public static function init($manager = null)
19
    {
20
        if (self::$connection) {
21
            return;
22
        }
23
        global $kernel;
24
25
        if ('AppCache' === \get_class($kernel)) {
26
            $kernel = $kernel->getKernel();
27
        }
28
        $container = $kernel->getContainer();
29
30
        $manager = $manager ?: $container->getParameter('sludio_helper.entity.manager');
31
        self::$entityManager = $container->get('doctrine')->getManager($manager);
32
        self::$connection = self::$entityManager->getConnection();
33
    }
34
35
    protected static function extract($object)
36
    {
37
        self::init(false);
38
        $data = self::extractExt(self::$entityManager->getMetadataFactory()->getMetadataFor(\get_class($object)));
39
40
        self::$mock = $data['mock'];
41
        self::$tableName = $data['table'];
42
        self::$metadata[$data['table']] = $data['meta'];
43
        self::$identifier = $data['identifier'];
44
    }
45
46
    public static function extractExt(ClassMetadata $metadata)
47
    {
48
        $fields = $metadata->getFieldNames();
49
        $columns = $metadata->getColumnNames();
50
        $table = $metadata->getTableName();
51
        $identifier = null;
52
53
        $result = [];
54
        foreach ($fields as $key => $field) {
55
            /** @var $columns array */
56
            foreach ($columns as $key2 => $column) {
57
                if ($key === $key2) {
58
                    $result[$table][$field] = $column;
59
                    if ($field === $metadata->getIdentifier()[0]) {
60
                        $identifier = $column;
61
                    }
62
                }
63
            }
64
        }
65
66
        $data = [
67
            'mock' => $result,
68
            'table' => $table,
69
            'meta' => $metadata,
70
            'identifier' => $identifier,
71
        ];
72
73
        return $data;
74
    }
75
76
    protected static function buildExtra($extra)
77
    {
78
        $methods = [
79
            'GROUP BY',
80
            'HAVING',
81
            'ORDER BY',
82
        ];
83
        $sql = '';
84
85
        foreach ($methods as $method) {
86
            if (isset($extra[$method])) {
87
                $sql .= ' '.$method.' ';
88
                if (\is_array($extra[$method])) {
89
                    $sql .= implode(' ', $extra[$method]).' ';
90
                } else {
91
                    $sql .= $extra[$method].' ';
92
                }
93
            }
94
        }
95
96
        if (isset($extra['LIMIT']) && \is_array($extra['LIMIT'])) {
97
            if (isset($extra['LIMIT'][1])) {
98
                list($offset, $limit) = $extra['LIMIT'];
99
            } else {
100
                $offset = 0;
101
                $limit = $extra['LIMIT'][0];
102
            }
103
            $sql = sprintf('%sLIMIT %s, %s', $sql, $offset, $limit);
104
        }
105
106
        return Helper::oneSpace($sql);
107
    }
108
109
    private static function numeric($tableName, $key, $value)
110
    {
111
        $intTypes = [
112
            'boolean',
113
            'integer',
114
            'longint',
115
        ];
116
        $flip = array_flip(self::$mock[$tableName]);
117
118
        if (isset(self::$metadata[$tableName], $flip[$key])) {
119
            if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) {
120
                return true;
121
            }
122
123
            return false;
124
        }
125
126
        return is_numeric($value);
127
    }
128
129
    protected static function buildWhere($tableName, array $where)
130
    {
131
        $whereSql = '';
132
        if (!empty($where)) {
133
            reset($where);
134
            $first = key($where);
135
            $path = ' WHERE ';
136
            foreach ($where as $key => $value) {
137
                if (!\is_array($value) && isset(self::$mock[$tableName][$key])) {
138
                    $whereSql .= $path.self::$mock[$tableName][$key].' = '.(self::numeric($tableName, $key, $value) ? $value : "'".addslashes(trim($value))."'");
139
                } elseif (\is_array($value)) {
140
                    $whereSql .= $path.$value[0];
141
                } else {
142
                    $whereSql .= $path.$key.' = '.(self::numeric($tableName, $key, $value) ? $value : "'".addslashes(trim($value))."'");
143
                }
144
                if ($key === $first) {
145
                    $path = ' AND ';
146
                }
147
            }
148
        }
149
150
        return $whereSql;
151
    }
152
153
    protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = [])
154
    {
155
        self::init($manager);
156
        if (\is_object($object)) {
157
            self::extract($object);
158
            $tableName = self::$tableName;
159
            $columns = self::$mock[$tableName] ?: [];
160
            $type = 'object';
161
        } else {
162
            $tableName = $object['table_name'];
163
            unset($object['table_name']);
164
            $type = 'table';
165
            $columns = array_keys($object) ?: [];
166
        }
167
168
        if (isset($extraFields[$tableName])) {
169
            $columns = array_merge($columns, $extraFields[$tableName]);
170
        }
171
    }
172
173
    protected static function value($object, $variable, $type, $check = true)
174
    {
175
        $value = null;
176
        if ($type === 'object') {
177
            $value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}();
178
        } else {
179
            if (isset($object[$variable])) {
180
                $value = $object[$variable];
181
            }
182
        }
183
184
        if ($check) {
185
            Helper::variable($value);
186
        }
187
188
        return $value;
189
    }
190
}
191