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

DateTimeParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 0
cbo 0
dl 0
loc 36
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A toObjectValue() 0 8 4
A toArrayValue() 0 8 2
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
}