PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 5.33.1
MailPoet – Newsletters, Email Marketing, and Automation v5.33.1
5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 4.44.0 All 541 releases
mailpoet / lib / API / JSON / ErrorResponse.php
ErrorResponse.php
37 lines 921 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
2
3 namespace MailPoet\API\JSON;
4
5 if (!defined('ABSPATH')) exit;
6
7
8 class ErrorResponse extends Response {
9 public $errors;
10
11 public function __construct(
12 $errors = [],
13 $meta = [],
14 $status = self::STATUS_NOT_FOUND
15 ) {
16 parent::__construct($status, $meta);
17 $this->errors = $this->formatErrors($errors);
18 }
19
20 public function getData() {
21 return (empty($this->errors)) ? null : ['errors' => $this->errors];
22 }
23
24 public function formatErrors($errors = []) {
25 return array_map(function($error, $message) {
26 // sanitize SQL error
27 if (preg_match('/^SQLSTATE/i', $message)) {
28 $message = __('An unknown error occurred.', 'mailpoet');
29 }
30 return [
31 'error' => $error,
32 'message' => $message,
33 ];
34 }, array_keys($errors), array_values($errors));
35 }
36 }
37