1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Entity; |
4
|
|
|
|
5
|
|
|
class DoctrineFieldReader |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $tableprefix; |
9
|
|
|
|
10
|
7 |
|
public function __construct($tableprefix) |
11
|
|
|
{ |
12
|
7 |
|
$this->tableprefix = $tableprefix; |
13
|
7 |
|
} |
14
|
7 |
|
public function getField2Object($fieldname, $object, $decodifiche = null) |
15
|
|
|
{ |
16
|
7 |
|
$property = ''; |
|
|
|
|
17
|
7 |
|
$field = ''; |
|
|
|
|
18
|
7 |
|
$propertyfound = false; |
19
|
7 |
|
$subfields = explode('.', str_replace($this->tableprefix, '', $fieldname)); |
|
|
|
|
20
|
7 |
|
foreach ($subfields as $field) { |
21
|
7 |
|
$property = $this->getObjectProperty($field, $object); |
22
|
7 |
|
if ($property) { |
23
|
7 |
|
$object = $object->$property(); |
|
|
|
|
24
|
7 |
|
$propertyfound = true; |
25
|
|
|
} |
26
|
|
|
} |
27
|
7 |
|
if (!$propertyfound) { |
28
|
|
|
throw new \Exception('Proprietà ' . $field . ' non trovata per ' . $fieldname); |
29
|
|
|
} |
30
|
7 |
|
if ($decodifiche) { |
31
|
|
|
if (key_exists($object, $decodifiche)) { |
32
|
|
|
$object = $decodifiche[$object]; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
7 |
|
return $object; |
37
|
|
|
} |
38
|
7 |
|
public function object2View($object, $type = null, $decodifiche = null) |
39
|
|
|
{ |
40
|
7 |
|
$risposta = null; |
41
|
|
|
|
42
|
7 |
|
if ($decodifiche) { |
43
|
|
|
$type = 'string'; |
44
|
|
|
} |
45
|
|
|
|
46
|
7 |
|
if (!is_null($object)) { |
47
|
7 |
|
switch (strtolower($this->getObjectType($type, $object))) { |
48
|
7 |
|
case 'array': |
49
|
1 |
|
$risposta = print_r($object, true); |
50
|
1 |
|
break; |
51
|
7 |
|
case 'date': |
52
|
1 |
|
$risposta = $object->format('d/m/Y'); |
53
|
1 |
|
break; |
54
|
7 |
|
case 'datetime': |
55
|
2 |
|
$risposta = $object->format('d/m/Y H:i'); |
56
|
2 |
|
break; |
57
|
7 |
|
case 'boolean': |
58
|
5 |
|
$risposta = $object ? 'SI' : 'NO'; |
59
|
5 |
|
break; |
60
|
7 |
|
case 'string': |
61
|
|
|
default: |
62
|
7 |
|
$risposta = $object; |
63
|
7 |
|
break; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
return $risposta; |
68
|
|
|
} |
69
|
7 |
|
private function getObjectType($type, $object) |
70
|
|
|
{ |
71
|
7 |
|
if (null === $type) { |
72
|
|
|
$tipo = is_object($object) ? |
73
|
|
|
get_class($object) : |
74
|
|
|
gettype($object); |
75
|
|
|
} else { |
76
|
7 |
|
$tipo = $type; |
77
|
|
|
} |
78
|
7 |
|
return $tipo; |
79
|
|
|
} |
80
|
7 |
|
private function getObjectProperty($field, $object) |
81
|
|
|
{ |
82
|
7 |
|
$property = 'get' . ucfirst($field); |
83
|
7 |
|
if (method_exists($object, $property)) { |
84
|
7 |
|
return $property; |
85
|
|
|
} |
86
|
7 |
|
$property = 'is' . ucfirst($field); |
87
|
7 |
|
if (method_exists($object, $property)) { |
88
|
1 |
|
return $property; |
89
|
|
|
} |
90
|
7 |
|
$property = 'has' . ucfirst($field); |
91
|
7 |
|
if (method_exists($object, $property)) { |
92
|
|
|
return $property; |
93
|
|
|
} |
94
|
7 |
|
} |
95
|
|
|
} |
96
|
|
|
|