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

DateTimeType::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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