externalize Success/ErrorResponse classes into their own files
This commit is contained in:
@@ -56,7 +56,7 @@ class API {
|
|||||||
|
|
||||||
function setupPublic() {
|
function setupPublic() {
|
||||||
if($this->checkToken() === false) {
|
if($this->checkToken() === false) {
|
||||||
$response = new ErrorResponse(array(
|
$response = new APIErrorResponse(array(
|
||||||
'unauthorized' => __('This request is not authorized.')
|
'unauthorized' => __('This request is not authorized.')
|
||||||
), APIResponse::STATUS_UNAUTHORIZED);
|
), APIResponse::STATUS_UNAUTHORIZED);
|
||||||
$response->send();
|
$response->send();
|
||||||
@@ -130,17 +130,17 @@ class API {
|
|||||||
$data = array(), $meta = array(), $status = APIResponse::STATUS_OK
|
$data = array(), $meta = array(), $status = APIResponse::STATUS_OK
|
||||||
) {
|
) {
|
||||||
|
|
||||||
return new SuccessResponse($data, $meta, $status);
|
return new APISuccessResponse($data, $meta, $status);
|
||||||
}
|
}
|
||||||
|
|
||||||
function errorResponse(
|
function errorResponse(
|
||||||
$errors = array(), $meta = array(), $status = APIResponse::STATUS_NOT_FOUND
|
$errors = array(), $meta = array(), $status = APIResponse::STATUS_NOT_FOUND
|
||||||
) {
|
) {
|
||||||
|
|
||||||
return new ErrorResponse($errors, $meta, $status);
|
return new APIErrorResponse($errors, $meta, $status);
|
||||||
}
|
}
|
||||||
|
|
||||||
function badRequest($errors = array(), $meta = array()) {
|
function badRequest($errors = array(), $meta = array()) {
|
||||||
return new ErrorResponse($errors, $meta, APIResponse::STATUS_BAD_REQUEST);
|
return new APIErrorResponse($errors, $meta, APIResponse::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
34
lib/API/APIErrorResponse.php
Normal file
34
lib/API/APIErrorResponse.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\API;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class APIErrorResponse extends APIResponse {
|
||||||
|
public $errors;
|
||||||
|
|
||||||
|
function __construct($errors = array(), $meta = array(), $status = self::STATUS_NOT_FOUND) {
|
||||||
|
parent::__construct($status, $meta);
|
||||||
|
$this->errors = $this->formatErrors($errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getData() {
|
||||||
|
if(empty($this->errors)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return array(
|
||||||
|
'errors' => $this->errors
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatErrors($errors = array()) {
|
||||||
|
$formatted_errors = array();
|
||||||
|
foreach($errors as $error => $message) {
|
||||||
|
$formatted_errors[] = array(
|
||||||
|
'error' => $error,
|
||||||
|
'message' => $message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $formatted_errors;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\API;
|
namespace MailPoet\API;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
abstract class APIResponse {
|
abstract class APIResponse {
|
||||||
const STATUS_OK = 200;
|
const STATUS_OK = 200;
|
||||||
const STATUS_BAD_REQUEST = 400;
|
const STATUS_BAD_REQUEST = 400;
|
||||||
@@ -36,53 +39,4 @@ abstract class APIResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract function getData();
|
abstract function getData();
|
||||||
}
|
|
||||||
|
|
||||||
class SuccessResponse extends APIResponse {
|
|
||||||
public $data;
|
|
||||||
|
|
||||||
function __construct($data = array(), $meta = array(), $status = self::STATUS_OK) {
|
|
||||||
parent::__construct($status, $meta);
|
|
||||||
$this->data = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getData() {
|
|
||||||
if(empty($this->data)) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return array(
|
|
||||||
'data' => $this->data
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ErrorResponse extends APIResponse {
|
|
||||||
public $errors;
|
|
||||||
|
|
||||||
function __construct($errors = array(), $meta = array(), $status = self::STATUS_NOT_FOUND) {
|
|
||||||
parent::__construct($status, $meta);
|
|
||||||
$this->errors = $this->formatErrors($errors);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getData() {
|
|
||||||
if(empty($this->errors)) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return array(
|
|
||||||
'errors' => $this->errors
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatErrors($errors = array()) {
|
|
||||||
$formatted_errors = array();
|
|
||||||
foreach($errors as $error => $message) {
|
|
||||||
$formatted_errors[] = array(
|
|
||||||
'error' => $error,
|
|
||||||
'message' => $message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return $formatted_errors;
|
|
||||||
}
|
|
||||||
}
|
}
|
23
lib/API/APISuccessResponse.php
Normal file
23
lib/API/APISuccessResponse.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\API;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class APISuccessResponse extends APIResponse {
|
||||||
|
public $data;
|
||||||
|
|
||||||
|
function __construct($data = array(), $meta = array(), $status = self::STATUS_OK) {
|
||||||
|
parent::__construct($status, $meta);
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getData() {
|
||||||
|
if(empty($this->data)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return array(
|
||||||
|
'data' => $this->data
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user