Completed
Pull Request — master (#36)
by Daniel
03:38
created

CollectionField   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 52
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getViewType() 0 4 1
A getFormType() 0 4 1
A getStorageType() 0 4 1
B configureOptions() 0 27 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ContentType\Standard\Field;
6
7
use Psi\Component\ContentType\FieldInterface;
8
use Psi\Component\ContentType\FieldRegistry;
9
use Psi\Component\ContentType\OptionsResolver\FieldOptionsResolver;
10
use Psi\Component\ContentType\Standard\View\CollectionView;
11
use Psi\Component\ContentType\Storage\ConfiguredType;
12
use Psi\Component\ContentType\Storage\TypeFactory;
13
use Symfony\Component\Form\Extension\Core\Type as FormType;
14
use Psi\Component\ContentType\Standard\View\CollectionType;
15
16
class CollectionField implements FieldInterface
17
{
18
    private $registry;
19
20
    public function __construct(FieldRegistry $registry)
21
    {
22
        $this->registry = $registry;
23
    }
24
25
    public function getViewType(): string
26
    {
27
        return CollectionType::class;
28
    }
29
30
    public function getFormType(): string
31
    {
32
        return FormType\CollectionType::class;
33
    }
34
35
    public function getStorageType(TypeFactory $factory): ConfiguredType
36
    {
37
        return $factory->create('collection');
38
    }
39
40
    public function configureOptions(FieldOptionsResolver $options)
41
    {
42
        $options->setRequired([
43
            'field_type',
44
        ]);
45
        $options->setDefault('field_options', []);
46
        $options->setFormMapper(function ($options) {
47
            $field = $this->registry->get($options['field_type']);
48
            $resolver = new FieldOptionsResolver();
49
            $field->configureOptions($resolver);
50
            $options = $resolver->resolveFormOptions($options['field_options']);
51
52
            return [
53
                'entry_type' => $field->getFormType(),
54
                'entry_options' => $options,
55
                'allow_add' => true,
56
                'allow_delete' => true,
57
            ];
58
        });
59
60
        $options->setViewMapper(function ($options) {
61
            return [
62
                'field_type' => $options['field_type'],
63
                'field_options' => $options['field_options']
64
            ];
65
        });
66
    }
67
}
68