DateTimeToStringValueConverter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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