/
ModelConfiguration.php
104 lines (90 loc) · 2.43 KB
/
ModelConfiguration.php
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
<?php
namespace SilverStripe\GraphQL\Config;
use SilverStripe\Core\ClassInfo;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Schema;
class ModelConfiguration extends AbstractConfiguration
{
/**
* @return callable|null
* @throws SchemaBuilderException
*/
public function getTypeFormatter(): ?callable
{
return $this->get('type_formatter', [ClassInfo::class, 'shortName']);
}
/**
* @return string
* @throws SchemaBuilderException
*/
public function getTypePrefix(): string
{
return $this->get('type_prefix', '');
}
/**
* @return array
* @throws SchemaBuilderException
*/
public function getNestedQueryPlugins(): array
{
return $this->get('nested_query_plugins', []);
}
/**
* @param string $operation
* @return array
* @throws SchemaBuilderException
*/
public function getOperationConfig(string $operation): array
{
return $this->get(['operations', $operation], []);
}
/**
* @param string $class
* @return string
* @throws SchemaBuilderException
*/
public function getTypeName(string $class): string
{
$mapping = $this->get('type_mapping', []);
$custom = $mapping[$class] ?? null;
if ($custom) {
return $custom;
}
$typeName = $this->formatClass($class);
$prefix = $this->getPrefix($class);
return $prefix . $typeName;
}
/**
* @param string $class
* @return string
* @throws SchemaBuilderException
*/
private function formatClass(string $class): string
{
$formatter = $this->getTypeFormatter();
Schema::invariant(
is_callable($formatter, false),
'type_formatter property for %s is not callable',
__CLASS__
);
return call_user_func_array($formatter, [$class]);
}
/**
* @param string $class
* @return string
* @throws SchemaBuilderException
*/
private function getPrefix(string $class): string
{
$prefix = $this->getTypePrefix();
if (is_callable($prefix, false)) {
return call_user_func_array($prefix, [$class]);
}
Schema::invariant(
is_string($prefix),
'type_prefix on %s must be a string',
__CLASS__
);
return $prefix;
}
}