|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Moip\Exceptions; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Error. |
|
7
|
|
|
* Represents an error returned by the API. |
|
8
|
|
|
*/ |
|
9
|
|
|
class Error |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Code of error. |
|
13
|
|
|
* |
|
14
|
|
|
* @var int|string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $code; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Path of error. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $path; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Description of error. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $description; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Error constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* Represents an error return by the API. Commonly used by {@see \Moip\Exceptions\ValidationException} |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $code unique error identifier. |
|
38
|
|
|
* @param string $path represents the field where the error ocurred. |
|
39
|
|
|
* @param string $description error description. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($code, $path, $description) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->code = $code; |
|
44
|
|
|
$this->path = $path; |
|
45
|
|
|
$this->description = $description; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the unique alphanumeric identifier of the error, ie.: "API-1". |
|
50
|
|
|
* |
|
51
|
|
|
* @return int|string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getCode() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->code; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns the dotted string representing the field where the error ocurred, ie.: "customer.birthDate". |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getPath() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->path; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the error description. |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getDescription() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->description; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Creates an Error array from a json string. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $json_string string returned by the Moip API |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function parseErrors($json_string) |
|
86
|
|
|
{ |
|
87
|
|
|
$error_obj = json_decode($json_string); |
|
88
|
|
|
$errors = []; |
|
89
|
|
|
if (!empty($error_obj->errors)) { |
|
90
|
|
|
foreach ($error_obj->errors as $error) { |
|
91
|
|
|
$errors[] = new self($error->code, $error->path, $error->description); |
|
92
|
|
|
} |
|
93
|
|
|
} elseif (!empty($error_obj->error)) { |
|
94
|
|
|
$errors[] = new self('', '', $error_obj->error); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $errors; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|