Completed
Branch master (d12331)
by Ventimiglia
01:57
created

FirebaseResponce::validateJson()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 7
nop 0
1
<?php
2
declare(strict_types = 1);
3
namespace ZendFirebase;
4
5
/**
6
 * PHP7 FIREBASE LIBRARY (http://samuelventimiglia.it/)
7
 *
8
 *
9
 * @link https://github.com/Samuel18/zend_Firebase
10
 * @copyright Copyright (c) 2016-now Ventimiglia Samuel - Biasin Davide
11
 * @license BSD 3-Clause License
12
 *
13
 */
14
15
/**
16
 *
17
 * @author Davide Biasin
18
 * @package ZendFirebase
19
 */
20
class FirebaseResponce
21
{
22
23
    /**
24
     * Data from Firebase
25
     *
26
     * @var array $firebaseData
27
     */
28
    private $firebaseData;
29
30
    /**
31
     * Type of operation
32
     *
33
     * @var string $operation
34
     */
35
    protected $operation;
36
37
    /**
38
     * Http server code
39
     *
40
     * @var integer $status
41
     */
42
    protected $status;
43
44
    /**
45
     * Constructior method
46
     */
47
    public function __construct()
48
    {
49
    }
50
51
    /**
52
     * Remove this current Object from memory
53
     */
54
    public function __destruct()
55
    {
56
    }
57
58
    /**
59
     * Format to array the responce
60
     *
61
     * @return array $firebaseData
62
     */
63
    public function getFirebaseData(): array
64
    {
65
        return $this->firebaseData;
66
    }
67
68
    /**
69
     * Type of Operation
70
     *
71
     * @return string $operation
72
     */
73
    public function getOperation(): string
74
    {
75
        return $this->operation;
76
    }
77
78
    /**
79
     * Status from http server
80
     *
81
     * @return integer $status
82
     */
83
    public function getStatus(): int
84
    {
85
        return $this->status;
86
    }
87
88
    /**
89
     * Set data from firebase api
90
     *
91
     * @param array $firebaseData
92
     */
93
    public function setFirebaseData($firebaseData)
94
    {
95
        $this->firebaseData = $firebaseData;
96
    }
97
98
    /**
99
     * Set type of operation
100
     *
101
     * @param string $operation
102
     */
103
    public function setOperation($operation)
104
    {
105
        $this->operation = $operation;
106
    }
107
108
    /**
109
     * Set status responce
110
     *
111
     * @param integer $status
112
     */
113
    public function setStatus($status)
114
    {
115
        $this->status = $status;
116
    }
117
118
    /**
119
     * Method for validate data arrives in _costruct.
120
     * If all data was correct skip function without returns.
121
     *
122
     * @throws \Exception
123
     */
124
    public function validateResponce()
125
    {
126
        try {
127
            /* check validity of Operation */
128
            $this->validateOperation();
129
            
130
            /* check validity of Status */
131
            $this->validateStatus();
132
            
133
            /* check validity of FirebaseData */
134
            $this->validateData();
135
        } catch (\Exception $e) {
136
            echo $e->getMessage();
137
        }
138
    }
139
140
    /**
141
     * Validate type of data receved
142
     *
143
     * @throws \Exception
144
     */
145
    private function validateOperation()
146
    {
147
        if (! is_string($this->getOperation())) {
148
            $getOperation = "Operation parameter must be STRING and NOT EMPTY. Received : ";
149
            $getOperation .= gettype($this->getOperation()) . " ({$this->getOperation()}).";
150
            
151
            throw new \Exception($getOperation);
152
        }
153
    }
154
155
    /**
156
     * Validate type of data receved
157
     *
158
     * @throws \Exception
159
     */
160
    private function validateStatus()
161
    {
162
        if (! is_numeric($this->getStatus())) {
163
            $getStatus = "Status parameter must be NUMERIC. Received : ";
164
            $getStatus .= gettype($this->getStatus()) . " ({$this->getStatus()}).";
165
            
166
            throw new \Exception($getStatus);
167
        }
168
    }
169
170
    /**
171
     * Validate type of data receved
172
     *
173
     * @throws \Exception
174
     */
175
    private function validateData()
176
    {
177
        if (! is_array($this->getFirebaseData())) {
178
            $gettype = "FirebaseData parameter must be ARRAY. Received : " . gettype($this->getFirebaseData()) . ".";
179
            throw new \Exception($gettype);
180
        }
181
    }
182
    
183
    /**
184
     * Validate type of json receved
185
     *
186
     * @return string|false
187
     */
188
    protected  function validateJson()
189
    {
190
        switch (json_last_error()) {
191
            case JSON_ERROR_NONE:
192
                $jsonValidator =  false;
193
                break;
194
            case JSON_ERROR_DEPTH:
195
                $jsonValidator =  ' - Maximum stack depth exceeded';
196
                break;
197
            case JSON_ERROR_STATE_MISMATCH:
198
                $jsonValidator =  ' - Underflow or the modes mismatch';
199
                break;
200
            case JSON_ERROR_CTRL_CHAR:
201
                $jsonValidator =  ' - Unexpected control character found';
202
                break;
203
            case JSON_ERROR_SYNTAX:
204
                $jsonValidator =  ' - Syntax error, malformed JSON';
205
                break;
206
            case JSON_ERROR_UTF8:
207
                $jsonValidator =  ' - Malformed UTF-8 characters, possibly incorrectly encoded';
208
                break;
209
            default:
210
                $jsonValidator =  ' - Unknown error';
211
                break;
212
        }
213
        
214
        return $jsonValidator;
215
    }
216
}
217