MAPG-86 refactor view-controller

This commit is contained in:
Bence Pőcze 2020-05-31 20:41:16 +02:00
parent b0b244712f
commit 88fe7b7a03
7 changed files with 62 additions and 17 deletions

View File

@ -1,8 +0,0 @@
<?php namespace MapGuesser\Interfaces\Controller;
use MapGuesser\Interfaces\View\IView;
interface IController
{
public function run(): IView;
}

View File

@ -1,6 +1,6 @@
<?php namespace MapGuesser\Interfaces\View;
<?php namespace MapGuesser\Interfaces\Response;
interface IView
interface IContent
{
public function &getData(): array;

View File

@ -0,0 +1,12 @@
<?php namespace MapGuesser\Interfaces\Response;
interface IRedirect
{
const PERMANENT = 1;
const TEMPORARY = 2;
public function getUrl(): string;
public function getHttpCode(): int;
}

View File

@ -1,8 +1,8 @@
<?php namespace MapGuesser\View;
<?php namespace MapGuesser\Response;
use MapGuesser\Interfaces\View\IView;
use MapGuesser\Interfaces\Response\IContent;
abstract class ViewBase implements IView
abstract class ContentBase implements IContent
{
protected array $data;

View File

@ -1,6 +1,6 @@
<?php namespace MapGuesser\View;
<?php namespace MapGuesser\Response;
class HtmlView extends ViewBase
class HtmlContent extends ContentBase
{
private string $template;

View File

@ -1,6 +1,6 @@
<?php namespace MapGuesser\View;
<?php namespace MapGuesser\Response;
class JsonView extends ViewBase
class JsonContent extends ContentBase
{
public function __construct(array &$data = [])
{

41
src/Response/Redirect.php Normal file
View File

@ -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;
}
}
}