Completed
Push — master ( 3f77a7...938ad9 )
by Andrii
03:48
created

AbstractResponse::isRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart;
12
13
use yii\helpers\Json;
14
15
abstract class AbstractResponse implements ResponseInterface
16
{
17
    /**
18
     * @var RequestInterface
19
     */
20
    protected $request;
21
22
    /**
23
     * @var mixed response data
24
     */
25
    protected $data;
26
27
    protected $isDecoded = false;
28
29 2
    public function getRequest()
30
    {
31 2
        return $this->request;
32
    }
33
34 2
    public function getData()
35
    {
36 2
        if (!$this->isDecoded) {
37 2
            $this->data = $this->decodeData();
38 2
            $this->isDecoded = true;
39 2
        }
40
41 2
        return $this->data;
42
    }
43
44 2
    public function decodeData()
45
    {
46 2
        $data = $this->getRawData();
47 2
        if (!$this->isRaw() && $this->isJson()) {
48 2
            $data = Json::decode($data);
49 2
        }
50
51 2
        return $data;
52
    }
53
54
    abstract public function getRawData();
55
56 2
    public function isRaw()
57
    {
58 2
        return $this->request->isRaw();
59
    }
60
61 2
    public function isJson()
62
    {
63 2
        return !empty(preg_grep('|application/json|i', $this->getHeader('Content-Type')));
64
    }
65
66
    abstract public function getHeader($name);
67
}
68