DateDataField   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Database;
6
7
use DateTime;
8
use RuntimeException;
9
10
class DateDataField extends AbstractDataField implements DataFieldInterface
11
{
12 336
    public function __construct(string $name)
13
    {
14 336
        parent::__construct($name, function ($input) use ($name): string {
15
            // empty string
16 182
            if ('' === $input) {
17 179
                return '';
18
            }
19
20
            // input formats
21 18
            foreach (['Y-m-d', 'd/m/Y'] as $format) {
22 18
                $date = DateTime::createFromFormat($format, $input);
23 18
                if ($date instanceof DateTime) {
24 17
                    return $date->format('Y-m-d');
25
                }
26
            }
27
28
            // don't know how to handle
29 1
            throw new RuntimeException("Para el campo $name la fecha $input no pudo ser interpretada");
30 336
        });
31
    }
32
}
33