Completed
Push — master ( 778938...5ff82c )
by Markus
11:26
created

GenericDateItemConverter::formatOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Mathielen\DataImport\ValueConverter;
3
4
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;
5
6
class GenericDateItemConverter extends DateTimeValueConverter
7
{
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 null;
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 3
            } 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
}
40