Completed
Push — master ( 477ca5...2d5ed5 )
by Daniel
10s
created

CollectionField::getViewType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\CollectionType;
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
15
class CollectionField implements FieldInterface
16
{
17
    private $registry;
18
19
    public function __construct(FieldRegistry $registry)
20
    {
21
        $this->registry = $registry;
22
    }
23
24
    public function getViewType(): string
25
    {
26
        return CollectionType::class;
27
    }
28
29
    public function getFormType(): string
30
    {
31
        return FormType\CollectionType::class;
32
    }
33
34
    public function getStorageType(TypeFactory $factory): ConfiguredType
35
    {
36
        return $factory->create('collection');
37
    }
38
39
    public function configureOptions(FieldOptionsResolver $options)
40
    {
41
        $options->setRequired([
42
            'field_type',
43
        ]);
44
        $options->setDefault('field_options', []);
45
        $options->setFormMapper(function ($options) {
46
            $field = $this->registry->get($options['field_type']);
47
            $resolver = new FieldOptionsResolver();
48
            $field->configureOptions($resolver);
49
            $options = $resolver->resolveFormOptions($options['field_options']);
50
51
            return [
52
                'entry_type' => $field->getFormType(),
53
                'entry_options' => $options,
54
                'allow_add' => true,
55
                'allow_delete' => true,
56
            ];
57
        });
58
59
        $options->setViewMapper(function ($options) {
60
            return [
61
                'field_type' => $options['field_type'],
62
                'field_options' => $options['field_options'],
63
            ];
64
        });
65
    }
66
}
67