|
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_ANY = 'any'; |
|
47
|
|
|
const TYPE_ANY_OBJECT = 'any_object'; |
|
48
|
|
|
const TYPE_ANY_INPUT = 'any_input'; |
|
49
|
|
|
|
|
50
|
|
|
private static $scalarObjectsCache = []; |
|
51
|
|
|
|
|
52
|
15 |
|
public static function isInputType($type) |
|
53
|
|
|
{ |
|
54
|
15 |
|
if (is_object($type)) { |
|
55
|
12 |
|
return ($type instanceof AbstractScalarType) |
|
56
|
6 |
|
|| ($type instanceof AbstractInputObjectType) |
|
57
|
6 |
|
|| ($type instanceof AbstractEnumType) |
|
58
|
12 |
|
|| ($type instanceof AbstractListType); |
|
59
|
|
|
} else { |
|
60
|
11 |
|
return self::isScalarType($type); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $type |
|
66
|
|
|
* |
|
67
|
|
|
* @return ObjectType |
|
68
|
|
|
*/ |
|
69
|
23 |
|
public static function getScalarTypeObject($type) |
|
70
|
|
|
{ |
|
71
|
23 |
|
if (self::isScalarType($type)) { |
|
72
|
23 |
|
if (empty(self::$scalarObjectsCache[$type])) { |
|
73
|
4 |
|
$name = ucfirst($type); |
|
74
|
4 |
|
$name = $name == 'Datetime' ? 'DateTime' : $name; |
|
75
|
|
|
|
|
76
|
4 |
|
$className = 'Youshido\GraphQL\Type\Scalar\\' . $name . 'Type'; |
|
77
|
4 |
|
self::$scalarObjectsCache[$type] = new $className(); |
|
78
|
4 |
|
} |
|
79
|
|
|
|
|
80
|
23 |
|
return self::$scalarObjectsCache[$type]; |
|
81
|
|
|
} else { |
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
25 |
|
public static function isScalarType($typeName) |
|
87
|
|
|
{ |
|
88
|
25 |
|
return in_array($typeName, self::getScalarTypes()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return AbstractType[] |
|
93
|
|
|
*/ |
|
94
|
28 |
|
public static function getScalarTypes() |
|
95
|
|
|
{ |
|
96
|
|
|
return [ |
|
97
|
28 |
|
self::TYPE_INT, |
|
98
|
28 |
|
self::TYPE_FLOAT, |
|
99
|
28 |
|
self::TYPE_STRING, |
|
100
|
28 |
|
self::TYPE_BOOLEAN, |
|
101
|
28 |
|
self::TYPE_ID, |
|
102
|
28 |
|
self::TYPE_DATETIME, |
|
103
|
28 |
|
self::TYPE_DATE, |
|
104
|
|
|
self::TYPE_TIMESTAMP |
|
105
|
28 |
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |