Completed
Push — master ( f9f0ef...24a695 )
by Portey
05:15
created

TypeMap::isInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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