1 | <?php |
||
30 | class FormContractor implements FormContractorInterface |
||
31 | { |
||
32 | /** |
||
33 | * NEXT_MAJOR: remove this property. |
||
34 | * |
||
35 | * @deprecated since sonata-project/doctrine-orm-admin-bundle 3.0.4, to be removed in 4.0 |
||
36 | * |
||
37 | * @var FormFactoryInterface |
||
38 | */ |
||
39 | protected $fieldFactory; |
||
40 | |||
41 | /** |
||
42 | * @var FormFactoryInterface |
||
43 | */ |
||
44 | protected $formFactory; |
||
45 | |||
46 | public function __construct(FormFactoryInterface $formFactory) |
||
50 | |||
51 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void |
||
82 | |||
83 | /** |
||
84 | * @return FormFactoryInterface |
||
85 | */ |
||
86 | public function getFormFactory() |
||
90 | |||
91 | public function getFormBuilder($name, array $options = []) |
||
95 | |||
96 | public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
||
97 | { |
||
98 | $options = []; |
||
99 | $options['sonata_field_description'] = $fieldDescription; |
||
100 | |||
101 | if ($this->isAnyInstanceOf($type, [ |
||
102 | ModelType::class, |
||
103 | ModelTypeList::class, |
||
104 | ModelListType::class, |
||
105 | ModelHiddenType::class, |
||
106 | ModelAutocompleteType::class, |
||
107 | ])) { |
||
108 | if ('list' === $fieldDescription->getOption('edit')) { |
||
109 | throw new \LogicException(sprintf( |
||
110 | 'The `%s` type does not accept an `edit` option anymore,' |
||
111 | .' please review the UPGRADE-2.1.md file from the SonataAdminBundle', |
||
112 | ModelType::class |
||
113 | )); |
||
114 | } |
||
115 | |||
116 | $options['class'] = $fieldDescription->getTargetModel(); |
||
|
|||
117 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
118 | |||
119 | if ($this->isAnyInstanceOf($type, [ModelAutocompleteType::class])) { |
||
120 | if (!$fieldDescription->getAssociationAdmin()) { |
||
121 | throw new \RuntimeException(sprintf( |
||
122 | 'The current field `%s` is not linked to an admin.' |
||
123 | .' Please create one for the target entity: `%s`', |
||
124 | $fieldDescription->getName(), |
||
125 | $fieldDescription->getTargetModel() |
||
126 | )); |
||
127 | } |
||
128 | } |
||
129 | } elseif ($this->isAnyInstanceOf($type, [AdminType::class])) { |
||
130 | if (!$fieldDescription->getAssociationAdmin()) { |
||
131 | throw new \RuntimeException(sprintf( |
||
132 | 'The current field `%s` is not linked to an admin.' |
||
133 | .' Please create one for the target entity : `%s`', |
||
134 | $fieldDescription->getName(), |
||
135 | $fieldDescription->getTargetModel() |
||
136 | )); |
||
137 | } |
||
138 | |||
139 | if (!\in_array($fieldDescription->getMappingType(), [ |
||
140 | ClassMetadata::ONE_TO_ONE, |
||
141 | ClassMetadata::MANY_TO_ONE, |
||
142 | ], true)) { |
||
143 | throw new \RuntimeException(sprintf( |
||
144 | 'You are trying to add `%s` field `%s` which is not One-To-One or Many-To-One.' |
||
145 | .' Maybe you want `%s` instead?', |
||
146 | AdminType::class, |
||
147 | $fieldDescription->getName(), |
||
148 | CollectionType::class |
||
149 | )); |
||
150 | } |
||
151 | |||
152 | // set sensitive default value to have a component working fine out of the box |
||
153 | $options['btn_add'] = false; |
||
154 | $options['delete'] = false; |
||
155 | |||
156 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
157 | $options['empty_data'] = static function () use ($fieldDescription) { |
||
158 | return $fieldDescription->getAssociationAdmin()->getNewInstance(); |
||
159 | }; |
||
160 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
161 | // NEXT_MAJOR: remove 'Sonata\CoreBundle\Form\Type\CollectionType' |
||
162 | } elseif ($this->isAnyInstanceOf($type, [CollectionType::class, 'Sonata\CoreBundle\Form\Type\CollectionType'])) { |
||
163 | if (!$fieldDescription->getAssociationAdmin()) { |
||
164 | throw new \RuntimeException(sprintf( |
||
165 | 'The current field `%s` is not linked to an admin.' |
||
166 | .' Please create one for the target entity : `%s`', |
||
167 | $fieldDescription->getName(), |
||
168 | $fieldDescription->getTargetModel() |
||
169 | )); |
||
170 | } |
||
171 | |||
172 | $options['type'] = AdminType::class; |
||
173 | $options['modifiable'] = true; |
||
174 | $options['type_options'] = [ |
||
175 | 'sonata_field_description' => $fieldDescription, |
||
176 | 'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
||
177 | 'empty_data' => static function () use ($fieldDescription) { |
||
178 | return $fieldDescription->getAssociationAdmin()->getNewInstance(); |
||
179 | }, |
||
180 | ]; |
||
181 | } |
||
182 | |||
183 | return $options; |
||
184 | } |
||
185 | |||
186 | private function hasAssociation(FieldDescriptionInterface $fieldDescription): bool |
||
195 | |||
196 | /** |
||
197 | * @param string[] $classes |
||
198 | */ |
||
199 | private function isAnyInstanceOf(string $type, array $classes): bool |
||
209 | } |
||
210 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: