Completed
Pull Request — master (#3)
by Harry
05:31
created

DateTimeProcessor::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Format\Processor;
15
16
use DateTimeInterface;
17
18
class DateTimeProcessor implements ProcessorInterface
19
{
20
    use InvokeProcessor;
21
22
    const FORMAT = 'Y-m-d H:i:s';
23
24
    /**
25
     * @var string
26
     */
27
    private $format;
28
29
    /**
30
     * @param string|null $format Custom DateTime format to use (Defaults to: Y-m-d H:i:s)
31
     */
32 21
    public function __construct($format = null)
33
    {
34 21
        $this->format = $format ?: static::FORMAT;
35 21
    }
36
37
    /**
38
     * @param array $row
39
     *
40
     * @return array
41
     */
42 17
    public function process(array $row)
43
    {
44 17
        foreach ($row as &$item) {
45 17
            if ($item instanceof DateTimeInterface) {
46 8
                $item = $item->format($this->format);
47 8
            }
48 17
        }
49
50 17
        return $row;
51
    }
52
}
53