|
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
|
|
|
private $jsonPrefixAllCollection = 'EntitySets'; |
|
22
|
|
|
|
|
23
|
1 |
|
public function getAccept() |
|
24
|
|
|
{ |
|
25
|
1 |
|
return 'application/json;odata=verbose;'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function getContentType() |
|
29
|
|
|
{ |
|
30
|
1 |
|
return 'application/json;odata=verbose;'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
6 |
|
public function parse($parse) |
|
34
|
|
|
{ |
|
35
|
6 |
|
if ($this->checkIntegrity($parse) === false) |
|
36
|
6 |
|
{ |
|
37
|
1 |
|
return []; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
$this->response = $parse; |
|
41
|
5 |
|
$this->validText = $this->chooseJsonPrefix($parse); |
|
|
|
|
|
|
42
|
5 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param $parse string json |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
5 |
|
private function chooseJsonPrefix($parse) |
|
50
|
|
|
{ |
|
51
|
5 |
|
$decode = json_decode($parse); |
|
52
|
5 |
|
if ( isset($decode->{$this->jsonPrefix}->{$this->jsonPrefixWord}) ) |
|
53
|
5 |
|
{ |
|
54
|
4 |
|
return $decode->{$this->jsonPrefix}->{$this->jsonPrefixWord}; |
|
55
|
|
|
} else { |
|
56
|
1 |
|
return $decode->{$this->jsonPrefix}->{$this->jsonPrefixAllCollection}; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param $response |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
8 |
|
public function checkIntegrity($response) |
|
65
|
|
|
{ |
|
66
|
8 |
|
return isset( json_decode($response)->{$this->jsonPrefix} ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function toArray() |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->objectToArray($this->validText); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string json |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function toJson() |
|
81
|
|
|
{ |
|
82
|
1 |
|
return json_encode($this->validText); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function getData() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return $this->validText; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function create(array $data) |
|
91
|
|
|
{ |
|
92
|
1 |
|
if ( empty($data)){ |
|
93
|
1 |
|
return $this->buildJson = '{}'; |
|
94
|
|
|
} |
|
95
|
1 |
|
return $this->buildJson = json_encode($data); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
private function objectToArray($data) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$result = array(); |
|
101
|
|
|
|
|
102
|
1 |
|
foreach ($data as $key => $value) { |
|
103
|
1 |
|
if (gettype($value) == 'object') |
|
104
|
1 |
|
{ |
|
105
|
1 |
|
$result[$key] = $this->objectToArray($value); |
|
106
|
1 |
|
} else{ |
|
107
|
1 |
|
if (gettype($value) != 'object') |
|
108
|
1 |
|
{ |
|
109
|
1 |
|
$result[$key] = $value; |
|
110
|
1 |
|
} |
|
111
|
|
|
// TODO not smog to find a similar case |
|
112
|
|
|
/* else{ |
|
|
|
|
|
|
113
|
|
|
$result[$key] = get_object_vars($value); |
|
114
|
|
|
}*/ |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
1 |
|
} |
|
118
|
1 |
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
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..