Completed
Push — master ( f8702a...461e07 )
by Alexandr
04:08
created

TypeMap::getScalarTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 11
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/30/15 12:36 AM
7
*/
8
9
namespace Youshido\GraphQL\Type;
10
11
12
use Youshido\GraphQL\Type\ListType\AbstractListType;
13
use Youshido\GraphQL\Type\Object\AbstractEnumType;
14
use Youshido\GraphQL\Type\Object\AbstractInputObjectType;
15
use Youshido\GraphQL\Type\Object\ObjectType;
16
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
17
use Youshido\GraphQL\Type\Scalar\StringType;
18
19
class TypeMap
20
{
21
22
    const KIND_SCALAR       = 'SCALAR';
23
    const KIND_OBJECT       = 'OBJECT';
24
    const KIND_INTERFACE    = 'INTERFACE';
25
    const KIND_UNION        = 'UNION';
26
    const KIND_ENUM         = 'ENUM';
27
    const KIND_INPUT_OBJECT = 'INPUT_OBJECT';
28
    const KIND_LIST         = 'LIST';
29
    const KIND_NON_NULL     = 'NON_NULL';
30
31
    const TYPE_INT        = 'int';
32
    const TYPE_FLOAT      = 'float';
33
    const TYPE_STRING     = 'string';
34
    const TYPE_BOOLEAN    = 'boolean';
35
    const TYPE_ID         = 'id';
36
    const TYPE_DATETIME   = 'datetime';
37
    const TYPE_DATETIMETZ = 'datetimetz';
38
    const TYPE_DATE       = 'date';
39
    const TYPE_TIMESTAMP  = 'timestamp';
40
41
    const TYPE_FUNCTION            = 'function';
42
    const TYPE_OBJECT_TYPE         = 'object_type';
43
    const TYPE_OBJECT_INPUT_TYPE   = 'object_input_type';
44
    const TYPE_LIST                = 'list';
45
    const TYPE_ARRAY               = 'array';
46
    const TYPE_ARRAY_OF_FIELDS     = 'array_of_fields';
47
    const TYPE_ARRAY_OF_INPUTS     = 'array_of_inputs';
48
    const TYPE_ARRAY_OF_VALUES     = 'array_of_values';
49
    const TYPE_ARRAY_OF_INTERFACES = 'array_of_interfaces';
50
    const TYPE_ANY                 = 'any';
51
    const TYPE_ANY_OBJECT          = 'any_object';
52
    const TYPE_ANY_INPUT           = 'any_input';
53
54
    private static $scalarObjectsCache = [];
55
56
    /**
57
     * @param mixed|AbstractType $type
58
     * @return bool
59
     */
60 17
    public static function isInputType($type)
61
    {
62 17
        if (is_object($type)) {
63 15
            $type = $type->getNullableType();
64 15
            return ($type instanceof AbstractScalarType)
65 7
                   || ($type instanceof AbstractInputObjectType)
66 7
                   || ($type instanceof AbstractEnumType)
67 15
                   || ($type instanceof AbstractListType);
68
        } else {
69 16
            return self::isScalarType($type);
70
        }
71
    }
72
73
    public static function getNamedType($object)
74
    {
75
        if (is_object($object)) {
76
            if ($object instanceof AbstractType) {
77
                return $object->getType();
78
            }
79
        } elseif (is_null($object)) {
80
            return null;
81
        } elseif (is_scalar($object)) {
82
            return new StringType();
83
        }
84
        throw new \Exception('Invalid type');
85
    }
86
87
    /**
88
     * @param string $type
89
     *
90
     * @return ObjectType
91
     */
92 29
    public static function getScalarTypeObject($type)
93
    {
94 29
        if (self::isScalarType($type)) {
95 29
            if (empty(self::$scalarObjectsCache[$type])) {
96 4
                $name = ucfirst($type);
97 4
                $name = $name == 'Datetime' ? 'DateTime' : $name;
98 4
                $name = $name == 'Datetimetz' ? 'DateTimeTz' : $name;
99
100 4
                $className                       = 'Youshido\GraphQL\Type\Scalar\\' . $name . 'Type';
101 4
                self::$scalarObjectsCache[$type] = new $className();
102
            }
103
104 29
            return self::$scalarObjectsCache[$type];
105
        } else {
106
            return null;
107
        }
108
    }
109
110 7
    public static function isInterface(TypeInterface $type)
111
    {
112 7
        return $type->getKind() == self::KIND_INTERFACE;
113
    }
114
115
116 31
    public static function isScalarType($typeName)
117
    {
118 31
        return in_array($typeName, self::getScalarTypes());
119
    }
120
121
    /**
122
     * @return AbstractType[]
123
     */
124 33
    public static function getScalarTypes()
125
    {
126
        return [
127 33
            self::TYPE_INT,
128 33
            self::TYPE_FLOAT,
129 33
            self::TYPE_STRING,
130 33
            self::TYPE_BOOLEAN,
131 33
            self::TYPE_ID,
132 33
            self::TYPE_DATETIME,
133 33
            self::TYPE_DATE,
134 33
            self::TYPE_TIMESTAMP,
135 33
            self::TYPE_DATETIMETZ,
136
        ];
137
    }
138
139
}