GenericDateItemConverter::convert()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.6333
c 0
b 0
f 0
cc 4
nc 8
nop 1
crap 4
1
<?php
2
3
namespace Mathielen\DataImport\ValueConverter;
4
5
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;
6
7
class GenericDateItemConverter extends DateTimeValueConverter
8
{
9 7
    public function __construct($outputFormat = 'Y-m-d')
10
    {
11 7
        parent::__construct(null, $outputFormat);
12 7
    }
13
14 5
    public function convert($input)
15
    {
16 5
        if (!$input) {
17 1
            return;
18
        }
19
20
        // dd.mm.yyyy, dd-mm-yyyy
21
        try {
22 4
            if (preg_match('/^([0-9]{1,2})[\.-]?([0-9]{1,2})[\.-]?([0-9]{2,4})$/', $input, $matches)) {
23 3
                $date = new \DateTime($matches[3].'-'.$matches[2].'-'.$matches[1]);
24
            } else {
25 1
                $date = new \DateTime($input);
26
            }
27
28 3
            return $this->formatOutput($date);
29 1
        } catch (\Exception $e) {
30 1
            return $input;
31
        }
32
    }
33
34 4
    protected function formatOutput(\DateTime $date)
35
    {
36 4
        return $date->format($this->outputFormat);
37
    }
38
}
39