Completed
Push — master ( ce0ced...50615c )
by Alexandr
02:46
created

TypeMap::isTypeAllowed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
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\Object\AbstractInputObjectType;
13
use Youshido\GraphQL\Type\Object\ObjectType;
14
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
15
16
class TypeMap
17
{
18
19
    const KIND_SCALAR = 'SCALAR';
20
    const KIND_OBJECT = 'OBJECT';
21
    const KIND_LIST   = 'LIST';
22
    const KIND_ENUM   = 'ENUM';
23
    const KIND_UNION  = 'UNION';
24
25
    const TYPE_INT     = 'int';
26
    const TYPE_FLOAT   = 'float';
27
    const TYPE_STRING  = 'string';
28
    const TYPE_BOOLEAN = 'boolean';
29
    const TYPE_ID      = 'id';
30
31
    const TYPE_FUNCTION          = 'function';
32
    const TYPE_OBJECT_TYPE       = 'object_type';
33
    const TYPE_OBJECT_INPUT_TYPE = 'object_input_type';
34
    const TYPE_LIST              = 'list';
35
    const TYPE_ARRAY             = 'array';
36
    const TYPE_ARRAY_OF_FIELDS   = 'array_of_fields';
37
    const TYPE_ARRAY_OF_INPUTS   = 'array_of_inputs';
38
    const TYPE_ANY               = 'any';
39
    const TYPE_ANY_OBJECT        = 'any_object';
40
    const TYPE_ANY_INPUT         = 'any_input';
41
42
    private static $scalarObjectsCache = [];
43
44 3
    public static function isInputType($type)
45
    {
46 3
        if (is_object($type)) {
47 1
            return ($type instanceof AbstractScalarType) || ($type instanceof AbstractInputObjectType);
48
        } else {
49 3
            return self::isScalarType($type);
50
        }
51
    }
52
53
    /**
54
     * @param string $type
55
     *
56
     * @return ObjectType
57
     */
58 8
    public static function getScalarTypeObject($type)
59
    {
60 8
        if (self::isScalarType($type)) {
61 8
            if (empty(self::$scalarObjectsCache[$type])) {
62 1
                $className                       = 'Youshido\GraphQL\Type\Scalar\\' . ucfirst($type) . 'Type';
63 1
                self::$scalarObjectsCache[$type] = new $className();
64 1
            }
65
66 8
            return self::$scalarObjectsCache[$type];
67
        } else {
68
            return null;
69
        }
70
    }
71
72 8
    public static function isScalarType($typeName)
73
    {
74 8
        return in_array(strtolower($typeName), self::getScalarTypes());
75
    }
76
77 4
    /**
78
     * @return AbstractType[]
79 4
     */
80
    public static function getScalarTypes()
81
    {
82
        return [
83
            self::TYPE_INT, self::TYPE_FLOAT, self::TYPE_STRING, self::TYPE_BOOLEAN, self::TYPE_ID
84
        ];
85 8
    }
86
87
}