Code Duplication    Length = 38-42 lines in 3 locations

src/Type/Scalar/DateType.php 1 location

@@ 17-54 (lines=38) @@
14
 * Class DateType
15
 * @package Youshido\GraphQL\Type\Scalar
16
 */
17
class DateType extends AbstractScalarType
18
{
19
20
    public function getName()
21
    {
22
        return 'Date';
23
    }
24
25
    /**
26
     * @param $value \DateTime
27
     * @return null|string
28
     */
29
    public function serialize($value)
30
    {
31
        if ($value === null) {
32
            return null;
33
        }
34
35
        return $value->format('Y-m-d');
36
    }
37
38
    public function isValidValue($value)
39
    {
40
        if (is_object($value)) {
41
            return true;
42
        }
43
44
        $d = \DateTime::createFromFormat('Y-m-d', $value);
45
46
        return $d && $d->format('Y-m-d') == $value;
47
    }
48
49
    public function getDescription()
50
    {
51
        return 'DEPRECATED. Use DateTime instead';
52
    }
53
54
}
55

Tests/DataProvider/TestTimeType.php 1 location

@@ 14-51 (lines=38) @@
11
12
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
13
14
class TestTimeType extends AbstractScalarType
15
{
16
17
    public function getName()
18
    {
19
        return 'TestTime';
20
    }
21
22
    /**
23
     * @param $value \DateTime
24
     * @return null|string
25
     */
26
    public function serialize($value)
27
    {
28
        if ($value === null) {
29
            return null;
30
        }
31
32
        return $value instanceof \DateTime ? $value->format('H:i:s') : $value;
33
    }
34
35
    public function isValidValue($value)
36
    {
37
        if (is_object($value)) {
38
            return true;
39
        }
40
41
        $d = \DateTime::createFromFormat('H:i:s', $value);
42
43
        return $d && $d->format('H:i:s') == $value;
44
    }
45
46
    public function getDescription()
47
    {
48
        return 'Representation time in "H:i:s" format';
49
    }
50
51
}

src/Type/Scalar/DateTimeTzType.php 1 location

@@ 11-52 (lines=42) @@
8
9
namespace Youshido\GraphQL\Type\Scalar;
10
11
class DateTimeTzType extends AbstractScalarType
12
{
13
    public function getName()
14
    {
15
        return 'DateTimeTz';
16
    }
17
18
    /**
19
     * @param $value \DateTime
20
     * @return null|string
21
     */
22
    public function serialize($value)
23
    {
24
        if ($value === null) {
25
            return null;
26
        }
27
28
        return $value->format('r');
29
    }
30
31
    public function parseValue($value)
32
    {
33
        return is_object($value) ? $this->serialize($value) : $value;
34
    }
35
36
    public function isValidValue($value)
37
    {
38
        if (is_object($value)) {
39
            return true;
40
        }
41
42
        $d = \DateTime::createFromFormat('D, d M Y H:i:s O', $value);
43
44
        return $d && $d->format('r') == $value;
45
    }
46
47
    public function getDescription()
48
    {
49
        return 'Representation of date and time in "r" format';
50
    }
51
52
}
53