MAPG-86 refactor view-controller
This commit is contained in:
1 parent
b0b244712f
commit
88fe7b7a03
7 files changed
+62
-17
No files matched your search
@@ -0,0 +1,17 @@
|
||||
<?php namespace MapGuesser\Response;
|
||||
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
|
||||
abstract class ContentBase implements IContent
|
||||
{
|
||||
protected array $data;
|
||||
|
||||
public function &getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
abstract public function &render(): string;
|
||||
|
||||
abstract public function getContentType(): string;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php namespace MapGuesser\Response;
|
||||
|
||||
class HtmlContent extends ContentBase
|
||||
{
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php namespace MapGuesser\Response;
|
||||
|
||||
class JsonContent extends ContentBase
|
||||
{
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php namespace MapGuesser\Response;
|
||||
|
||||
use MapGuesser\Interfaces\Response\IRedirect;
|
||||
|
||||
class Redirect implements IRedirect
|
||||
{
|
||||
private $target;
|
||||
|
||||
private int $type;
|
||||
|
||||
public function __construct($target, int $type = IRedirect::TEMPORARY)
|
||||
{
|
||||
$this->target = $target;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function getUrl(): string
|
||||
{
|
||||
if (is_array($this->target)) {
|
||||
$link = $this->target[0]->generateLink($this->target[1]);
|
||||
} else {
|
||||
$link = $this->target;
|
||||
}
|
||||
|
||||
return $link;
|
||||
}
|
||||
|
||||
public function getHttpCode(): int
|
||||
{
|
||||
switch ($this->type) {
|
||||
case IRedirect::PERMANENT:
|
||||
return 301;
|
||||
|
||||
case IRedirect::TEMPORARY:
|
||||
return 302;
|
||||
|
||||
default:
|
||||
return 302;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user