<?php
 class vB_Exception_Api extends vB_Exception { protected $errors = []; public function __construct($phrase_id = '', $args = [], $message = '', $code = false, $file = false, $line = false) { $fullmessage = '<b>API Error</b><br>'; if ($message) { $fullmessage .= '<b>Message:</b>: ' . htmlspecialchars($message) . '<br>'; } if (!empty($line)) { $fullmessage .= '<b>Line:</b> ' . htmlspecialchars($line) . '<br>'; } if ($phrase_id) { if (is_array($phrase_id)) { $temp = array_shift($phrase_id); if (!$args) { $args = $phrase_id; } $phrase_id = $temp; } if (!is_array($args)) { $args = [$args]; } $this->add_error($phrase_id, $args); $fullmessage .= '<b>Error:</b> ' . htmlspecialchars($phrase_id) . '<br>'; if ($args) { $fullmessage .= '<b>Args:</b><br><pre style="font-family:Lucida Console,Monaco5,monospace;font-size:small;overflow:auto;border:1px solid #CCC;">'; $fullmessage .= htmlspecialchars(var_export($args, true)); $fullmessage .= '</pre>'; } } parent::__construct($fullmessage, $code, $file, $line); } public function add_error($phrase_id, array $args = []) { if (is_array($phrase_id)) { $this->errors[] = $phrase_id; } else { $error = $args; array_unshift($error, $phrase_id); $this->errors[] = $error; } } public function add_errors(array $errors) : void { $this->errors = array_merge($this->errors, array_values($errors)); } public function has_errors() { return !empty($this->errors); } public function get_errors() { return $this->errors; } public function getErrors() { return $this->errors; } public function has_error($phrase_id) { if (!$this->has_errors()) { return false; } foreach ($this->errors AS $error) { if (in_array($phrase_id, $error)) { return true; } } return false; } } 