MAPG-115 add new classes to store request data (get, post, session, etc.)

This commit is contained in:
Bence Pőcze 2020-06-09 00:54:44 +02:00
parent ecaf8ca9d4
commit fe814908c7
5 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,10 @@
<?php namespace MapGuesser\Interfaces\Authentication;
interface IUser
{
const PERMISSION_NORMAL = 0;
const PERMISSION_ADMIN = 1;
public function hasPermission(int $permission): bool;
}

View File

@ -0,0 +1,14 @@
<?php namespace MapGuesser\Interfaces\Request;
use MapGuesser\Interfaces\Authentication\IUser;
interface IRequest
{
public function query(string $key);
public function post(string $key);
public function session(): ISession;
public function user(): ?IUser;
}

View File

@ -0,0 +1,12 @@
<?php namespace MapGuesser\Interfaces\Request;
interface ISession
{
public function has(string $key): bool;
public function get(string $key);
public function set(string $key, $value): void;
public function delete(string $key): void;
}

57
src/Request/Request.php Normal file
View File

@ -0,0 +1,57 @@
<?php namespace MapGuesser\Request;
use MapGuesser\Interfaces\Authentication\IUser;
use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Interfaces\Request\ISession;
use MapGuesser\Model\User;
class Request implements IRequest
{
private array $get;
private array $routeParams;
private array $post;
private Session $session;
public function __construct(array &$get, array &$routeParams, array &$post, array &$session)
{
$this->get = &$get;
$this->routeParams = &$routeParams;
$this->post = &$post;
$this->session = new Session($session);
}
public function query($key)
{
if (isset($this->get[$key])) {
return $this->get[$key];
}
if (isset($this->routeParams[$key])) {
return $this->routeParams[$key];
}
return null;
}
public function post($key)
{
if (isset($this->post[$key])) {
return $this->post[$key];
}
return null;
}
public function session(): ISession
{
return $this->session;
}
public function user(): ?IUser
{
return $this->session->get('user');
}
}

37
src/Request/Session.php Normal file
View File

@ -0,0 +1,37 @@
<?php namespace MapGuesser\Request;
use MapGuesser\Interfaces\Request\ISession;
class Session implements ISession
{
private array $data;
public function __construct(array &$data)
{
$this->data = &$data;
}
public function has($key): bool
{
return isset($this->data[$key]);
}
public function get($key)
{
if (isset($this->data[$key])) {
return $this->data[$key];
}
return null;
}
public function set($key, $value): void
{
$this->data[$key] = $value;
}
public function delete($key): void
{
unset($this->data[$key]);
}
}