MAPG-10 refactor view-controller to be able to handle JSON response

This commit is contained in:
bence committed 2020-05-19 03:13:55 +02:00
1 parent 55b693df68
commit 037d19b0b3
5 files changed
+73 -24

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
<?php namespace MapGuesser\View;
class HtmlView extends ViewBase
{
private string $template;
public function __construct(string $template, array &$data = [])
{
$this->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';
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php namespace MapGuesser\View;
class JsonView extends ViewBase
{
public function __construct(array &$data = [])
{
$this->data = &$data;
}
public function &render(): string
{
$content = json_encode($this->data);
return $content;
}
public function getContentType(): string
{
return 'application/json';
}
}
+15
View File
@@ -0,0 +1,15 @@
<?php namespace MapGuesser\View;
abstract class ViewBase
{
protected array $data;
public function &getData(): array
{
return $this->data;
}
abstract public function &render(): string;
abstract public function getContentType(): string;
}