Completed
Push — master ( e78a07...82539f )
by Jean C.
02:16
created

Error::parseErrors()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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