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