3030class RequestResolver
3131{
3232
33+ public const QUERY = 'Query ' ;
34+ public const COLLECTION_QUERY = 'CollectionQuery ' ;
35+ public const REQUEST = 'Request ' ;
36+
3337 /**
3438 * @var array
3539 */
3640 private static array $ custom = [];
3741
3842 /**
39- * @var string
43+ * @var array
4044 */
41- private string $ type ;
45+ private static array $ defaults = [
46+ self ::QUERY => AnonymousQuery::class,
47+ self ::COLLECTION_QUERY => AnonymousCollectionQuery::class,
48+ ];
4249
4350 /**
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
5952 */
60- public static function registerCollectionQuery (string $ resourceType , string $ class ): void
61- {
62- self ::register ('CollectionQuery ' , $ resourceType , $ class );
63- }
53+ private string $ type ;
6454
6555 /**
66- * Register a custom binding for a resource request.
56+ * Use the provided class as the default class for the specified request type .
6757 *
68- * @param string $resourceType
58+ * @param string $type
6959 * @param string $class
7060 */
71- public static function registerRequest (string $ resourceType , string $ class ): void
61+ public static function useDefault (string $ type , string $ class ): void
7262 {
73- self ::register ( ' Request ' , $ resourceType , $ class) ;
63+ self ::$ defaults [ $ type ] = $ class ;
7464 }
7565
7666 /**
@@ -80,7 +70,7 @@ public static function registerRequest(string $resourceType, string $class): voi
8070 * @param string $resourceType
8171 * @param string $class
8272 */
83- private static function register (string $ type , string $ resourceType , string $ class ): void
73+ public static function register (string $ type , string $ resourceType , string $ class ): void
8474 {
8575 self ::$ custom [$ type ] = self ::$ custom [$ type ] ?? [];
8676 self ::$ custom [$ type ][$ resourceType ] = $ class ;
@@ -113,10 +103,8 @@ public function __invoke(string $resourceType, bool $allowNull = false): ?FormRe
113103 if (!class_exists ($ fqn ) && !$ app ->bound ($ fqn )) {
114104 if (true === $ allowNull ) {
115105 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 ];
120108 }
121109 }
122110
0 commit comments