Completed
Push — master ( 2975e7...46c3ee )
by Rafael
07:49
created

DateType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 68
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseLiteral() 0 10 3
A serialize() 0 8 2
A __construct() 0 6 1
A parseValue() 0 8 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Type;
12
13
use GraphQL\Error\Error;
14
use GraphQL\Language\AST\StringValueNode;
15
use GraphQL\Type\Definition\ScalarType;
16
use GraphQL\Utils;
17
18
/**
19
 * Class DateType
20
 */
21
class DateType extends ScalarType
22
{
23
    public const DATE = 'Date';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function __construct(array $config = [])
29
    {
30
        $this->name = self::DATE;
31
        $this->description = 'An ISO-8601 encoded date string. Example: `1985-06-18`';
32
33
        parent::__construct($config);
34
    }
35
36
    /**
37
     * Serializes an internal value to include in a response.
38
     *
39
     * @param string $value
40
     *
41
     * @return string
42
     */
43
    public function serialize($value)
44
    {
45
        // Assuming internal representation of email is always correct:
46
        if ($value instanceof \DateTime) {
0 ignored issues
show
introduced by
$value is never a sub-type of DateTime.
Loading history...
47
            return $value->format('Y-m-d');
48
        }
49
50
        return $value;
51
    }
52
53
    /**
54
     * Parses an externally provided value (query variable) to use as an input
55
     *
56
     * @param mixed $value
57
     *
58
     * @return mixed
59
     *
60
     * @throws Error
61
     */
62
    public function parseValue($value)
63
    {
64
        $date = \DateTime::createFromFormat('Y-m-d', $value);
65
        if (!$date) {
66
            throw new Error(sprintf("Cannot represent following value as date: %s", Utils::printSafeJson($value)));
67
        }
68
69
        return $date;
70
    }
71
72
    /**
73
     * @param \GraphQL\Language\AST\Node $valueNode
74
     *
75
     * @return string
76
     *
77
     * @throws Error
78
     */
79
    public function parseLiteral($valueNode)
80
    {
81
        if (!$valueNode instanceof StringValueNode) {
82
            throw new Error(sprintf('Query error: Can only parse strings got: %s', $valueNode->kind), [$valueNode]);
83
        }
84
        if (!$date = \DateTime::createFromFormat('Y-m-d', $valueNode->value)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $date is dead and can be removed.
Loading history...
85
            throw new Error("Not a valid date", [$valueNode]);
86
        }
87
88
        return $valueNode->value;
89
    }
90
}
91