AutoField   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 45
ccs 21
cts 21
cp 1
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getDataTransformer() 0 32 9
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 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