File: /var/www/vhosts/creativefellows.nl/mygeomil.creativefellows.nl/03072023/src/Action/Action.php
<?php
namespace App\Action;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Container\ContainerInterface;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
abstract class Action
{
/**
* @var PhpRenderer
*/
protected $responder;
/**
* @var Request
*/
protected $request;
/**
* @var Response
*/
protected $response;
/**
* @var Container
*/
protected $container;
/**
* @var array
*/
protected $args;
public function __construct(ContainerInterface $container, PhpRenderer $responder)
{
$this->responder = $responder;
$this->container = $container;
}
/**
* @param Request $request
* @param Response $response
* @param array $args
* @return Response
* @throws HttpNotFoundException
* @throws HttpBadRequestException
*/
public function __invoke(Request $request, Response $response, $args): Response
{
$this->request = $request;
$this->response = $response;
$this->args = $args;
$this->section_name = $this->section();
return $this->action();
}
/**
* @return Response
* @throws DomainRecordNotFoundException
* @throws HttpBadRequestException
*/
abstract protected function action(): Response;
private function section()
{
$url_parts = explode("/",$this->request->getUri()->getPath());
return $url_parts[1];
}
/*public function returnToPage(string $redirect)
{
return $this->response
->withHeader('Location', $redirect)
->withStatus(200);
}*/
public function returnToPage(string $redirect,array $params = [])
{
if(!empty($params)){
$redirect = sprintf('%s?%s', $redirect, http_build_query($params,":"));
}
return $this->response
->withHeader('Location', $redirect)
->withStatus(200);
}
public function respond(array $array = []){
$json = json_encode($array, JSON_PRETTY_PRINT);
$this->response->getBody()->write($json);
return $this->response
->withHeader('Content-Type', 'application/json')
->withStatus(200);
}
public function nameToUrl($str)
{
return preg_replace("/[-]+/","-",preg_replace("/[^0-9a-zA-Z]/",'-', trim($str)));
}
}