ArrayField   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 34
ccs 15
cts 15
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createView() 0 3 1
A getDataTransformer() 0 16 4
A configureOptions() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Field;
6
7
use Doctrine\Common\Collections\Collection;
8
use LAG\AdminBundle\Field\View\TextView;
9
use LAG\AdminBundle\Field\View\View;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
class ArrayField extends AbstractField
13
{
14
    public function configureOptions(OptionsResolver $resolver): void
15
    {
16 1
        $resolver
17
            ->setDefaults([
18
                'glue' => ', ',
19 1
                'sortable' => false,
20 1
            ])
21
            ->setAllowedTypes('glue', 'string')
22
        ;
23 1
    }
24
25 1
    public function createView(): View
26
    {
27 1
        return new TextView($this->getName(), $this->getOptions(), $this->getDataTransformer());
28
    }
29 1
30
    public function getDataTransformer(): ?\Closure
31
    {
32 1
        return function ($data) {
33
            if ($data === null) {
34 1
                return '';
35 1
            }
36 1
37
            if ($data instanceof Collection) {
38
                $data = $data->toArray();
39 1
            }
40 1
41
            if ($data instanceof \Iterator) {
42
                $data = iterator_to_array($data);
43 1
            }
44 1
45
            return implode($this->getOption('glue'), $data);
46
        };
47 1
    }
48
}
49