PluginProbe
Moloni / 3.0.51
Moloni v3.0.51
5.0.10 5.0.09 5.0.08 5.0.07 5.0.06 trunk 0.4.0.00 0.4.8.1 3.0.10 3.0.11 3.0.35 3.0.36 3.0.37 3.0.38 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.0.46 3.0.47 3.0.48 3.0.49 3.0.50 All 98 releases
moloni / src / Error.php

Error.php in Moloni 3.0.51, at src/Error.php

94 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Moloni;
4
5 use Exception;
6
7 class Error extends Exception
8 {
9 /** @var array */
10 private $request;
11
12 /**
13 * Throws a new error with a message and a log from the last request made
14 * @param $message
15 * @param bool $request
16 * @param int $code
17 * @param Exception|null $previous
18 */
19 public function __construct($message, $request = false, $code = 0, Exception $previous = null)
20 {
21 $this->request = $request ?: Curl::getLog();
22 parent::__construct($message, $code, $previous);
23 }
24
25 public function showError()
26 {
27 /** @noinspection PhpUnusedLocalVariableInspection */
28 $message = $this->getDecodedMessage();
29
30 /** @noinspection PhpUnusedLocalVariableInspection */
31 $url = $this->request['url'] ?: '';
32
33 /** @noinspection PhpUnusedLocalVariableInspection */
34 $sent = $this->request['sent'] ?: [];
35
36 /** @noinspection PhpUnusedLocalVariableInspection */
37 $received = $this->request['received'] ?: [];
38
39 include MOLONI_TEMPLATE_DIR . 'Messages/DocumentError.php';
40 }
41
42 public function getError()
43 {
44 ob_start();
45 $this->showError();
46 return ob_get_clean();
47 }
48
49 /**
50 * Returns the default error message from construct
51 * Or tries to translate the error from Moloni API
52 * @return string
53 */
54 public function getDecodedMessage()
55 {
56 $errorMessage = '<b>' . $this->getMessage() . '</b>';
57
58 if (isset($this->request['received']) && is_array($this->request['received'])) {
59 foreach ($this->request['received'] as $line) {
60 if (isset($line['description'])) {
61 $errorMessage .= '<br>' . $this->translateMessage($line['description']);
62 } elseif (isset($line[0]['description'])) {
63 $errorMessage .= '<br>' . $this->translateMessage($line[0]['description']);
64 }
65 }
66 }
67
68 return $errorMessage;
69 }
70
71 /**
72 * @param string $message
73 * @return string
74 */
75 private function translateMessage($message)
76 {
77 switch ($message) {
78 case 'Field \'exemption_reason\' is required':
79 $message = __('Um dos artigos não tem uma taxa de IVA associada e como tal, tem que seleccionar uma razão de isenção');
80 break;
81 case "Field 'category_id' must be integer, greater than 0" :
82 $message = __('Verifique por favor se o artigo tem uma categoria associada');
83 break;
84
85 }
86
87 return $message;
88 }
89
90 public function getRequest()
91 {
92 return $this->request;
93 }
94 }