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

CollectionField::configureOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 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