|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Twig\Extension; |
|
4
|
|
|
|
|
5
|
|
|
class ObjectToTabellaExtension extends \Twig_Extension |
|
6
|
|
|
{ |
|
7
|
|
|
protected $fifreetableprefix; |
|
8
|
|
|
|
|
9
|
10 |
|
public function __construct($fifreetableprefix) |
|
10
|
|
|
{ |
|
11
|
10 |
|
$this->fifreetableprefix = $fifreetableprefix; |
|
12
|
10 |
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function getFunctions() |
|
15
|
|
|
{ |
|
16
|
|
|
return array( |
|
17
|
|
|
new \Twig_SimpleFunction('object2view', array($this, 'object2View', 'is_safe' => array('html'))), |
|
18
|
|
|
new \Twig_SimpleFunction('field2object', array($this, 'field2Object', 'is_safe' => array('html'))), |
|
19
|
|
|
); |
|
20
|
|
|
} |
|
21
|
6 |
|
public function object2View($object, $type = null) |
|
22
|
|
|
{ |
|
23
|
6 |
|
$risposta = null; |
|
24
|
|
|
|
|
25
|
6 |
|
if (!is_null($object)) { |
|
26
|
6 |
|
if ($type === null) { |
|
27
|
|
|
$tipo = is_object($object) ? |
|
28
|
|
|
get_class($object) : |
|
29
|
|
|
gettype($object); |
|
30
|
|
|
} else { |
|
31
|
6 |
|
$tipo = $type; |
|
32
|
|
|
} |
|
33
|
6 |
|
switch (strtolower($tipo)) { |
|
34
|
6 |
|
case 'array': |
|
35
|
|
|
$risposta = var_export($object); |
|
|
|
|
|
|
36
|
|
|
break; |
|
37
|
6 |
|
case 'date': |
|
38
|
1 |
|
$risposta = $object->format("d/m/Y"); |
|
|
|
|
|
|
39
|
1 |
|
break; |
|
40
|
6 |
|
case 'datetime': |
|
41
|
1 |
|
$risposta = $object->format("d/m/Y H:i"); |
|
|
|
|
|
|
42
|
1 |
|
break; |
|
43
|
6 |
|
case 'boolean': |
|
44
|
4 |
|
$risposta = $object ? "SI" : "NO"; |
|
|
|
|
|
|
45
|
4 |
|
break; |
|
46
|
6 |
|
case 'string': |
|
47
|
|
|
default: |
|
48
|
6 |
|
$risposta = $object; |
|
49
|
6 |
|
break; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
6 |
|
return $risposta; |
|
53
|
|
|
} |
|
54
|
6 |
|
private function getObjectProperty($field, $object) |
|
55
|
|
|
{ |
|
56
|
6 |
|
$property = "get" . ucfirst($field); |
|
|
|
|
|
|
57
|
6 |
|
if (method_exists($object, $property)) { |
|
58
|
6 |
|
return $property; |
|
59
|
|
|
} |
|
60
|
6 |
|
$property = "is" . ucfirst($field); |
|
|
|
|
|
|
61
|
6 |
|
if (method_exists($object, $property)) { |
|
62
|
|
|
return $property; |
|
63
|
|
|
} |
|
64
|
6 |
|
$property = "has" . ucfirst($field); |
|
|
|
|
|
|
65
|
6 |
|
if (method_exists($object, $property)) { |
|
66
|
|
|
return $property; |
|
67
|
|
|
} |
|
68
|
6 |
|
} |
|
69
|
6 |
|
public function field2Object($fieldtoobj, $object) |
|
70
|
|
|
{ |
|
71
|
6 |
|
$property = ""; |
|
|
|
|
|
|
72
|
6 |
|
$field = ""; |
|
|
|
|
|
|
73
|
6 |
|
$propertyfound = false; |
|
74
|
6 |
|
$subfields = explode(".", str_replace($this->fifreetableprefix, "", $fieldtoobj)); |
|
|
|
|
|
|
75
|
6 |
|
foreach ($subfields as $field) { |
|
76
|
6 |
|
$property = $this->getObjectProperty($field, $object); |
|
77
|
6 |
|
if ($property) { |
|
78
|
6 |
|
$object = $object->$property(); |
|
|
|
|
|
|
79
|
6 |
|
$propertyfound = true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
6 |
|
if (!$propertyfound) { |
|
83
|
|
|
throw new \Exception("Proprietà " . $field . " non trovata per " . $fieldtoobj); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
return $object; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.