soko-web/src/Request/Request.php

105 lines
2.1 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;
2023-04-16 15:50:53 +02:00
private array $headers;
2023-05-02 11:16:20 +02:00
private ISession $session;
2023-04-07 19:32:15 +02:00
private ?IUser $user = null;
2023-04-16 15:50:53 +02:00
public function __construct(
string $base,
array $get,
array $post,
2023-04-16 15:50:53 +02:00
array $headers,
2023-05-02 11:16:20 +02:00
ISession $session,
2023-04-16 15:50:53 +02:00
IUserRepository $userRepository)
2023-04-07 19:32:15 +02:00
{
$this->base = $base;
$this->get = $get;
$this->post = $post;
2023-04-16 15:50:53 +02:00
$this->headers = $headers;
2023-05-02 11:16:20 +02:00
$this->session = $session;
2023-04-07 19:32:15 +02:00
$userId = $this->session->get('userId');
if ($userId !== null) {
$this->user = $userRepository->getById($userId);
}
}
public function setParsedRouteParams(array $routeParams): void
2023-04-07 19:32:15 +02:00
{
$this->routeParams = $routeParams;
2023-04-07 19:32:15 +02:00
}
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;
}
2023-04-16 15:50:53 +02:00
public function header($key)
{
if (isset($this->headers[$key])) {
return $this->headers[$key];
}
return null;
}
2023-04-07 19:32:15 +02:00
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;
}
}