Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

TypeService   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 4 Features 1
Metric Value
wmc 27
c 14
b 4
f 1
lcom 0
cbo 3
dl 0
loc 130
rs 10
ccs 52
cts 52
cp 1

10 Methods

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