athenahr/app/Helpers/JSON.php

154 lines
2.5 KiB
PHP
Raw Normal View History

2021-03-29 23:47:55 +01:00
<?php
namespace App\Helpers;
/**
* Class JSON - Used for JSON responses.
*/
class JSON
{
protected $type;
protected $status;
protected $message;
protected $code;
2021-03-29 23:47:55 +01:00
protected $data;
protected $additional;
2021-03-29 23:47:55 +01:00
/**
* @param mixed $type
2021-03-29 23:47:55 +01:00
*/
public function setResponseType($type): JSON
{
$this->type = $type;
2021-03-29 23:47:55 +01:00
return $this;
}
/**
* @param mixed $additional
*/
public function setAdditional($additional)
{
$this->additional = $additional;
return $this;
}
/**
* @return mixed
*/
public function getAdditional()
{
return $this->additional;
}
2021-03-29 23:47:55 +01:00
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @return mixed
*/
public function getStatus()
{
return $this->status;
}
/**
* @param mixed $status
2021-03-29 23:47:55 +01:00
* @return JSON
*/
public function setStatus($status)
{
$this->status = $status;
2021-03-29 23:47:55 +01:00
return $this;
}
/**
* @return mixed
*/
public function getMessage()
{
return $this->message;
}
/**
* @param mixed $message
2021-03-29 23:47:55 +01:00
* @return JSON
*/
public function setMessage($message)
{
$this->message = $message;
2021-03-29 23:47:55 +01:00
return $this;
}
/**
* @return mixed
*/
public function getCode()
{
return $this->code;
}
/**
* @param mixed $code
2021-03-29 23:47:55 +01:00
* @return JSON
*/
public function setCode($code)
{
$this->code = $code;
2021-03-29 23:47:55 +01:00
return $this;
}
/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* @param mixed $data
2021-03-29 23:47:55 +01:00
* @return JSON
*/
public function setData($data)
{
$this->data = $data;
2021-03-29 23:47:55 +01:00
return $this;
}
public function build($headers = [])
{
2021-03-31 03:55:09 +01:00
// Uses the same structure as model resources, for consistency when they aren't used.
2021-03-29 23:47:55 +01:00
$response = [
2021-03-31 03:55:09 +01:00
'data' => $this->getData(),
'meta' => [
'status' => $this->getStatus(),
'message' => $this->getMessage(),
],
2021-03-29 23:47:55 +01:00
];
if (! empty($this->additional)) {
foreach ($this->additional as $additionalKeyName => $key) {
$response[$additionalKeyName] = $key;
}
}
2021-03-29 23:47:55 +01:00
return response($response, $this->getCode(), $headers);
}
}