ImageType   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 69
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 59 3
A configureOptions() 0 4 1
1
<?php
2
3
namespace App\Form;
4
5
use App\Entity\Image;
6
use App\Entity\Wander;
7
use Doctrine\ORM\EntityRepository;
8
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
9
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\CallbackTransformer;
11
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
12
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
13
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
14
use Symfony\Component\Form\Extension\Core\Type\NumberType;
15
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
21
class ImageType extends AbstractType
22
{
23
    public function buildForm(FormBuilderInterface $builder, array $options)
24
    {
25
        $builder
26
            //->add('imageFile', VichImageType::class)
27
            ->add('title', TextType::class)
28
            ->add('description', TextareaType::class, [
29
                'required' => false,
30
                'attr' => ['rows' => 10]
31
            ])
32
            ->add('wander', EntityType::class, [
33
                'required' => false,
34
                'class' => Wander::class,
35
                //'multiple' => true,
36
                //'by_reference' => false, // You took this back out when you changed to One-to-Many https://stackoverflow.com/a/35765987/300836
37
                'query_builder' => function(EntityRepository $er) {
38
                    return $er->createQueryBuilder('w')
39
                        ->orderBy('w.startTime', 'DESC');
40
                }
41
            ])
42
            ->add('rating', ChoiceType::class, [
43
                'choices'  => [
44
                    '-' => null,
45
                    '★' => 1,
46
                    '★★' => 2,
47
                    '★★★' => 3,
48
                    '★★★★' => 4,
49
                    '★★★★★' => 5,
50
                ],
51
            ])
52
            ->add('latlng', TextType::class, [
53
                'required' => false
54
            ])
55
            ->add('location', TextType::class, [
56
                'required' => false
57
            ])
58
            ->add('tagsText', TextType::class,
59
                [
60
                    'required' => false,
61
                    'label' => 'Tags'
62
                ]
63
            )
64
            ->add('capturedAt', DateTimeType::class, [
65
                'widget' => 'choice'
66
67
            ])
68
        ;
69
        // Transform latitude, longitude string to/from array
70
        $builder->get('latlng')
71
            ->addModelTransformer(new CallbackTransformer(
72
                function($latlngAsArray) {
73
                    if ($latlngAsArray !== null) {
74
                        return implode(', ', $latlngAsArray);
75
                    }
76
                },
77
                function($latlngAsString) {
78
                    if ($latlngAsString === null) {
79
                        return null;
80
                    }
81
                    return explode(',', preg_replace('/\s+/', '', $latlngAsString));
82
                }
83
            ));
84
    }
85
86
    public function configureOptions(OptionsResolver $resolver)
87
    {
88
        $resolver->setDefaults([
89
            'data_class' => Image::class,
90
        ]);
91
    }
92
}
93