Completed
Push — master ( ce838f...e81cc5 )
by Portey
37:21
created

TypeMap::isScalarType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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