DataTypeGuesser::guessPattern()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
namespace Knp\RadBundle\Form;
4
5
use Symfony\Component\Form\FormTypeGuesserInterface;
6
use Symfony\Component\Form\Guess\TypeGuess;
7
use Symfony\Component\Form\Guess\Guess;
8
9
class DataTypeGuesser implements FormTypeGuesserInterface
10
{
11
    private $data;
12
13
    public function setData($data = null)
14
    {
15
        $this->data = $data;
16
    }
17
18
    public function guessType($class, $property)
19
    {
20
        if ($property === '_id') {
21
            return new TypeGuess('hidden', array(), Guess::LOW_CONFIDENCE);
22
        }
23
24
        if (!isset($this->data->$property)) {
25
            return;
26
        }
27
        $data = $this->data->$property;
28
29
        $type = $this->getType($data);
30
31
        return new TypeGuess($type, array(), Guess::LOW_CONFIDENCE);
32
    }
33
34
    public function guessRequired($class, $property)
35
    {
36
    }
37
38
    public function guessPattern($class, $property)
39
    {
40
    }
41
42
    public function guessMaxLength($class, $property)
43
    {
44
    }
45
46
    public function guessMinLength($class, $property)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $property is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
    }
49
50
    private function getType($value)
51
    {
52
        if (is_object($value)) {
53
            switch (true) {
54
                case $value instanceof \DateTime:
55
                    return 'date';
56
                case $value instanceof \ArrayIterator:
57
                    return 'collection';
58
                default:
59
                    return 'text';
60
            }
61
        }
62
63
        switch (gettype($value)) {
64
            case 'boolean':
65
                return 'checkbox';
66
            case 'array':
67
                return 'collection';
68
            case 'string':
69
            default:
70
                return 'text';
71
        }
72
    }
73
}
74