1111use TiMacDonald \JsonApi \Exceptions \ResourceIdentificationException ;
1212use TiMacDonald \JsonApi \ResourceIdentifier ;
1313
14+ /**
15+ * @internal
16+ */
1417trait Identification
1518{
1619 private const ID_RESOLVER_KEY = self ::class.':$idResolver ' ;
@@ -22,28 +25,11 @@ trait Identification
2225 private string |null $ typeCache = null ;
2326
2427 /**
25- * @internal
26- *
2728 * @var array<int, (callable(ResourceIdentifier): void)>
2829 */
2930 private array $ resourceIdentifierCallbacks = [];
3031
3132 /**
32- * @api
33- *
34- * @param (callable(ResourceIdentifier): void) $callback
35- * @return $this
36- */
37- public function withResourceIdentifier (callable $ callback )
38- {
39- $ this ->resourceIdentifierCallbacks [] = $ callback ;
40-
41- return $ this ;
42- }
43-
44- /**
45- * @api
46- *
4733 * @param (callable(mixed): string) $callback
4834 * @return void
4935 */
@@ -53,8 +39,6 @@ public static function resolveIdUsing(callable $callback)
5339 }
5440
5541 /**
56- * @api
57- *
5842 * @param (callable(mixed): string) $callback
5943 * @return void
6044 */
@@ -64,44 +48,12 @@ public static function resolveTypeUsing(callable $callback)
6448 }
6549
6650 /**
67- * @internal
68- *
69- * @return string
70- */
71- public function toUniqueResourceIdentifier (Request $ request )
72- {
73- return "type: {$ this ->resolveType ($ request )};id: {$ this ->resolveId ($ request )}; " ;
74- }
75-
76- /**
77- * @internal
78- *
79- * @return string
80- */
81- private function resolveId (Request $ request )
82- {
83- return $ this ->idCache ??= $ this ->toId ($ request );
84- }
85-
86- /**
87- * @internal
88- *
89- * @return string
90- */
91- private function resolveType (Request $ request )
92- {
93- return $ this ->typeCache ??= $ this ->toType ($ request );
94- }
95-
96- /**
97- * @internal
98- *
9951 * @return (callable(mixed, Request): string)
10052 */
10153 private static function idResolver ()
10254 {
10355 if (! App::bound (self ::ID_RESOLVER_KEY )) {
104- App::instance (self ::ID_RESOLVER_KEY , function (mixed $ resource , Request $ request ): string {
56+ return App::instance (self ::ID_RESOLVER_KEY , function (mixed $ resource , Request $ request ): string {
10557 if (! $ resource instanceof Model) {
10658 throw ResourceIdentificationException::attemptingToDetermineIdFor ($ resource );
10759 }
@@ -117,14 +69,12 @@ private static function idResolver()
11769 }
11870
11971 /**
120- * @internal
121- *
12272 * @return (callable(mixed, Request): string)
12373 */
12474 private static function typeResolver ()
12575 {
12676 if (! App::bound (self ::TYPE_RESOLVER_KEY )) {
127- App::instance (self ::TYPE_RESOLVER_KEY , function (mixed $ resource , Request $ request ): string {
77+ return App::instance (self ::TYPE_RESOLVER_KEY , function (mixed $ resource , Request $ request ): string {
12878 if (! $ resource instanceof Model) {
12979 throw ResourceIdentificationException::attemptingToDetermineTypeFor ($ resource );
13080 }
@@ -136,17 +86,56 @@ private static function typeResolver()
13686 return App::make (self ::TYPE_RESOLVER_KEY );
13787 }
13888
89+ /**
90+ * @param (callable(ResourceIdentifier): void) $callback
91+ * @return $this
92+ */
93+ public function pipeResourceIdentifier (callable $ callback )
94+ {
95+ $ this ->resourceIdentifierCallbacks [] = $ callback ;
96+
97+ return $ this ;
98+ }
99+
139100 /**
140101 * @internal
141102 *
142103 * @return ResourceIdentifier
143104 */
144105 public function resolveResourceIdentifier (Request $ request )
145106 {
146- return tap ($ this ->toResourceIdentifier ($ request ), function (ResourceIdentifier $ identifier ): void {
107+ return with ($ this ->toResourceIdentifier ($ request ), function (ResourceIdentifier $ identifier ): ResourceIdentifier {
147108 foreach ($ this ->resourceIdentifierCallbacks as $ callback ) {
148- $ callback ($ identifier );
109+ $ identifier = $ callback ($ identifier );
149110 }
111+
112+ return $ identifier ;
150113 });
151114 }
115+
116+ /**
117+ * @internal
118+ *
119+ * @return array{0: string, 1: string}
120+ */
121+ public function uniqueKey (Request $ request )
122+ {
123+ return [$ this ->resolveType ($ request ), $ this ->resolveId ($ request )];
124+ }
125+
126+ /**
127+ * @return string
128+ */
129+ private function resolveId (Request $ request )
130+ {
131+ return $ this ->idCache ??= $ this ->toId ($ request );
132+ }
133+
134+ /**
135+ * @return string
136+ */
137+ private function resolveType (Request $ request )
138+ {
139+ return $ this ->typeCache ??= $ this ->toType ($ request );
140+ }
152141}
0 commit comments