Skip to content

Commit d69e68e

Browse files
committedOct 13, 2024
Adding function return type.
Updateing dictionary path. Checking dictionary status.
1 parent 1d2916c commit d69e68e

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed
 

‎Controllers/SearchController.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@
77
class SearchController
88
{
99

10-
function index($params)
10+
function index(array $params): void // Just echo something
1111
{
1212

13-
$data = json_decode(file_get_contents("/home/max/absent/api/dict.json"), true);
13+
$data = json_decode(file_get_contents(__DIR__."/../dict.json"), true);
14+
15+
if (json_last_error() === JSON_ERROR_NONE && is_array($data)){
16+
17+
$word = strtolower($params["word"]);
1418

15-
$word = strtolower($params["word"]);
19+
foreach ($data as $query => $translate){
1620

17-
$result = [];
21+
if ($query === $word){
22+
$result = ["status" => 200, "data" => ["word" => $word, "translate" => $translate]];
23+
break;
24+
}
25+
}
1826

19-
foreach ($data as $query => $translate){
27+
if (!$result) $result = ["status" => 504, "data" => ["word" => $word, "massage" => "Word not found"]];
2028

21-
if ($query == $word){
22-
$result = ["status" => 200, "data" => ["word" => $word, "translate" => $translate]];
23-
break;
24-
}
29+
} else{
30+
$result = ["status" => 501, "data" => ["massage" => "Internal server error"]];
2531
}
2632

27-
if (!$result) $result = ["status" => 404, "data" => ["massage" => "Word not found"]];
28-
29-
print_r(json_encode($result));
33+
echo json_encode($result);
3034
}
3135
}

0 commit comments

Comments
 (0)