Completed
Push — master ( 78a448...1049ef )
by Alexandr
03:53
created

TypeService::isInterface()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 4
cts 4
cp 1
cc 2
eloc 4
nc 2
nop 1
crap 2
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 30
    public static function isInterface($type)
59
    {
60 30
        if (!is_object($type)) {
61 1
            return false;
62
        }
63
64 29
        return $type->getKind() == TypeMap::KIND_INTERFACE;
65
    }
66
67
    /**
68
     * @param AbstractType|mixed $type
69
     * @return bool
70
     */
71 22
    public static function isAbstractType($type)
72
    {
73 22
        if (!is_object($type)) {
74 1
            return false;
75
        }
76
77 22
        return in_array($type->getKind(), [TypeMap::KIND_INTERFACE, TypeMap::KIND_UNION]);
78
    }
79
80 92
    public static function isScalarType($type)
81
    {
82 92
        if (is_object($type)) {
83 59
            return $type instanceof AbstractScalarType;
84
        }
85
86 92
        return in_array(strtolower($type), TypeFactory::getScalarTypesNames());
87
    }
88
89 62
    public static function isGraphQLType($type)
90
    {
91 62
        return $type instanceof AbstractType || TypeService::isScalarType($type);
92
    }
93
94 38
    public static function isObjectType($type)
95
    {
96 38
        return $type instanceof AbstractObjectType;
97
    }
98
99
    /**
100
     * @param mixed|AbstractType $type
101
     * @return bool
102
     */
103 38
    public static function isInputType($type)
104
    {
105 38
        if (is_object($type)) {
106 36
            $namedType = $type->getNullableType()->getNamedType();
107
108 36
            return ($namedType instanceof AbstractScalarType)
109 15
                   || ($type instanceof AbstractListType)
110 15
                   || ($namedType instanceof AbstractInputObjectType)
111 36
                   || ($namedType instanceof AbstractEnumType);
112
        } else {
113 3
            return TypeService::isScalarType($type);
114
        }
115
    }
116
117 2
    public static function isInputObjectType($type)
118
    {
119 2
        return $type instanceof AbstractInputObjectType;
120
    }
121
122 9
    public static function getPropertyValue($data, $path)
123
    {
124 9
        if (is_object($data)) {
125 6
            $getter = $path;
126 6
            if (substr($path, 0, 2) != 'is') {
127 6
                $getter = 'get' . self::classify($path);
128 6
            }
129
130 6
            return is_callable([$data, $getter]) ? $data->$getter() : null;
131 9
        } elseif (is_array($data)) {
132 1
            return array_key_exists($path, $data) ? $data[$path] : null;
133
        }
134
135 8
        return null;
136
    }
137
138 6
    protected static function classify($text)
139
    {
140 6
        $text       = explode(' ', str_replace(['_', '/', '-', '.'], ' ', $text));
141 6
        $textLength = count($text);
142 6
        for ($i = 0; $i < $textLength; $i++) {
143 6
            $text[$i] = ucfirst($text[$i]);
144 6
        }
145 6
        $text = ucfirst(implode('', $text));
146
147 6
        return $text;
148
    }
149
}
150