|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Guesser; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Form\Guess\Guess; |
|
15
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
|
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
17
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
|
18
|
|
|
|
|
19
|
|
|
class TypeGuesser extends AbstractTypeGuesser |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
public function guessType($class, $property, ModelManagerInterface $modelManager) |
|
25
|
|
|
{ |
|
26
|
|
|
if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) { |
|
27
|
|
|
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
list($metadata, $propertyName, $parentAssociationMappings) = $ret; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
if ($metadata->hasAssociation($propertyName)) { |
|
33
|
|
|
$mapping = $metadata->getAssociationMapping($propertyName); |
|
34
|
|
|
|
|
35
|
|
|
switch ($mapping['type']) { |
|
36
|
|
|
case ClassMetadataInfo::ONE_TO_MANY: |
|
37
|
|
|
return new TypeGuess('orm_one_to_many', array(), Guess::HIGH_CONFIDENCE); |
|
38
|
|
|
|
|
39
|
|
|
case ClassMetadataInfo::MANY_TO_MANY: |
|
40
|
|
|
return new TypeGuess('orm_many_to_many', array(), Guess::HIGH_CONFIDENCE); |
|
41
|
|
|
|
|
42
|
|
|
case ClassMetadataInfo::MANY_TO_ONE: |
|
43
|
|
|
return new TypeGuess('orm_many_to_one', array(), Guess::HIGH_CONFIDENCE); |
|
44
|
|
|
|
|
45
|
|
|
case ClassMetadataInfo::ONE_TO_ONE: |
|
46
|
|
|
return new TypeGuess('orm_one_to_one', array(), Guess::HIGH_CONFIDENCE); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
switch ($metadata->getTypeOfField($propertyName)) { |
|
51
|
|
|
case 'array': |
|
52
|
|
|
case 'json': |
|
53
|
|
|
return new TypeGuess('array', array(), Guess::HIGH_CONFIDENCE); |
|
54
|
|
|
case 'boolean': |
|
55
|
|
|
return new TypeGuess('boolean', array(), Guess::HIGH_CONFIDENCE); |
|
56
|
|
|
case 'datetime': |
|
57
|
|
|
case 'vardatetime': |
|
58
|
|
|
case 'datetimetz': |
|
59
|
|
|
return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE); |
|
60
|
|
|
case 'date': |
|
61
|
|
|
return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE); |
|
62
|
|
|
case 'decimal': |
|
63
|
|
|
case 'float': |
|
64
|
|
|
return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE); |
|
65
|
|
|
case 'integer': |
|
66
|
|
|
case 'bigint': |
|
67
|
|
|
case 'smallint': |
|
68
|
|
|
return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE); |
|
69
|
|
|
case 'string': |
|
70
|
|
|
return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE); |
|
71
|
|
|
case 'text': |
|
72
|
|
|
return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE); |
|
73
|
|
|
case 'time': |
|
74
|
|
|
return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE); |
|
75
|
|
|
default: |
|
76
|
|
|
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.