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