DateTimeToStringValueConverter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 39
rs 10
c 1
b 0
f 0
ccs 8
cts 9
cp 0.8889

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A convert() 0 12 3
1
<?php
2
3
namespace Ddeboer\DataImport\ValueConverter;
4
5
use Ddeboer\DataImport\Exception\UnexpectedValueException;
6
7
/**
8
 * Convert an date time object into string
9
 */
10
class DateTimeToStringValueConverter
11
{
12
    /**
13
     * Date time format
14
     *
15
     * @var string
16
     * @see http://php.net/manual/en/datetime.createfromformat.php
17
     */
18
    protected $outputFormat;
19
20
    /**
21
     * @param string $outputFormat
22
     */
23 2
    public function __construct($outputFormat = 'Y-m-d H:i:s')
24
    {
25 2
        $this->outputFormat = $outputFormat;
26 2
    }
27
28
    /**
29
     * Convert string to date time object
30
     * using specified format
31
     *
32
     * @param mixed $input
33
     * @return \DateTime|string
34
     * @throws UnexpectedValueException
35
     */
36 2
    public function convert($input)
37
    {
38 2
        if (!$input) {
39
            return;
40
        }
41
42 2
        if (!($input instanceof \DateTime)) {
43 1
            throw new UnexpectedValueException('Input must be DateTime object.');
44
        }
45
46 1
        return $input->format($this->outputFormat);
47
    }
48
}
49