1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Utility; |
4
|
|
|
|
5
|
|
|
class StringHelper { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Turn a list of machine names into a camel-cased string. |
9
|
|
|
* |
10
|
|
|
* @return string |
11
|
|
|
* A camel-cased concatenation of the input components. |
12
|
|
|
* |
13
|
|
|
* @throws \InvalidArgumentException |
14
|
|
|
* If the provided input does can't be converted to a specification compliant |
15
|
|
|
* string representation for field or type names. |
16
|
|
|
*/ |
17
|
|
|
public static function camelCase() { |
18
|
|
|
$args = func_get_args(); |
19
|
|
|
$components = array_map(function ($component) { |
20
|
|
|
return preg_replace('[^a-zA-Z0-9_]', '_', $component); |
21
|
|
|
}, $args); |
22
|
|
|
|
23
|
|
|
$components = array_filter(explode('_', implode('_', $components))); |
24
|
|
|
if (!count($components)) { |
25
|
|
|
throw new \InvalidArgumentException(sprintf("Failed to create a specification compliant string representation for '%s'.", implode('', $args))); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$string = implode('', array_map('ucfirst', $components)); |
29
|
|
|
$string = $string && is_numeric($string[0]) ? "_$string" : $string; |
30
|
|
|
return $string; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Turn a list of machine names into a upper-cased string. |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
* A upper-cased concatenation of the input components. |
38
|
|
|
*/ |
39
|
|
|
public static function upperCase() { |
40
|
|
|
$result = call_user_func_array([static::class, 'camelCase'], func_get_args()); |
41
|
|
|
return strtoupper($result); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Turn a list of machine names into a property-cased string. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
* A camel-cased concatenation of the input components. |
49
|
|
|
*/ |
50
|
|
|
public static function propCase() { |
51
|
|
|
$result = call_user_func_array([static::class, 'camelCase'], func_get_args()); |
52
|
|
|
return ctype_upper($result) ? strtolower($result) : lcfirst($result); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Wraps a type string in brackets declaring it as a list. |
57
|
|
|
* |
58
|
|
|
* @param string $type |
59
|
|
|
* The type to declare as a list. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
* The decorated type string. |
63
|
|
|
*/ |
64
|
|
|
public static function listType($type) { |
65
|
|
|
return "[$type]"; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Appends an exclamation mark to a type string declaring it as non-null. |
70
|
|
|
* |
71
|
|
|
* @param string $type |
72
|
|
|
* The type to declare as non-null. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
* The decorated type string. |
76
|
|
|
*/ |
77
|
|
|
public static function nonNullType($type) { |
78
|
|
|
return "$type!"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Decorates a type as non-null and/or as a list. |
83
|
|
|
* |
84
|
|
|
* @param string $type |
85
|
|
|
* The type to declare as non-null. |
86
|
|
|
* @param bool $list |
87
|
|
|
* Whether to mark the type as a list. |
88
|
|
|
* @param bool $required |
89
|
|
|
* Whether to mark the type as required. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
* The decorated type. |
93
|
|
|
*/ |
94
|
|
|
public static function decorateType($type, $list = FALSE, $required = FALSE) { |
95
|
|
|
if (!empty($list)) { |
96
|
|
|
$type = static::listType($type); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!empty($required)) { |
100
|
|
|
$type = static::nonNullType($type); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $type; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Parses a type definition from a string and properly decorates it. |
108
|
|
|
* |
109
|
|
|
* Converts type strings (e.g. [Foo!]) to their object representations. |
110
|
|
|
* |
111
|
|
|
* @param string $type |
112
|
|
|
* The type string to parse. |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
* The extracted type with the type decorators. |
116
|
|
|
*/ |
117
|
|
|
public static function parseType($type) { |
118
|
|
|
$decorators = []; |
119
|
|
|
$unwrapped = $type; |
120
|
|
|
$matches = NULL; |
121
|
|
|
|
122
|
|
|
while (preg_match('/[\[\]!]/', $unwrapped) && preg_match_all('/^(\[?)(.*?)(\]?)(!*?)$/', $unwrapped, $matches)) { |
123
|
|
|
if ($unwrapped === $matches[2][0] || empty($matches[1][0]) !== empty($matches[3][0])) { |
124
|
|
|
throw new \InvalidArgumentException(sprintf("Invalid type declaration '%s'.", $type)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!empty($matches[4][0])) { |
128
|
|
|
array_unshift($decorators, ['GraphQL\Type\Definition\Type', 'nonNull']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!empty($matches[1][0]) && !empty($matches[3][0])) { |
132
|
|
|
array_unshift($decorators, ['GraphQL\Type\Definition\Type', 'listOf']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$unwrapped = $matches[2][0]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return [$unwrapped, $decorators]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |