@@ -105,17 +105,46 @@ public function scopeOfKey(Builder $query, string $key): Builder
105105
106106 if ($ compatibilityMode ) {
107107 return $ query ->where (function (Builder $ query ) use ($ key ) {
108- return $ query ->where ('key ' , $ key )
109- ->orWhere ('key ' , hash ('sha256 ' , $ key ));
108+ if ($ this ->isMissingId ($ key )) {
109+ return $ query ->where ('key ' , $ key )
110+ ->orWhere ('key ' , hash ('sha256 ' , $ key ));
111+ }
112+
113+ $ id = $ this ->extractId ($ key );
114+ $ key = $ this ->extractKey ($ key );
115+
116+ return $ query
117+ ->where (function (Builder $ query ) use ($ key , $ id ) {
118+ return $ query ->where ('key ' , $ key )
119+ ->where ('id ' , $ id );
120+ })
121+ ->orWhere (function (Builder $ query ) use ($ key , $ id ) {
122+ return $ query ->where ('key ' , hash ('sha256 ' , $ key ))
123+ ->where ('id ' , $ id );
124+ });
110125 });
111126 }
112127
113- if (strpos ($ key, ' | ' ) === false ) {
128+ if ($ this -> isMissingId ($ key) ) {
114129 return $ query ->where ('key ' , hash ('sha256 ' , $ key ));
115130 }
116131
117- [$ id , $ key ] = explode ('| ' , $ key , 2 );
132+ return $ query ->where ('id ' , $ this ->extractId ($ key ))
133+ ->where ('key ' , hash ('sha256 ' , $ this ->extractKey ($ key )));
134+ }
135+
136+ private function isMissingId (string $ key ): bool
137+ {
138+ return strpos ($ key , '| ' ) === false ;
139+ }
118140
119- return $ query ->where ('id ' , $ id )->where ('key ' , hash ('sha256 ' , $ key ));
141+ private function extractId (string $ key ): string
142+ {
143+ return explode ('| ' , $ key , 2 )[0 ];
144+ }
145+
146+ private function extractKey (string $ key ): string
147+ {
148+ return explode ('| ' , $ key , 2 )[1 ];
120149 }
121150}
0 commit comments