Skip to content

Instantly share code, notes, and snippets.

@yahyaerturan
Created November 12, 2018 12:52
Show Gist options
  • Save yahyaerturan/b0d1c013f4f54b4bb99df5bcd39544ae to your computer and use it in GitHub Desktop.
Save yahyaerturan/b0d1c013f4f54b4bb99df5bcd39544ae to your computer and use it in GitHub Desktop.
<?php
namespace App\Helper;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Translation\TranslatorInterface;
class XhrResponse
{
private $message;
private $data;
private $statusCode = 200;
private $headers = array();
private $json = false;
private $translator;
public function __construct($message = null, int $statusCode = 200, $data = null, array $headers = array(), bool $json = false, TranslatorInterface $translator)
{
$this->translator = $translator;
$this->setMessage($this->translator->trans($message));
$this->setData($data);
$this->setStatusCode($statusCode);
$this->setHeaders($headers);
$this->setJson($json);
if($this->getMessage() OR $this->getData())
{
$this->execute();
}
}
/**
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}
/**
* @param string|null $message
* @return XhrResponse
*/
public function setMessage(?string $message): XhrResponse
{
$this->message = $message;
return $this;
}
public function getData()
{
return $this->data;
}
/**
* @param $data
* @return XhrResponse
*/
public function setData($data): XhrResponse
{
$this->data = $data;
return $this;
}
/**
* @return int
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* @param int $statusCode
* @return XhrResponse
*/
public function setStatusCode(int $statusCode): XhrResponse
{
$this->statusCode = $statusCode;
return $this;
}
/**
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* @param array $headers
* @return XhrResponse
*/
public function setHeaders(array $headers): XhrResponse
{
$this->headers = $headers;
return $this;
}
/**
* @return bool
*/
public function isJson(): bool
{
return $this->json;
}
/**
* @param bool $json
* @return XhrResponse
*/
public function setJson(bool $json): XhrResponse
{
$this->json = $json;
return $this;
}
public function execute()
{
$response = (object) null;
if($this->getStatusCode() >= 200 && $this->getStatusCode() < 300)
{
$response->data = (object) null;
$response->data->status = "success";
$response->data->message = $this->getMessage();
$response->data->items = $this->getMessage();
}
else if ($this->getStatusCode() >= 400 && $this->getStatusCode() < 500)
{
$response->error = (object) null;
$response->error->status = "error";
$response->error->code = $this->getStatusCode();
$response->error->message = $this->getMessage()
? $this->translator->trans($this->getMessage())
: $this->translator->trans("Error");
$response->error->errors = $this->getData();
}
return new JsonResponse($response, $this->statusCode, $this->headers, $this->isJson());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment