1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface as Container; |
6
|
|
|
|
7
|
|
|
class DatabaseUtility |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private $container; |
11
|
|
|
private $em; |
12
|
|
|
|
13
|
1 |
|
public function __construct(Container $container) |
14
|
|
|
{ |
15
|
1 |
|
$this->container = $container; |
16
|
1 |
|
$this->em = $this->container->get("doctrine")->getManager(); |
|
|
|
|
17
|
1 |
|
} |
18
|
|
|
|
19
|
1 |
|
public function getFieldType($entity, $field) |
20
|
|
|
{ |
21
|
1 |
|
$metadata = $this->em->getClassMetadata(get_class($entity)); |
|
|
|
|
22
|
1 |
|
$fieldMetadata = $metadata->fieldMappings[$field]; |
23
|
|
|
|
24
|
1 |
|
$fieldType = $fieldMetadata['type']; |
25
|
1 |
|
return $fieldType; |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
public function getEntityProperties($fieldname, $objrecord) |
29
|
|
|
{ |
30
|
1 |
|
$parametri = array('str' => $fieldname, 'primamaiuscola' => true); |
|
|
|
|
31
|
1 |
|
$getfieldname = \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
32
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
33
|
1 |
|
$getfieldname = "has" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
34
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
35
|
1 |
|
$getfieldname = "is" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
36
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
37
|
1 |
|
$getfieldname = "get" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
38
|
1 |
|
} |
39
|
1 |
|
} |
40
|
1 |
|
} |
41
|
1 |
|
$setfieldname = "set" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
42
|
|
|
|
43
|
1 |
|
return array("get" => $getfieldname, "set" => $setfieldname); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue) |
47
|
|
|
{ |
48
|
1 |
|
$fieldtype = $this->getFieldType(new $entity(), $fieldname); |
49
|
1 |
|
if ($fieldtype === 'datetime') { |
50
|
1 |
|
return $this->isDateChanged($oldvalue, $newvalue); |
51
|
|
|
} |
52
|
1 |
|
if (is_array($oldvalue)) { |
53
|
1 |
|
return $this->isArrayChanged($oldvalue, $newvalue); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return ($oldvalue !== $newvalue); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function isDateChanged($oldvalue, $newvalue) |
60
|
|
|
{ |
61
|
1 |
|
$datenewvalue = new \DateTime(); |
62
|
1 |
|
$datenewvalue->setTimestamp($newvalue); |
63
|
1 |
|
if (!$oldvalue && !$newvalue) { |
64
|
1 |
|
return false; |
65
|
|
|
} |
66
|
|
|
if ((!$oldvalue && $newvalue) || ($oldvalue && !$newvalue)) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
return ($oldvalue != $datenewvalue); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function isArrayChanged($oldvalue, $newvalue) |
73
|
|
|
{ |
74
|
1 |
|
if (!$oldvalue && !$newvalue) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
1 |
|
if ((!$oldvalue && $newvalue) || ($oldvalue && !$newvalue)) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
1 |
|
return count(array_diff($oldvalue, $newvalue)) > 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function entityExists($className) |
84
|
|
|
{ |
85
|
|
|
|
86
|
1 |
|
if (is_object($className)) { |
87
|
|
|
$className = ($className instanceof Proxy) ? get_parent_class($className) : get_class($className); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return !$this->em->getMetadataFactory()->isTransient($className); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function getEntityJoinTables($entityclass) |
94
|
|
|
{ |
95
|
1 |
|
$jointables = array(); |
96
|
1 |
|
$metadata = $this->em->getClassMetadata($entityclass); |
|
|
|
|
97
|
1 |
|
$fielsassoc = $metadata->associationMappings; |
98
|
1 |
|
foreach ($fielsassoc as $tableassoc) { |
99
|
1 |
|
if ($tableassoc["inversedBy"]) { |
|
|
|
|
100
|
1 |
|
$jointables[$tableassoc["targetEntity"]] = array("entity" => $tableassoc); |
|
|
|
|
101
|
1 |
|
} |
102
|
1 |
|
} |
103
|
1 |
|
return $jointables; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function entityHasJoinTables($entityclass) |
107
|
|
|
{ |
108
|
1 |
|
$jointables = $this->getEntityJoinTables($entityclass); |
109
|
1 |
|
return count($jointables) > 0 ? true : false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.