30
30
class RequestResolver
31
31
{
32
32
33
+ public const QUERY = 'Query ' ;
34
+ public const COLLECTION_QUERY = 'CollectionQuery ' ;
35
+ public const REQUEST = 'Request ' ;
36
+
33
37
/**
34
38
* @var array
35
39
*/
36
40
private static array $ custom = [];
37
41
38
42
/**
39
- * @var string
43
+ * @var array
40
44
*/
41
- private string $ type ;
45
+ private static array $ defaults = [
46
+ self ::QUERY => AnonymousQuery::class,
47
+ self ::COLLECTION_QUERY => AnonymousCollectionQuery::class,
48
+ ];
42
49
43
50
/**
44
- * Register a custom binding for a query.
45
- *
46
- * @param string $resourceType
47
- * @param string $class
48
- */
49
- public static function registerQuery (string $ resourceType , string $ class ): void
50
- {
51
- self ::register ('Query ' , $ resourceType , $ class );
52
- }
53
-
54
- /**
55
- * Register a custom binding for a collection query.
56
- *
57
- * @param string $resourceType
58
- * @param string $class
51
+ * @var string
59
52
*/
60
- public static function registerCollectionQuery (string $ resourceType , string $ class ): void
61
- {
62
- self ::register ('CollectionQuery ' , $ resourceType , $ class );
63
- }
53
+ private string $ type ;
64
54
65
55
/**
66
- * Register a custom binding for a resource request.
56
+ * Use the provided class as the default class for the specified request type .
67
57
*
68
- * @param string $resourceType
58
+ * @param string $type
69
59
* @param string $class
70
60
*/
71
- public static function registerRequest (string $ resourceType , string $ class ): void
61
+ public static function useDefault (string $ type , string $ class ): void
72
62
{
73
- self ::register ( ' Request ' , $ resourceType , $ class) ;
63
+ self ::$ defaults [ $ type ] = $ class ;
74
64
}
75
65
76
66
/**
@@ -80,7 +70,7 @@ public static function registerRequest(string $resourceType, string $class): voi
80
70
* @param string $resourceType
81
71
* @param string $class
82
72
*/
83
- private static function register (string $ type , string $ resourceType , string $ class ): void
73
+ public static function register (string $ type , string $ resourceType , string $ class ): void
84
74
{
85
75
self ::$ custom [$ type ] = self ::$ custom [$ type ] ?? [];
86
76
self ::$ custom [$ type ][$ resourceType ] = $ class ;
@@ -113,10 +103,8 @@ public function __invoke(string $resourceType, bool $allowNull = false): ?FormRe
113
103
if (!class_exists ($ fqn ) && !$ app ->bound ($ fqn )) {
114
104
if (true === $ allowNull ) {
115
105
return null ;
116
- } else if ('CollectionQuery ' === $ this ->type ) {
117
- $ fqn = AnonymousCollectionQuery::class;
118
- } else if ('Query ' === $ this ->type ) {
119
- $ fqn = AnonymousQuery::class;
106
+ } else if (isset (self ::$ defaults [$ this ->type ])) {
107
+ $ fqn = self ::$ defaults [$ this ->type ];
120
108
}
121
109
}
122
110
0 commit comments