| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Exceptions; |
| 6 |
|
| 7 |
class SettingsException extends \Exception |
| 8 |
{ |
| 9 |
/** |
| 10 |
* The setting errors |
| 11 |
*/ |
| 12 |
protected array $settingErrors = []; |
| 13 |
|
| 14 |
/** |
| 15 |
* The accepted error keys |
| 16 |
*/ |
| 17 |
protected array $acceptedErrorKeys = [ |
| 18 |
'key', |
| 19 |
'message', |
| 20 |
]; |
| 21 |
|
| 22 |
/** |
| 23 |
* Set the data for the exception. Multiple address fields can contain an |
| 24 |
* error so each entry in the array should contain the key and the type of |
| 25 |
* the error. |
| 26 |
* @throws \Exception Should be uncaught to know we're doing it wrong |
| 27 |
*/ |
| 28 |
public function setErrors(array $settingErrors): SettingsException |
| 29 |
{ |
| 30 |
foreach ($settingErrors as $fields) { |
| 31 |
foreach ($fields as $key => $errorData) { |
| 32 |
if (!empty($this->acceptedErrorKeys) && !in_array($key, $this->acceptedErrorKeys)) { |
| 33 |
throw new \Exception('The key ' . esc_html($key) . ' is not accepted in the data array.'); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
$this->settingErrors = $settingErrors; |
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the setting errors from the exception |
| 44 |
*/ |
| 45 |
public function getErrors(): array |
| 46 |
{ |
| 47 |
return $this->settingErrors; |
| 48 |
} |
| 49 |
} |
| 50 |
|