1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
6
|
|
|
|
7
|
|
|
class Filters |
8
|
|
|
{ |
9
|
|
|
public static function filterGetResult($result = null, array $fields = [], $one = false) |
10
|
|
|
{ |
11
|
|
|
if ($result) { |
12
|
|
|
/** @var array $result */ |
13
|
|
|
$field = (\count($fields) === 1 && $fields[0] !== '*') ? $fields[0] : null; |
14
|
|
|
if ($field !== null) { |
15
|
|
|
if (!$one) { |
16
|
|
|
foreach ($result as &$res) { |
17
|
|
|
$res = $res[$field]; |
18
|
|
|
} |
19
|
|
|
} else { |
20
|
|
|
$result = $result[0][$field]; |
21
|
|
|
} |
22
|
|
|
} elseif ($one) { |
23
|
|
|
$result = $result[0]; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $result; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function getLimit(array $extra = [], &$sql) |
31
|
|
|
{ |
32
|
|
|
if (isset($extra['LIMIT']) && \is_array($extra['LIMIT'])) { |
33
|
|
|
if (isset($extra['LIMIT'][1])) { |
34
|
|
|
list($offset, $limit) = $extra['LIMIT']; |
35
|
|
|
} else { |
36
|
|
|
$offset = 0; |
37
|
|
|
$limit = $extra['LIMIT'][0]; |
38
|
|
|
} |
39
|
|
|
$sql = sprintf('%sLIMIT %s, %s', $sql, $offset, $limit); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function extractExt(ClassMetadata $metadata) |
44
|
|
|
{ |
45
|
|
|
$fields = $metadata->getFieldNames(); |
46
|
|
|
$columns = $metadata->getColumnNames(); |
47
|
|
|
$table = $metadata->getTableName(); |
48
|
|
|
$identifier = null; |
49
|
|
|
|
50
|
|
|
$result = []; |
51
|
|
|
foreach ($fields as $key => $field) { |
52
|
|
|
/** @var $columns array */ |
53
|
|
|
foreach ($columns as $key2 => $column) { |
54
|
|
|
if ($key === $key2) { |
55
|
|
|
$result[$table][$field] = $column; |
56
|
|
|
if ($field === $metadata->getIdentifier()[0]) { |
57
|
|
|
$identifier = $column; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$data = [ |
64
|
|
|
'mock' => $result, |
65
|
|
|
'table' => $table, |
66
|
|
|
'meta' => $metadata, |
67
|
|
|
'identifier' => $identifier, |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
return $data; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|