ArrayField::getDataTransformer()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 4
rs 10
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