soko-web/src/Request/Request.php

87 lines
1.8 KiB
PHP
Raw Normal View History

2023-04-07 19:32:15 +02:00
<?php namespace SokoWeb\Request;
use SokoWeb\Interfaces\Repository\IUserRepository;
use SokoWeb\Interfaces\Authentication\IUser;
use SokoWeb\Interfaces\Request\IRequest;
use SokoWeb\Interfaces\Request\ISession;
class Request implements IRequest
{
private string $base;
private array $get;
private array $routeParams = [];
private array $post;
private Session $session;
private ?IUser $user = null;
public function __construct(string $base, array &$get, array &$post, array &$session, IUserRepository $userRepository)
{
$this->base = $base;
$this->get = &$get;
$this->post = &$post;
$this->session = new Session($session);
$userId = $this->session->get('userId');
if ($userId !== null) {
$this->user = $userRepository->getById($userId);
}
}
public function setParsedRouteParams(array &$routeParams): void
{
$this->routeParams = &$routeParams;
}
public function getBase(): string
{
return $this->base;
}
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 setUser(?IUser $user): void
{
if ($user === null) {
$this->session->delete('userId');
return;
}
$this->session->set('userId', $user->getUniqueId());
}
public function user(): ?IUser
{
return $this->user;
}
}