GenericDateItemConverter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 32
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A convert() 0 19 4
A formatOutput() 0 4 1
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