Completed
Push — master ( 366fbf...5f1478 )
by Rafael
02:45
created

DateTimeParser::toObjectValue()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.2
cc 4
eloc 4
nc 2
nop 4
1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object\Parser;
11
12
class DateTimeParser implements ValueParserInterface
13
{
14
    const NAME = 'datetime';
15
16
    /**
17
     * @inheritDoc
18
     */
19
    public function getName()
20
    {
21
        return self::NAME;
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function toObjectValue($value, $type, \ReflectionProperty $property, $object)
28
    {
29
        if (is_string($value) && ($type === 'DateTime' || $type === '\DateTime')) {
30
            return new \DateTime($value);
31
        }
32
33
        return $value;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function toArrayValue($value, $type, \ReflectionProperty $property, $object)
40
    {
41
        if ($value instanceof \DateTime) {
42
            return $value->format('c');
43
        }
44
45
        return $value;
46
    }
47
}