OAuthException::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 04/03/2018
6
 * Time: 17:40
7
 */
8
9
namespace OAuth2\Exceptions;
10
11
12
class OAuthException extends \Exception implements \JsonSerializable
13
{
14
    /**
15
     * @var string
16
     */
17
    private $error;
18
    /**
19
     * @var null|string
20
     */
21
    private $errorDescription;
22
    /**
23
     * @var null|string
24
     */
25
    private $errorUri;
26
27
    /**
28
     * OAuthException constructor.
29
     * @param string $error
30
     * @param null|string $errorDescription
31
     * @param null|string $errorUri
32
     *
33
     * error
34
     * REQUIRED.  A single ASCII [USASCII] error code.
35
     *
36
     * Values for the "error" parameter MUST NOT include characters
37
     * outside the set %x20-21 / %x23-5B / %x5D-7E.
38
     *
39
     * error_description
40
     * OPTIONAL.  Human-readable ASCII [USASCII] text providing
41
     * additional information, used to assist the client developer in
42
     * understanding the error that occurred.
43
     * Values for the "error_description" parameter MUST NOT include
44
     * characters outside the set %x20-21 / %x23-5B / %x5D-7E.
45
     *
46
     * error_uri
47
     * OPTIONAL.  A URI identifying a human-readable web page with
48
     * information about the error, used to provide the client
49
     * developer with additional information about the error.
50
     * Values for the "error_uri" parameter MUST conform to the
51
     * URI-reference syntax and thus MUST NOT include characters
52
     * outside the set %x21 / %x23-5B / %x5D-7E.
53
     */
54
    public function __construct(string $error, ?string $errorDescription = null, ?string $errorUri = null)
55
    {
56
        parent::__construct($errorDescription);
57
        $this->error = $error;
58
        $this->errorDescription = $errorDescription;
59
        $this->errorUri = $errorUri;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getError(): string
66
    {
67
        return $this->error;
68
    }
69
70
    /**
71
     * @return null|string
72
     */
73
    public function getErrorDescription(): ?string
74
    {
75
        return $this->errorDescription;
76
    }
77
78
    /**
79
     * @return null|string
80
     */
81
    public function getErrorUri(): ?string
82
    {
83
        return $this->errorUri;
84
    }
85
86
    /**
87
     * Specify data which should be serialized to JSON
88
     * @link  http://php.net/manual/en/jsonserializable.jsonserialize.php
89
     * @return mixed data which can be serialized by <b>json_encode</b>,
90
     * which is a value of any type other than a resource.
91
     * @since 5.4.0
92
     */
93
    public function jsonSerialize()
94
    {
95
        $data = [
96
            'error' => $this->error
97
        ];
98
        if ($this->errorDescription) {
99
            $data['error_description'] = $this->errorDescription;
100
        }
101
        if ($this->errorUri) {
102
            $data['error_uri'] = $this->errorUri;
103
        }
104
105
        return json_encode($data);
106
    }
107
}