Completed
Push — master ( 118ce9...9e4cdb )
by Alexandr
02:51
created

DateTimeType   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A serialize() 0 8 2
A parseValue() 0 4 2
A isValidValue() 0 10 3
A getDescription() 0 4 1
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 11/27/15 1:22 AM
7
*/
8
9
namespace Youshido\GraphQL\Type\Scalar;
10
11
class DateTimeType extends AbstractScalarType
12
{
13
14
    private $format;
15
16 4
    public function __construct($format = 'Y-m-d H:i:s')
17
    {
18 4
        $this->format = $format;
19 4
    }
20
21 3
    public function getName()
22
    {
23 3
        return 'DateTime';
24
    }
25
26
    /**
27
     * @param $value \DateTime
28
     * @return null|string
29
     */
30 2
    public function serialize($value)
31
    {
32 2
        if ($value === null) {
33 1
            return null;
34
        }
35
36 2
        return $value->format($this->format);
37
    }
38
39 2
    public function parseValue($value)
40
    {
41 2
        return is_object($value) ? $this->serialize($value) : $value;
42
    }
43
44 2
    public function isValidValue($value)
45
    {
46 2
        if (is_object($value)) {
47 1
            return true;
48
        }
49
50 2
        $d = \DateTime::createFromFormat($this->format, $value);
51
52 2
        return $d && $d->format($this->format) == $value;
53
    }
54
55 1
    public function getDescription()
56
    {
57 1
        return 'Representation of date and time in "Y-m-d H:i:s" format';
58
    }
59
60
}
61