Response::isJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of the AMFWebServicesClientBundle package.
5
 *
6
 * (c) Amine Fattouch <http://github.com/fattouchsquall>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AMF\WebServicesClientBundle\Rest\Component;
13
14
/**
15
 * This class represents a ReST Response.
16
 *
17
 * @author Mohamed Amine Fattouch <[email protected]>
18
 */
19
class Response
20
{
21
    /**
22
     * @var mixed
23
     */
24
    protected $content;
25
26
    /**
27
     * @var integer
28
     */
29
    protected $statusCode;
30
31
    /**
32
     * @var string
33
     */
34
    protected $format;
35
36
    /**
37
     * Constructor class.
38
     *
39
     * @param mixed   $content    The content of response.
40
     * @param integer $statusCode The status code of response.
41
     */
42
    public function __construct($content, $statusCode)
43
    {
44
        $this->content    = $content;
45
        $this->statusCode = $statusCode;
46
    }
47
48
    /**
49
     * Getter for content.
50
     *
51
     * @return mixed
52
     */
53
    public function getContent()
54
    {
55
        return $this->content;
56
    }
57
58
    /**
59
     * Getter for status code.
60
     *
61
     * @return integer
62
     */
63
    public function getStatusCode()
64
    {
65
        return $this->statusCode;
66
    }
67
68
    /**
69
     * The setter for response format.
70
     *
71
     * @param string $format The response format.
72
     *
73
     * @void
74
     */
75
    public function setResponseFormat($format = 'json')
76
    {
77
        $this->format = $format;
78
    }
79
80
    /**
81
     * The getter the response format.
82
     *
83
     * @return string The response format.
84
     */
85
    public function getResponseFormat()
86
    {
87
        return $this->format;
88
    }
89
90
    /**
91
     * Tests whether the response is a valid json or not.
92
     *
93
     * @return boolean
94
     */
95
    public function isJson()
96
    {
97
        json_decode($this->content);
98
99
        return (json_last_error() == JSON_ERROR_NONE);
100
    }
101
102
    /**
103
     * Tests whether the response is a valid xml or not.
104
     *
105
     * @return boolean
106
     */
107
    public function isXml()
108
    {
109
        $loaded = simplexml_load_string($this->content);
110
111
        if ($loaded === false) {
112
            return false;
113
        }
114
115
        return true;
116
    }
117
118
    /**
119
     * Tests whether the response is ok or not.
120
     *
121
     * @return boolean
122
     */
123
    public function isSuccess()
124
    {
125
        return $this->statusCode >= 200 && $this->statusCode < 300;
126
    }
127
128
    /**
129
     * Tests whether there's a froward or not.
130
     *
131
     * @return boolean
132
     */
133
    public function isForward()
134
    {
135
        return $this->statusCode >= 300 && $this->statusCode < 400;
136
    }
137
138
    /**
139
     * Tests whether there's a client error.
140
     *
141
     * @return boolean
142
     */
143
    public function isClientError()
144
    {
145
        return $this->statusCode >= 400 && $this->statusCode < 500;
146
    }
147
148
    /**
149
     * Tests whether there's an internal error or not.
150
     *
151
     * @return boolean
152
     */
153
    public function isInternetError()
154
    {
155
        return $this->statusCode >= 500 && $this->statusCode < 600;
156
    }
157
158
    /**
159
     * Creates a response.
160
     *
161
     * @param mixed   $content    The content of response.
162
     * @param integer $statusCode The code status of response.
163
     *
164
     * @return \static
165
     */
166
    public static function create($content, $statusCode)
167
    {
168
        return new static($content, $statusCode);
169
    }
170
}
171