JsonLintException::setJsonColumnNo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace CMPayments\JsonLint\Exceptions;
2
3
/**
4
 * Class JsonLintException
5
 *
6
 * @author  Bas Peters <[email protected]>
7
 * @author  Boy Wijnmaalen <[email protected]>
8
 *
9
 * @package CMPayments\JsonLint\Exceptions
10
 */
11
class JsonLintException extends \ErrorException
12
{
13
    const ERROR_TEMPORARILY_UNAVAILABLE = 1;
14
    const ERROR_LEXICAL_ERROR           = 2;
15
16
    protected $messages = [
17
        self::ERROR_TEMPORARILY_UNAVAILABLE => 'This Service is temporarily unavailable',
18
        self::ERROR_LEXICAL_ERROR           => 'Lexical error on line %d, unrecognized text'
19
    ];
20
21
    /**
22
     * @var array
23
     */
24
    private $args = [];
25
26
    /**
27
     * @var null
28
     */
29
    private $jsonLineNo = null;
30
31
    /**
32
     * @var null
33
     */
34
    private $jsonColumnNo = null;
35
36
    /**
37
     * @var null
38
     */
39
    private $jsonMatch = null;
40
41
    /**
42
     * @var null
43
     */
44
    private $jsonToken = null;
45
46
    /**
47
     * @var null
48
     */
49
    private $jsonExpected = null;
50
51
    /**
52
     * @return mixed
53
     */
54
    public function getArgs()
55
    {
56
        return $this->args;
57
    }
58
59
    /**
60
     * @param mixed $args
61
     */
62
    public function setArgs($args)
63
    {
64
        // check is $args is an array, if not, cast it into an array
65
        if (!is_array($args)) {
66
67
            $args = [$args];
68
        }
69
70
        $this->args = $args;
71
    }
72
73
    /**
74
     * @return null
75
     */
76
    public function getJsonLineNo()
77
    {
78
        return $this->jsonLineNo;
79
    }
80
81
    /**
82
     * @param null $jsonLineNo
83
     */
84
    public function setJsonLineNo($jsonLineNo)
85
    {
86
        $this->jsonLineNo = $jsonLineNo;
87
    }
88
89
    /**
90
     * @return null
91
     */
92
    public function getJsonColumnNo()
93
    {
94
        return $this->jsonColumnNo;
95
    }
96
97
    /**
98
     * @param integer $jsonColumnNo
99
     */
100
    public function setJsonColumnNo($jsonColumnNo)
101
    {
102
        $this->jsonColumnNo = $jsonColumnNo;
0 ignored issues
show
Documentation Bug introduced by
It seems like $jsonColumnNo of type integer is incompatible with the declared type null of property $jsonColumnNo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
103
    }
104
105
    /**
106
     * @return null
107
     */
108
    public function getJsonMatch()
109
    {
110
        return $this->jsonMatch;
111
    }
112
113
    /**
114
     * @param null $jsonMatch
115
     */
116
    public function setJsonMatch($jsonMatch)
117
    {
118
        $this->jsonMatch = $jsonMatch;
119
    }
120
121
    /**
122
     * @return null
123
     */
124
    public function getJsonToken()
125
    {
126
        return $this->jsonToken;
127
    }
128
129
    /**
130
     * @param null $jsonToken
131
     */
132
    public function setJsonToken($jsonToken)
133
    {
134
        $this->jsonToken = $jsonToken;
135
    }
136
137
    /**
138
     * @return null
139
     */
140
    public function getJsonExpected()
141
    {
142
        return $this->jsonExpected;
143
    }
144
145
    /**
146
     * @param null $jsonExpected
147
     */
148
    public function setJsonExpected($jsonExpected)
149
    {
150
        $this->jsonExpected = $jsonExpected;
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function getDetails()
157
    {
158
        return [
159
            'errorCode' => $this->getCode(),
160
            'match'     => $this->getJsonMatch(),
161
            'token'     => $this->getJsonToken(),
162
            'line'      => $this->getJsonLineNo(),
163
            'column'    => $this->getJsonColumnNo(),
164
            'expected'  => $this->getJsonExpected()
165
        ];
166
    }
167
168
    /**
169
     * Append the current message with some more text
170
     *
171
     * @param string|null $appendingText
172
     */
173
    public function appendMessage($appendingText)
174
    {
175
        $this->message = $this->getMessage() . $appendingText;
176
    }
177
178
    /**
179
     * ApiException constructor.
180
     *
181
     * @param string $code
182
     * @param array  $args
183
     * @param null   $message
184
     */
185
    public function __construct($code, $args = [], $message = null)
186
    {
187
        $this->setArgs($args);
188
189
        // parent constructor
190
        parent::__construct($this->getItemFromVariableArray($code, 'This service is temporarily unavailable'), $code);
191
    }
192
193
    /**
194
     * Retrieves a specific array key from a class constant
195
     *
196
     * @param int    $code
197
     * @param string $default
198
     * @param string $msgArray
199
     *
200
     * @return null|string
201
     */
202
    public function getItemFromVariableArray($code, $default = null, $msgArray = 'messages')
203
    {
204
        $messages = [];
205
        if (isset($this->$msgArray)) {
206
207
            $messages = $this->$msgArray;
208
        }
209
210
        // override because when the given code exists
211
        if (isset($messages[$code])) {
212
213
            $default = vsprintf($messages[$code], $this->getArgs());
214
        }
215
216
        return $default;
217
    }
218
219
    /**
220
     * PHP 5.4 workaround for something like this ClassA::class (which is invalid in PHP5.4)
221
     *
222
     * @return string
223
     */
224
    static public function getClassName()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
225
    {
226
        return get_called_class();
227
    }
228
}
229