83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\DTO;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class ApiErrorMessage
|
|
{
|
|
const TYPE_VALIDATION_ERROR = 'validation_error';
|
|
const TYPE_INVALID_REQUEST_BODY_FORMAT = 'invalid_body_format';
|
|
const TYPE_LOGICAL_ERROR = 'logical_error';
|
|
const TYPE_SERVER_ERROR = 'server_error';
|
|
const TYPE_INVALID_DATE = 'invalid_date';
|
|
const TYPE_API_LIMIT = 'api_limit';
|
|
|
|
private static $titles = array(
|
|
self::TYPE_VALIDATION_ERROR => 'There was a validation error',
|
|
self::TYPE_INVALID_REQUEST_BODY_FORMAT => 'Invalid JSON format sent',
|
|
self::TYPE_LOGICAL_ERROR => 'Invalid request due to a logic error',
|
|
self::TYPE_SERVER_ERROR => 'Error on server side',
|
|
self::TYPE_INVALID_DATE => 'Invalid date on request',
|
|
self::TYPE_API_LIMIT => 'Reached API Limit'
|
|
);
|
|
|
|
private int $statusCode;
|
|
private string $type;
|
|
private string $title;
|
|
private array $extraData = array();
|
|
|
|
public function __construct($statusCode, $type = null)
|
|
{
|
|
if ($type === null) {
|
|
// no type? The default is about:blank and the title should
|
|
// be the standard status code message
|
|
$type = 'about:blank';
|
|
$title = isset(Response::$statusTexts[$statusCode]) ?
|
|
Response::$statusTexts[$statusCode] : 'Unknown status code :(';
|
|
} else {
|
|
if (!isset(self::$titles[$type])) {
|
|
throw new \InvalidArgumentException('No title for type ' . $type);
|
|
}
|
|
$title = self::$titles[$type];
|
|
}
|
|
|
|
$this->statusCode = $statusCode;
|
|
$this->type = $type;
|
|
$this->title = $title;
|
|
}
|
|
|
|
public function getStatusCode(): int
|
|
{
|
|
return $this->statusCode;
|
|
}
|
|
|
|
public function getTitle(): string
|
|
{
|
|
return $this->title;
|
|
}
|
|
|
|
public function setExtraData($name, $value)
|
|
{
|
|
$this->extraData[$name] = $value;
|
|
}
|
|
|
|
public function setDetails($value)
|
|
{
|
|
$this->extraData['details'] = $value;
|
|
}
|
|
|
|
|
|
public function toArray(): array
|
|
{
|
|
return array_merge(
|
|
$this->extraData,
|
|
array(
|
|
'status' => $this->statusCode,
|
|
'type' => $this->type,
|
|
'title' => $this->title,
|
|
)
|
|
);
|
|
}
|
|
}
|