1
|
|
|
<?php |
2
|
|
|
namespace agoalofalife\bpm\Handlers; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use agoalofalife\bpm\Contracts\Collection; |
6
|
|
|
use agoalofalife\bpm\Contracts\Handler; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property string buildJson |
10
|
|
|
*/ |
11
|
|
|
class JsonHandler implements Handler, Collection |
12
|
|
|
{ |
13
|
|
|
use XmlConverter; |
14
|
|
|
|
15
|
|
|
protected $response; |
16
|
|
|
protected $buildJson; |
17
|
|
|
|
18
|
|
|
private $jsonPrefix = 'd'; |
19
|
|
|
private $jsonPrefixWord = 'results'; |
20
|
|
|
private $validText = []; |
21
|
|
|
|
22
|
1 |
|
public function getAccept() |
23
|
|
|
{ |
24
|
1 |
|
return 'application/json;odata=verbose;'; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public function getContentType() |
28
|
|
|
{ |
29
|
1 |
|
return 'application/json;odata=verbose;'; |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
public function parse($parse) |
33
|
|
|
{ |
34
|
5 |
|
if ($this->checkIntegrity($parse) === false) |
35
|
5 |
|
{ |
36
|
1 |
|
return []; |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
$this->response = $parse; |
40
|
4 |
|
$this->validText = json_decode($parse)->{$this->jsonPrefix}->{$this->jsonPrefixWord}; |
41
|
4 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $response |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
7 |
|
public function checkIntegrity($response) |
49
|
|
|
{ |
50
|
7 |
|
return isset( json_decode($response)->{$this->jsonPrefix} ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
1 |
|
public function toArray() |
57
|
|
|
{ |
58
|
1 |
|
return $this->objectToArray($this->validText); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string json |
63
|
|
|
*/ |
64
|
1 |
|
public function toJson() |
65
|
|
|
{ |
66
|
1 |
|
return json_encode($this->validText); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function getData() |
70
|
|
|
{ |
71
|
1 |
|
return $this->validText; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public function create(array $data) |
75
|
|
|
{ |
76
|
1 |
|
if ( empty($data)){ |
77
|
1 |
|
return $this->buildJson = '{}'; |
78
|
|
|
} |
79
|
1 |
|
return $this->buildJson = json_encode($data); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
private function objectToArray($data) |
83
|
|
|
{ |
84
|
1 |
|
$result = array(); |
85
|
|
|
|
86
|
1 |
|
foreach ($data as $key => $value) { |
87
|
1 |
|
if (gettype($value) == 'object') |
88
|
1 |
|
{ |
89
|
1 |
|
$result[$key] = $this->objectToArray($value); |
90
|
1 |
|
} else{ |
91
|
1 |
|
if (gettype($value) != 'object') |
92
|
1 |
|
{ |
93
|
1 |
|
$result[$key] = $value; |
94
|
1 |
|
} |
95
|
|
|
// TODO not smog to find a similar case |
96
|
|
|
/* else{ |
|
|
|
|
97
|
|
|
$result[$key] = get_object_vars($value); |
98
|
|
|
}*/ |
99
|
|
|
|
100
|
|
|
} |
101
|
1 |
|
} |
102
|
1 |
|
return $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.