|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is a part of GraphQL project. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
|
6
|
|
|
* created: 5/11/16 10:19 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Type; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Type\Enum\AbstractEnumType; |
|
13
|
|
|
use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType; |
|
14
|
|
|
use Youshido\GraphQL\Type\ListType\AbstractListType; |
|
15
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
|
16
|
|
|
use Youshido\GraphQL\Type\Scalar\AbstractScalarType; |
|
17
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
|
18
|
|
|
|
|
19
|
|
|
class TypeService |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
const TYPE_CALLABLE = 'callable'; |
|
23
|
|
|
const TYPE_GRAPHQL_TYPE = 'graphql_type'; |
|
24
|
|
|
const TYPE_OBJECT_TYPE = 'object_type'; |
|
25
|
|
|
const TYPE_ARRAY_OF_OBJECT_TYPES = 'array_of_object_types'; |
|
26
|
|
|
const TYPE_OBJECT_INPUT_TYPE = 'object_input_type'; |
|
27
|
|
|
const TYPE_LIST = 'list'; |
|
28
|
|
|
const TYPE_BOOLEAN = TypeMap::TYPE_BOOLEAN; |
|
29
|
|
|
const TYPE_STRING = TypeMap::TYPE_STRING; |
|
30
|
|
|
const TYPE_ARRAY = 'array'; |
|
31
|
|
|
const TYPE_ARRAY_OF_FIELDS_CONFIG = 'array_of_fields'; |
|
32
|
|
|
const TYPE_ARRAY_OF_INPUT_FIELDS = 'array_of_inputs'; |
|
33
|
|
|
const TYPE_ENUM_VALUES = 'array_of_values'; |
|
34
|
|
|
const TYPE_ARRAY_OF_INTERFACES = 'array_of_interfaces'; |
|
35
|
|
|
const TYPE_ANY = 'any'; |
|
36
|
|
|
const TYPE_ANY_OBJECT = 'any_object'; |
|
37
|
|
|
const TYPE_ANY_INPUT = 'any_input'; |
|
38
|
|
|
|
|
39
|
2 |
|
public static function resolveNamedType($object) |
|
40
|
|
|
{ |
|
41
|
2 |
|
if (is_object($object)) { |
|
42
|
1 |
|
if ($object instanceof AbstractType) { |
|
43
|
1 |
|
return $object->getType(); |
|
44
|
|
|
} |
|
45
|
2 |
|
} elseif (is_null($object)) { |
|
46
|
1 |
|
return null; |
|
47
|
2 |
|
} elseif (is_scalar($object)) { |
|
48
|
1 |
|
return new StringType(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
throw new \Exception('Invalid type'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param AbstractType|mixed $type |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public static function isInterface($type) |
|
59
|
|
|
{ |
|
60
|
2 |
|
if (!is_object($type)) { |
|
61
|
1 |
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return $type->getKind() == TypeMap::KIND_INTERFACE; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param AbstractType|mixed $type |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public static function isAbstractType($type) |
|
72
|
|
|
{ |
|
73
|
3 |
|
if (!is_object($type)) { |
|
74
|
1 |
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
return in_array($type->getKind(), [TypeMap::KIND_INTERFACE, TypeMap::KIND_UNION]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
123 |
|
public static function isScalarType($type) |
|
81
|
|
|
{ |
|
82
|
123 |
|
if (is_object($type)) { |
|
83
|
107 |
|
return $type instanceof AbstractScalarType || $type instanceof AbstractEnumType; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
101 |
|
return in_array(strtolower($type), TypeFactory::getScalarTypesNames()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
91 |
|
public static function isGraphQLType($type) |
|
90
|
|
|
{ |
|
91
|
91 |
|
return $type instanceof AbstractType || TypeService::isScalarType($type); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public static function isLeafType($type) |
|
95
|
|
|
{ |
|
96
|
|
|
return $type instanceof AbstractEnumType || TypeService::isScalarType($type); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
8 |
|
public static function isObjectType($type) |
|
100
|
|
|
{ |
|
101
|
8 |
|
return $type instanceof AbstractObjectType; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param mixed|AbstractType $type |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public static function isInputType($type) |
|
109
|
|
|
{ |
|
110
|
6 |
|
if (is_object($type)) { |
|
111
|
4 |
|
$namedType = $type->getNullableType()->getNamedType(); |
|
112
|
|
|
|
|
113
|
4 |
|
return ($namedType instanceof AbstractScalarType) |
|
114
|
3 |
|
|| ($type instanceof AbstractListType) |
|
115
|
3 |
|
|| ($namedType instanceof AbstractInputObjectType) |
|
116
|
4 |
|
|| ($namedType instanceof AbstractEnumType); |
|
117
|
|
|
} else { |
|
118
|
3 |
|
return TypeService::isScalarType($type); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
public static function isInputObjectType($type) |
|
123
|
|
|
{ |
|
124
|
2 |
|
return $type instanceof AbstractInputObjectType; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
11 |
|
public static function getPropertyValue($data, $path) |
|
128
|
|
|
{ |
|
129
|
11 |
|
if (is_object($data)) { |
|
130
|
11 |
|
$getter = $path; |
|
131
|
11 |
|
if (substr($path, 0, 2) != 'is') { |
|
132
|
11 |
|
$getter = 'get' . self::classify($path); |
|
133
|
11 |
|
if (!is_callable([$data, $getter])) { |
|
134
|
1 |
|
$getter = 'is' . self::classify($path); |
|
135
|
1 |
|
} |
|
136
|
11 |
|
if (!is_callable([$data, $getter])) { |
|
137
|
1 |
|
$getter = self::classify($path); |
|
138
|
1 |
|
} |
|
139
|
11 |
|
} |
|
140
|
|
|
|
|
141
|
11 |
|
return is_callable([$data, $getter]) ? $data->$getter() : (isset($data->$path) ? $data->$path : null); |
|
142
|
1 |
|
} elseif (is_array($data)) { |
|
143
|
1 |
|
return array_key_exists($path, $data) ? $data[$path] : null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return null; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
11 |
|
protected static function classify($text) |
|
150
|
|
|
{ |
|
151
|
11 |
|
$text = explode(' ', str_replace(['_', '/', '-', '.'], ' ', $text)); |
|
152
|
11 |
|
$textLength = count($text); |
|
153
|
11 |
|
for ($i = 0; $i < $textLength; $i++) { |
|
154
|
11 |
|
$text[$i] = ucfirst($text[$i]); |
|
155
|
11 |
|
} |
|
156
|
11 |
|
$text = ucfirst(implode('', $text)); |
|
157
|
|
|
|
|
158
|
11 |
|
return $text; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|