blob: e58ca519e8cb717c3af08caf3ebb276c2e44d2fe [file] [log] [blame]
avm9996399bb77c2020-01-27 03:15:08 +01001<?php
2class api {
3 private function writeJSON($array) {
4 echo json_encode($array);
5 }
6
7 private function error($error = null) {
8 self::writeJSON(["status" => "error", "error" => $error]);
9 exit();
10 }
11
12 private function returnData($data) {
13 self::writeJSON(["status" => "ok", "data" => $data]);
14 exit();
15 }
16
17 public static function handleRequest() {
18 global $_GET, $_POST;
19
20 if (!isset($_GET["action"])) self::error("actionNotProvided");
21
22 switch ($_GET["action"]) {
23 case "linies":
24 $liniesFull = tmbApi::request("transit/linies/metro");
25 if ($liniesFull === false || !isset($liniesFull["features"])) self::error("unexpected");
26
27 $linies = [];
28 foreach ($liniesFull["features"] as $linia) {
29 if (!isset($linia["properties"])) self::error("unexpected");
30 $linies[] = [
31 "id" => $linia["properties"]["ID_LINIA"] ?? null,
32 "nom" => $linia["properties"]["NOM_LINIA"] ?? "",
33 "desc" => $linia["properties"]["DESC_LINIA"] ?? "",
34 "color" => $linia["properties"]["COLOR_LINIA"] ?? "000",
35 "colorText" => $linia["properties"]["COLOR_TEXT_LINIA"] ?? "fff",
36 "ordre" => $linia["properties"]["ORDRE_LINIA"] ?? 0
37 ];
38 }
39
40 usort($linies, function ($a, $b) {
41 return $a["ordre"] - $b["ordre"];
42 });
43
44 self::returnData($linies);
45 break;
46
47 case "estacions":
48 if (!isset($_GET["linia"])) self::error("missingArguments");
49 $linia = (int)$_GET["linia"];
50 $estacionsFull = tmbApi::request("transit/linies/metro/".$linia."/estacions");
51 if ($estacionsFull === false || !isset($estacionsFull["features"])) self::error("unexpected");
52
53 $estacions = [];
54 foreach ($estacionsFull["features"] as $estacio) {
55 if (!isset($estacio["properties"])) self::error("unexpected");
56 $estacions[] = [
57 "id" => $estacio["properties"]["CODI_ESTACIO"] ?? null,
58 "nom" => $estacio["properties"]["NOM_ESTACIO"] ?? "",
59 "color" => $estacio["properties"]["COLOR_LINIA"] ?? "000",
60 "ordre" => $estacio["properties"]["ORDRE_ESTACIO"] ?? 0
61 ];
62 }
63
64 usort($estacions, function ($a, $b) {
65 return $a["ordre"] - $b["ordre"];
66 });
67
68 self::returnData($estacions);
69 break;
70
71 default:
72 self::error("actionNotImplemented");
73 }
74 }
75}