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

GenericDateItemConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 34
ccs 15
cts 15
cp 1
rs 10

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
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