-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathNamingStrategy.php
More file actions
123 lines (106 loc) · 3.74 KB
/
Copy pathNamingStrategy.php
File metadata and controls
123 lines (106 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite;
use TheCodingMachine\GraphQLite\Annotations\Factory;
use TheCodingMachine\GraphQLite\Annotations\Input;
use TheCodingMachine\GraphQLite\Annotations\TypeInterface;
use function implode;
use function lcfirst;
use function str_ends_with;
use function str_replace;
use function str_starts_with;
use function strlen;
use function strrpos;
use function substr;
class NamingStrategy implements NamingStrategyInterface
{
/**
* Returns the name of the GraphQL interface from a name of a concrete class (when the interface is created
* automatically to manage inheritance)
*/
public function getInterfaceNameFromConcreteName(string $concreteType): string
{
return $concreteType . 'Interface';
}
/**
* Returns the name of the GraphQL object from a name of GraphQL interface type (when the object is created
* automatically from a "Type" annotated interface)
*/
public function getConcreteNameFromInterfaceName(string $name): string
{
return str_replace('Interface', '', $name) . 'Impl';
}
/**
* Returns the GraphQL output object type name based on the type className and the Type annotation.
*/
public function getOutputTypeName(string $typeClassName, TypeInterface $type): string
{
$name = $type->getName();
if ($name !== null) {
return $name;
}
$prevPos = strrpos($typeClassName, '\\');
if ($prevPos) {
$typeClassName = substr($typeClassName, $prevPos + 1);
}
// By default, if the class name ends with Type, let's take the name of the class for the type
if (! $type->isSelfType() && str_ends_with($typeClassName, 'Type')) {
return substr($typeClassName, 0, -4);
}
// Else, let's take the name of the targeted class
$typeClassName = $type->getClass();
$prevPos = strrpos($typeClassName, '\\');
if ($prevPos) {
$typeClassName = substr($typeClassName, $prevPos + 1);
}
return $typeClassName;
}
public function getInputTypeName(string $className, Input|Factory $input): string
{
$inputTypeName = $input->getName();
if ($inputTypeName !== null) {
return $inputTypeName;
}
$prevPos = strrpos($className, '\\');
if ($prevPos) {
$className = substr($className, $prevPos + 1);
}
if (str_ends_with($className, 'Input')) {
return $className;
}
return $className . 'Input';
}
/**
* Returns the name of a GraphQL field from the name of the annotated method.
*/
public function getFieldNameFromMethodName(string $methodName): string
{
// Let's remove any "get" or "is".
if (str_starts_with($methodName, 'get') && strlen($methodName) > 3) {
return lcfirst(substr($methodName, 3));
}
if (str_starts_with($methodName, 'is') && strlen($methodName) > 2) {
return lcfirst(substr($methodName, 2));
}
return $methodName;
}
/**
* Returns the name of a GraphQL input field from the name of the annotated method.
*/
public function getInputFieldNameFromMethodName(string $methodName): string
{
if (str_starts_with($methodName, 'set') && strlen($methodName) > 3) {
return lcfirst(substr($methodName, 3));
}
return $methodName;
}
/**
* Returns the name of a GraphQL union type based on the included types.
*
* @param string[] $typeNames The list of GraphQL type names
*/
public function getUnionTypeName(array $typeNames): string
{
return 'Union' . implode('', $typeNames);
}
}