1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pgs\RestfonyBundle\Form\Type; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Form\AbstractType; |
6
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
use Symfony\Component\OptionsResolver\Options; |
9
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
10
|
|
|
use Pgs\RestfonyBundle\Form\DataTransformer\RestCollectionTransformer; |
11
|
|
|
use Pgs\RestfonyBundle\Form\DataTransformer\ArrayToStringTransformer; |
12
|
|
|
|
13
|
|
|
class RestCollectionType extends AbstractType |
14
|
|
|
{ |
15
|
|
|
/** @var ObjectManager */ |
16
|
|
|
private $objectManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param ObjectManager $objectManager |
20
|
|
|
*/ |
21
|
2 |
|
public function __construct(ObjectManager $objectManager) |
22
|
|
|
{ |
23
|
2 |
|
$this->objectManager = $objectManager; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
1 |
|
public function buildForm(FormBuilderInterface $builder, array $options) |
30
|
|
|
{ |
31
|
1 |
|
$transformer = new RestCollectionTransformer($this->objectManager, $options['entityName']); |
32
|
1 |
|
$viewTransformer = new ArrayToStringTransformer(); |
33
|
1 |
|
$builder->addModelTransformer($transformer); |
34
|
1 |
|
$builder->addViewTransformer($viewTransformer); |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
1 |
View Code Duplication |
public function configureOptions(OptionsResolver $resolver) |
|
|
|
|
41
|
|
|
{ |
42
|
1 |
|
$resolver->setRequired(['entityName']); |
43
|
|
|
|
44
|
1 |
|
if ($resolver instanceof OptionsResolver) { |
45
|
1 |
|
$resolver->setAllowedTypes('entityName', ['string']); |
46
|
1 |
|
$resolver->setDefined(['entityName']); |
47
|
1 |
|
$resolver->setDefault('invalid_message', function (Options $options) { |
48
|
1 |
|
return 'This value is not valid. Unable to find ' . $options['entityName'] . ' in the database.'; |
49
|
1 |
|
}); |
50
|
|
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function getName() |
57
|
|
|
{ |
58
|
|
|
return 'collection'; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.