From d9395a0eb22f6a643b5b24e7b7c8c14a36c0de9f Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sun, 26 Aug 2018 11:11:48 +0200 Subject: [PATCH] Reorganized poll formatting. --- index.php | 30 ++++++++---------------------- src/Format.php | 14 ++++++++++++++ {models => src/models}/Poll.php | 4 ++-- 3 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 src/Format.php rename {models => src/models}/Poll.php (97%) diff --git a/index.php b/index.php index 6edc20f..4b06053 100644 --- a/index.php +++ b/index.php @@ -1,23 +1,9 @@ $poll->id, - "title" => $poll->title, - "creation_date" => $poll->creation_date, - "options" => $poll->options, - ]; - - if ($with_delete_token === true) - $array['delete_token'] = $poll->delete_token; - - return $array; -} - Flight::route("POST /polls", function () { $request = Flight::request(); if ($request->type === "application/json") @@ -25,7 +11,7 @@ Flight::route("POST /polls", function () { $request_json = $request->data; $poll = Poll::create_poll($request_json); if ($poll) - Flight::json(format_poll($poll, true), 201); + Flight::json(array_merge(Format::poll($poll), ["delete_token" => $poll->delete_token]), 201); else Flight::halt(403, "

403 Forbidden

Invalid data.

"); } @@ -38,7 +24,7 @@ Flight::route("GET /polls/@id:[a-fA-F0-9]+", function ($id) { if ($poll) { if (Flight::request()->type === "application/json") - Flight::json(format_poll($poll)); + Flight::json(Format::poll($poll)); else { // If unique_ip option is enabled => Only allow unregistered IPs. @@ -69,7 +55,7 @@ Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) { { // Then save and show poll data. $poll->save(); - Flight::json(format_poll($poll)); + Flight::json(Format::poll($poll)); } else Flight::halt(403, "

403 Forbidden

Too many votes for this IP address or too many options selected.

"); @@ -110,7 +96,7 @@ Flight::route("GET /polls/@id:[a-fA-F0-9]+/results", function ($id) { if ($poll) { if (Flight::request()->type === "application/json") - Flight::json(format_poll($poll)); //TODO Add a svg for results? + Flight::json(Format::poll($poll)); //TODO Add a svg for results? else { Flight::render("svg/results", ["poll" => $poll, "colors" => $VERLAINE["chart_colors"]], "results_chart"); @@ -130,7 +116,7 @@ Flight::route("GET|DELETE /polls/@id:[a-fA-F0-9]+/@token:[a-fA-F0-9]+", function { $poll->delete(); if (Flight::request()->type === "application/json") - Flight::json(format_poll($poll), 204); + Flight::json(Format::poll($poll), 204); else Flight::redirect('/', 308); } @@ -139,7 +125,7 @@ Flight::route("GET|DELETE /polls/@id:[a-fA-F0-9]+/@token:[a-fA-F0-9]+", function if (Flight::request()->type === "application/json") Flight::halt(401, "

401 Unauthorized

Invalid token.

"); else - Flight::redirect('/', 401); + Flight::redirect('/'); } } else diff --git a/src/Format.php b/src/Format.php new file mode 100644 index 0000000..55d64e9 --- /dev/null +++ b/src/Format.php @@ -0,0 +1,14 @@ + $poll->id, + "title" => $poll->title, + "creation_date" => $poll->creation_date, + "options" => $poll->options, + ]; + } +} \ No newline at end of file diff --git a/models/Poll.php b/src/models/Poll.php similarity index 97% rename from models/Poll.php rename to src/models/Poll.php index 201e401..f3b623d 100644 --- a/models/Poll.php +++ b/src/models/Poll.php @@ -1,8 +1,8 @@ true, "multiple_choices" => false]);