Passed
Push — master ( 046444...56e09b )
by Andrea
28:16 queued 22:41
created

DoctrineFieldReader::getField2Object()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 0
Metric Value
cc 6
eloc 15
c 0
b 0
f 0
nc 12
nop 3
dl 0
loc 23
ccs 15
cts 16
cp 0.9375
crap 6.0087
rs 9.2222
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Entity;
4
5
use Exception;
6
use Doctrine\Inflector\Inflector;
7
use Doctrine\Inflector\NoopWordInflector;
8
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils;
9
10
class DoctrineFieldReader
11
{
12
    private $tableprefix;
13
14 7
    public function __construct($tableprefix)
15
    {
16 7
        $this->tableprefix = $tableprefix;
17 7
    }
18
19 7
    public function getField2Object($fieldname, $object, $decodifiche = null)
20
    {
21 7
        $property = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $property is dead and can be removed.
Loading history...
22 7
        $field = '';
23 7
        $propertyfound = false;
24 7
        $subfields = explode('.', str_replace($this->tableprefix, '', $fieldname));
25 7
        foreach ($subfields as $field) {
26 7
            $property = $this->getObjectProperty($field, $object);
27 7
            if ($property) {
28 7
                $object = $object->$property();
29 7
                $propertyfound = true;
30
            }
31
        }
32 7
        if (!$propertyfound) {
33
            throw new Exception('Proprietà '.$field.' non trovata per '.$fieldname);
34
        }
35 7
        if ($decodifiche) {
36 1
            if (key_exists($object, $decodifiche)) {
37 1
                $object = $decodifiche[$object];
38
            }
39
        }
40
41 7
        return $object;
42
    }
43
44 7
    public function object2View($object, $type = null, $decodifiche = null)
45
    {
46 7
        $risposta = null;
47
48 7
        if ($decodifiche) {
49
            $type = 'string';
50
        }
51
52 7
        if (!is_null($object)) {
53 7
            switch (strtolower($this->getObjectType($type, $object))) {
54 7
                case 'array':
55 1
                    $risposta = print_r($object, true);
56 1
                    break;
57 7
                case 'date':
58 1
                    $risposta = $object->format(FieldTypeUtils::getEnvVar("DATE_FORMAT", "d/m/Y"));
59 1
                    break;
60 7
                case 'datetime':
61 2
                    $risposta = $object->format(FieldTypeUtils::getEnvVar("DATETIME_FORMAT", "d/m/Y H:i"));
62 2
                    break;
63 7
                case 'string2datetime':
64
                    $time = strtotime($object);
65
                    $risposta = date(FieldTypeUtils::getEnvVar("DATETIME_FORMAT", "d/m/Y H:i"), $time);
66
                    break;
67 7
                case 'string2date':
68
                    $time = strtotime($object);
69
                    $risposta = date(FieldTypeUtils::getEnvVar("DATE_FORMAT", "d/m/Y"), $time);
70
                    break;
71 7
                case 'string2bool':
72
                    $risposta = $object ? 'SI' : 'NO';
73
                    break;
74 7
                case 'boolean':
75 5
                    $risposta = $object ? 'SI' : 'NO';
76 5
                    break;
77 7
                case 'string':
78
                default:
79 7
                    $risposta = $object;
80 7
                    break;
81
            }
82
        }
83
84 7
        return $risposta;
85
    }
86
87 7
    private function getObjectType($type, $object)
88
    {
89 7
        if (null === $type) {
90
            $tipo = is_object($object) ?
91
                    get_class($object) :
92
                    gettype($object);
93
        } else {
94 7
            $tipo = $type;
95
        }
96
97 7
        return $tipo;
98
    }
99
100 7
    private function getObjectProperty($field, $object)
101
    {
102 7
        $inflector = new Inflector(new NoopWordInflector(), new NoopWordInflector());
103
104 7
        $property = 'get'.$inflector->camelize(ucfirst($field));
105 7
        if (method_exists($object, $property)) {
106 7
            return $property;
107
        }
108 7
        $property = 'get'.ucfirst($field);
109 7
        if (method_exists($object, $property)) {
110
            return $property;
111
        }
112 7
        $property = 'is'.ucfirst($field);
113 7
        if (method_exists($object, $property)) {
114 1
            return $property;
115
        }
116 7
        $property = 'has'.ucfirst($field);
117 7
        if (method_exists($object, $property)) {
118
            return $property;
119
        }
120 7
    }
121
}
122