AutoField::getDataTransformer()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 16
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 32
ccs 17
cts 17
cp 1
crap 9
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Field;
6
7
use Doctrine\Common\Collections\Collection;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class AutoField extends AbstractField
11
{
12
    public function configureOptions(OptionsResolver $resolver): void
13
    {
14 3
        $resolver
15
            ->setDefaults([
16
                'template' => '@LAGAdmin/fields/auto.html.twig',
17 3
                'date_format' => 'd/m/Y',
18 3
            ])
19
            ->setAllowedTypes('date_format', 'string')
20
        ;
21 3
    }
22
23 3
    public function getDataTransformer(): ?\Closure
24
    {
25 1
        return function ($data) {
26
            if ($data === null) {
27 1
                return '';
28 1
            }
29 1
30
            if (\is_string($data)) {
31
                return $data;
32 1
            }
33 1
34
            if (is_numeric($data)) {
35
                return (string) $data;
36 1
            }
37 1
38
            if (\is_array($data)) {
39
                return implode(',', $data);
40 1
            }
41 1
42
            if ($data instanceof \DateTimeInterface) {
43
                return $data->format($this->getOption('date_format'));
44 1
            }
45 1
46
            if ($data instanceof Collection) {
47
                return implode(',', $data->toArray());
48 1
            }
49 1
50
            if (\is_object($data) && method_exists($data, '__toString')) {
51
                return (string) $data;
52 1
            }
53 1
54
            return '';
55
        };
56 1
    }
57
}
58