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
|
|
|
if(\is_object($manager)){ |
32
|
|
|
self::$entityManager = $manager; |
33
|
|
|
} else { |
34
|
|
|
self::$entityManager = $container->get('doctrine')->getManager($manager); |
35
|
|
|
} |
36
|
|
|
self::$connection = self::$entityManager->getConnection(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected static function extract($object) |
40
|
|
|
{ |
41
|
|
|
self::init(false); |
42
|
|
|
$data = self::extractExt(self::$entityManager->getMetadataFactory()->getMetadataFor(\get_class($object))); |
43
|
|
|
|
44
|
|
|
self::$mock = $data['mock']; |
45
|
|
|
self::$tableName = $data['table']; |
46
|
|
|
self::$metadata[$data['table']] = $data['meta']; |
47
|
|
|
self::$identifier = $data['identifier']; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function extractExt(ClassMetadata $metadata) |
51
|
|
|
{ |
52
|
|
|
$fields = $metadata->getFieldNames(); |
53
|
|
|
$columns = $metadata->getColumnNames(); |
54
|
|
|
$table = $metadata->getTableName(); |
55
|
|
|
$identifier = null; |
56
|
|
|
|
57
|
|
|
$result = []; |
58
|
|
|
foreach ($fields as $key => $field) { |
59
|
|
|
/** @var $columns array */ |
60
|
|
|
foreach ($columns as $key2 => $column) { |
61
|
|
|
if ($key === $key2) { |
62
|
|
|
$result[$table][$field] = $column; |
63
|
|
|
if ($field === $metadata->getIdentifier()[0]) { |
64
|
|
|
$identifier = $column; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$data = [ |
71
|
|
|
'mock' => $result, |
72
|
|
|
'table' => $table, |
73
|
|
|
'meta' => $metadata, |
74
|
|
|
'identifier' => $identifier, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
return $data; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected static function buildExtra($extra) |
81
|
|
|
{ |
82
|
|
|
$methods = [ |
83
|
|
|
'GROUP BY', |
84
|
|
|
'HAVING', |
85
|
|
|
'ORDER BY', |
86
|
|
|
]; |
87
|
|
|
$sql = ''; |
88
|
|
|
|
89
|
|
|
foreach ($methods as $method) { |
90
|
|
|
if (isset($extra[$method])) { |
91
|
|
|
$sql .= ' '.$method.' '; |
92
|
|
|
if (\is_array($extra[$method])) { |
93
|
|
|
$sql .= implode(' ', $extra[$method]).' '; |
94
|
|
|
} else { |
95
|
|
|
$sql .= $extra[$method].' '; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (isset($extra['LIMIT']) && \is_array($extra['LIMIT'])) { |
101
|
|
|
if (isset($extra['LIMIT'][1])) { |
102
|
|
|
list($offset, $limit) = $extra['LIMIT']; |
103
|
|
|
} else { |
104
|
|
|
$offset = 0; |
105
|
|
|
$limit = $extra['LIMIT'][0]; |
106
|
|
|
} |
107
|
|
|
$sql = sprintf('%sLIMIT %s, %s', $sql, $offset, $limit); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return Helper::oneSpace($sql); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected static function numeric($tableName, $key, $value) |
114
|
|
|
{ |
115
|
|
|
$intTypes = [ |
116
|
|
|
'boolean', |
117
|
|
|
'integer', |
118
|
|
|
'longint', |
119
|
|
|
]; |
120
|
|
|
$flip = []; |
121
|
|
|
if(isset(self::$mock[$tableName])) { |
122
|
|
|
$flip = array_flip(self::$mock[$tableName]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (isset(self::$metadata[$tableName], $flip[$key])) { |
126
|
|
|
if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) { |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return is_numeric($value); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected static function buildWhere($tableName, array $where) |
137
|
|
|
{ |
138
|
|
|
$whereSql = ''; |
139
|
|
|
if (!empty($where)) { |
140
|
|
|
reset($where); |
141
|
|
|
$first = key($where); |
142
|
|
|
$path = ' WHERE '; |
143
|
|
|
foreach ($where as $key => $value) { |
144
|
|
|
if (!\is_array($value) && isset(self::$mock[$tableName][$key])) { |
145
|
|
|
$whereSql .= $path.self::$mock[$tableName][$key].' = '.(self::numeric($tableName, $key, $value) ? $value : "'".addslashes(trim($value))."'"); |
146
|
|
|
} elseif (\is_array($value)) { |
147
|
|
|
$whereSql .= $path.$value[0]; |
148
|
|
|
} else { |
149
|
|
|
$whereSql .= $path.$key.' = '.(self::numeric($tableName, $key, $value) ? $value : "'".addslashes(trim($value))."'"); |
150
|
|
|
} |
151
|
|
|
if ($key === $first) { |
152
|
|
|
$path = ' AND '; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $whereSql; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = []) |
161
|
|
|
{ |
162
|
|
|
self::init($manager); |
163
|
|
|
if (\is_object($object)) { |
164
|
|
|
self::extract($object); |
165
|
|
|
$tableName = self::$tableName; |
166
|
|
|
$columns = self::$mock[$tableName] ?: []; |
167
|
|
|
$type = 'object'; |
168
|
|
|
} else { |
169
|
|
|
$tableName = $object['table_name']; |
170
|
|
|
unset($object['table_name']); |
171
|
|
|
$type = 'table'; |
172
|
|
|
$columns = array_keys($object) ?: []; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (isset($extraFields[$tableName])) { |
176
|
|
|
$columns = array_merge($columns, $extraFields[$tableName]); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
protected static function value($object, $variable, $type, $check = true) |
181
|
|
|
{ |
182
|
|
|
$value = null; |
183
|
|
|
if ($type === 'object') { |
184
|
|
|
$value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}(); |
185
|
|
|
} else { |
186
|
|
|
if (isset($object[$variable])) { |
187
|
|
|
$value = $object[$variable]; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ($check) { |
192
|
|
|
Helper::variable($value); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $value; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|