1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineMongoDBAdminBundle\Guesser; |
15
|
|
|
|
16
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
17
|
|
|
use Doctrine\ODM\MongoDB\Types\Type; |
18
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
19
|
|
|
use Symfony\Component\Form\Guess\Guess; |
20
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
21
|
|
|
|
22
|
|
|
class TypeGuesser extends AbstractTypeGuesser |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function guessType($class, $property, ModelManagerInterface $modelManager) |
28
|
|
|
{ |
29
|
|
|
if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) { |
|
|
|
|
30
|
|
|
return new TypeGuess('text', [], Guess::LOW_CONFIDENCE); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
list($metadata, $propertyName, $parentAssociationMappings) = $ret; |
|
|
|
|
34
|
|
|
|
35
|
|
|
if ($metadata->hasAssociation($propertyName)) { |
36
|
|
|
$mapping = $metadata->fieldMappings[$propertyName]; |
37
|
|
|
|
38
|
|
|
switch ($mapping['type']) { |
39
|
|
|
case ClassMetadata::ONE: |
40
|
|
|
return new TypeGuess('mongo_one', [], Guess::HIGH_CONFIDENCE); |
41
|
|
|
|
42
|
|
|
case ClassMetadata::MANY: |
43
|
|
|
return new TypeGuess('mongo_many', [], Guess::HIGH_CONFIDENCE); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
switch ($metadata->getTypeOfField($propertyName)) { |
48
|
|
|
case Type::COLLECTION: |
49
|
|
|
case Type::HASH: |
50
|
|
|
return new TypeGuess('array', [], Guess::HIGH_CONFIDENCE); |
51
|
|
|
case 'array': |
52
|
|
|
@trigger_error( |
|
|
|
|
53
|
|
|
'The array type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
54
|
|
|
E_USER_DEPRECATED |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
return new TypeGuess('array', [], Guess::HIGH_CONFIDENCE); |
58
|
|
|
case Type::BOOL: |
59
|
|
|
case Type::BOOLEAN: |
60
|
|
|
return new TypeGuess('boolean', [], Guess::HIGH_CONFIDENCE); |
61
|
|
|
case 'datetime': |
62
|
|
|
@trigger_error( |
|
|
|
|
63
|
|
|
'The datetime type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
64
|
|
|
E_USER_DEPRECATED |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE); |
68
|
|
|
case 'vardatetime': |
69
|
|
|
@trigger_error( |
|
|
|
|
70
|
|
|
'The vardatetime type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
71
|
|
|
E_USER_DEPRECATED |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE); |
75
|
|
|
case 'datetimetz': |
76
|
|
|
@trigger_error( |
|
|
|
|
77
|
|
|
'The datetimetz type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
78
|
|
|
E_USER_DEPRECATED |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE); |
82
|
|
|
case Type::TIMESTAMP: |
83
|
|
|
return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE); |
84
|
|
|
case Type::DATE: |
85
|
|
|
// NEXT_MAJOR: Use only the constant when dropping support for doctrine/mongodb-odm 1.3. |
86
|
|
|
// case Type::DATE_IMMUTABLE: |
87
|
|
|
case 'date_immutable': |
88
|
|
|
return new TypeGuess('date', [], Guess::HIGH_CONFIDENCE); |
89
|
|
|
case 'decimal': |
90
|
|
|
@trigger_error( |
|
|
|
|
91
|
|
|
'The decimal type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
92
|
|
|
E_USER_DEPRECATED |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return new TypeGuess('number', [], Guess::MEDIUM_CONFIDENCE); |
96
|
|
|
case Type::FLOAT: |
97
|
|
|
return new TypeGuess('number', [], Guess::MEDIUM_CONFIDENCE); |
98
|
|
|
case Type::INTEGER: |
99
|
|
|
case Type::INT: |
100
|
|
|
return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE); |
101
|
|
|
case 'bigint': |
102
|
|
|
@trigger_error( |
|
|
|
|
103
|
|
|
'The bigint type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
104
|
|
|
E_USER_DEPRECATED |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE); |
108
|
|
|
case 'smallint': |
109
|
|
|
@trigger_error( |
|
|
|
|
110
|
|
|
'The smallint type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
111
|
|
|
E_USER_DEPRECATED |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE); |
115
|
|
|
case Type::STRING: |
116
|
|
|
return new TypeGuess('text', [], Guess::MEDIUM_CONFIDENCE); |
117
|
|
|
case 'text': |
118
|
|
|
@trigger_error( |
|
|
|
|
119
|
|
|
'The text type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
120
|
|
|
E_USER_DEPRECATED |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return new TypeGuess('textarea', [], Guess::MEDIUM_CONFIDENCE); |
124
|
|
|
case 'time': |
125
|
|
|
@trigger_error( |
|
|
|
|
126
|
|
|
'The time type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
127
|
|
|
E_USER_DEPRECATED |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return new TypeGuess('time', [], Guess::HIGH_CONFIDENCE); |
131
|
|
|
default: |
132
|
|
|
return new TypeGuess('text', [], Guess::LOW_CONFIDENCE); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.