@@ -169,10 +169,8 @@ protected function post(ServerRequestInterface|string $request, int|string &$res
169169
170170 switch ($ methodType ) {
171171 case 'delete ' :
172- $ existingIndex = array_search ($ ckey , array_column ($ list , 'ss13 ' ));
173- if ($ existingIndex === false ) $ existingIndex = array_search ($ discord , array_column ($ list , 'discord ' ));
174172 $ this ->delete (
175- $ existingIndex ,
173+ $ this -> getIndex ( $ ckey , $ discord ) ,
176174 $ list ,
177175 $ response ,
178176 $ headers ,
@@ -218,13 +216,8 @@ protected function __post(array &$list, string $ckey, string $discord, int|strin
218216 return ;
219217 }
220218
221- $ list [] = [
222- 'ss13 ' => $ ckey ,
223- 'discord ' => $ discord ,
224- 'create_time ' => date ('Y-m-d H:i:s ' )
225- ];
226- $ this ->state ::writeJson ($ this ->state ->getJsonPath (), $ list );
227- $ this ->state ->setVerifyList ($ list );
219+ $ this ->add ($ ckey , $ discord );
220+
228221 $ headers = ['Content-Type ' => 'application/json ' ];
229222 $ headers ['Content-Length ' ] = ($ body = $ this ->__content ())
230223 ? strlen ($ body )
@@ -248,13 +241,49 @@ protected function delete(int|string|false $existingIndex, array &$list, int|str
248241 $ body = 'Not Found ' ;
249242 return ;
250243 }
251- $ splice = array_splice ( $ list , $ existingIndex , 1 );
252- $ this -> state :: writeJson ( $ this ->state -> getJsonPath (), $ list );
253- $ this -> state -> setVerifyList ( $ list );
244+
245+ $ splice = $ this ->removeIndex ( $ existingIndex );
246+
254247 $ response = Response::STATUS_OK ;
255248 $ headers = ['Content-Type ' => 'application/json ' ];
256249 $ headers ['Content-Length ' ] = ($ content = json_encode ($ splice ))
257250 ? strlen ($ body = $ content )
258251 : 0 ;
259252 }
253+
254+ public function add (string $ ckey , string $ discord ): void
255+ {
256+ $ list = &$ this ->state ->getVerifyList ();
257+ $ list [] = [
258+ 'ss13 ' => $ ckey ,
259+ 'discord ' => $ discord ,
260+ 'create_time ' => date ('Y-m-d H:i:s ' )
261+ ];
262+ $ this ->state ::writeJson ($ this ->state ->getJsonPath (), $ list );
263+ $ this ->state ->setVerifyList ($ list );
264+ }
265+
266+ public function remove (string $ ckey , string $ discord ): ?array
267+ {
268+ $ existingIndex = $ this ->getIndex ($ ckey , $ discord );
269+ if ($ existingIndex === false ) return null ;
270+ return $ this ->removeIndex ($ existingIndex );
271+ }
272+
273+ public function getIndex (string $ ckey , string $ discord ): int |string |false
274+ {
275+ $ list = &$ this ->state ->getVerifyList ();
276+ $ existingIndex = array_search ($ ckey , array_column ($ list , 'ss13 ' ));
277+ if ($ existingIndex === false ) $ existingIndex = array_search ($ discord , array_column ($ list , 'discord ' ));
278+ return $ existingIndex ;
279+ }
280+
281+ public function removeIndex (int |string $ existingIndex ): array
282+ {
283+ $ list = &$ this ->state ->getVerifyList ();
284+ $ splice = array_splice ($ list , (int )$ existingIndex , 1 );
285+ $ this ->state ::writeJson ($ this ->state ->getJsonPath (), $ list );
286+ $ this ->state ->setVerifyList ($ list );
287+ return $ splice ;
288+ }
260289}
0 commit comments