DateDataField::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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