From 037d19b0b3941c18356c18c179f61662073571c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 19 May 2020 03:13:55 +0200 Subject: [PATCH] MAPG-10 refactor view-controller to be able to handle JSON response --- src/Controller/BaseController.php | 24 --------------------- src/Controller/ControllerInterface.php | 8 +++++++ src/View/HtmlView.php | 29 ++++++++++++++++++++++++++ src/View/JsonView.php | 21 +++++++++++++++++++ src/View/ViewBase.php | 15 +++++++++++++ 5 files changed, 73 insertions(+), 24 deletions(-) delete mode 100644 src/Controller/BaseController.php create mode 100644 src/Controller/ControllerInterface.php create mode 100644 src/View/HtmlView.php create mode 100644 src/View/JsonView.php create mode 100644 src/View/ViewBase.php diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php deleted file mode 100644 index 9e2c2a7..0000000 --- a/src/Controller/BaseController.php +++ /dev/null @@ -1,24 +0,0 @@ -operate(); - - extract($this->variables); - - ob_start(); - require ROOT . '/views/' . $this->view . '.php'; - $content = ob_get_contents(); - ob_end_clean(); - - return $content; - } - - abstract protected function operate() : void; -} diff --git a/src/Controller/ControllerInterface.php b/src/Controller/ControllerInterface.php new file mode 100644 index 0000000..22d2f8f --- /dev/null +++ b/src/Controller/ControllerInterface.php @@ -0,0 +1,8 @@ +template = $template; + $this->data = &$data; + } + + public function &render(): string + { + extract($this->data); + + ob_start(); + require ROOT . '/views/' . $this->template . '.php'; + $content = ob_get_contents(); + ob_end_clean(); + + return $content; + } + + public function getContentType(): string + { + return 'text/html'; + } +} diff --git a/src/View/JsonView.php b/src/View/JsonView.php new file mode 100644 index 0000000..78eb90f --- /dev/null +++ b/src/View/JsonView.php @@ -0,0 +1,21 @@ +data = &$data; + } + + public function &render(): string + { + $content = json_encode($this->data); + + return $content; + } + + public function getContentType(): string + { + return 'application/json'; + } +} diff --git a/src/View/ViewBase.php b/src/View/ViewBase.php new file mode 100644 index 0000000..9bad81f --- /dev/null +++ b/src/View/ViewBase.php @@ -0,0 +1,15 @@ +data; + } + + abstract public function &render(): string; + + abstract public function getContentType(): string; +}