Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

TypeUtil::isTypeNonNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Util;
12
13
use GraphQL\Type\Definition\Type;
14
15
/**
16
 * Util to work with GraphQL types
17
 */
18
final class TypeUtil
19
{
20
    /**
21
     * @param string $type
22
     *
23
     * @return bool
24
     */
25 1
    public static function isTypeList($type): bool
26
    {
27 1
        return (bool) preg_match('/^\[([\\\\\w]+)!?\]!?$/', $type);
28
    }
29
30
    /**
31
     * @param string $type
32
     *
33
     * @return bool
34
     */
35 1
    public static function isTypeNonNullList($type): bool
36
    {
37 1
        return (bool) preg_match('/^\[([\\\\\w]+)!\]!?$/', $type);
38
    }
39
40
    /**
41
     * @param string $type
42
     *
43
     * @return bool
44
     */
45 1
    public static function isTypeNonNull($type): bool
46
    {
47 1
        return (bool) preg_match('/!$/', $type);
48
    }
49
50
    /**
51
     * @param string $type
52
     *
53
     * @return string
54
     */
55 1
    public static function normalize($type)
56
    {
57 1
        if (preg_match('/^\[?([\\\\\w]+)!?\]?!?$/', $type, $matches)) {
58 1
            $type = $matches[1];
59
        }
60
61
        switch ($type) {
62 1
            case 'bool':
63 1
            case 'boolean':
64 1
                $type = Type::BOOLEAN;
65 1
                break;
66 1
            case 'decimal':
67 1
            case 'float':
68
                $type = Type::FLOAT;
69
                break;
70 1
            case 'int':
71 1
            case 'integer':
72 1
                $type = Type::INT;
73 1
                break;
74 1
            case 'string':
75 1
                $type = Type::STRING;
76 1
                break;
77 1
            case 'id':
78
                $type = Type::ID;
79
                break;
80 1
            case 'datetime':
81 1
            case 'date_time':
82 1
            case 'date':
83 1
                $type = 'DateTime';
84 1
                break;
85
        }
86
87 1
        return $type;
88
    }
89
}
90