1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\RestfulServer\DataFormatter; |
4
|
|
|
|
5
|
|
|
use SilverStripe\View\ArrayData; |
6
|
|
|
use SilverStripe\Core\Convert; |
7
|
|
|
use SilverStripe\RestfulServer\DataFormatter; |
8
|
|
|
use SilverStripe\ORM\DataObjectInterface; |
9
|
|
|
use SilverStripe\Control\Director; |
10
|
|
|
use SilverStripe\ORM\SS_List; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Formats a DataObject's member fields into a JSON string |
14
|
|
|
*/ |
15
|
|
|
class JSONDataFormatter extends DataFormatter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @config |
19
|
|
|
* @todo pass this from the API to the data formatter somehow |
20
|
|
|
*/ |
21
|
|
|
private static $api_base = "api/v1/"; |
22
|
|
|
|
23
|
|
|
protected $outputContentType = 'application/json'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function supportedExtensions() |
29
|
|
|
{ |
30
|
|
|
return array( |
31
|
|
|
'json', |
32
|
|
|
'js' |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function supportedMimeTypes() |
40
|
|
|
{ |
41
|
|
|
return array( |
42
|
|
|
'application/json', |
43
|
|
|
'text/x-json' |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $array |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function convertArray($array) |
52
|
|
|
{ |
53
|
|
|
return Convert::array2json($array); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Generate a JSON representation of the given {@link DataObject}. |
|
|
|
|
58
|
|
|
* |
59
|
|
|
* @param DataObject $obj The object |
60
|
|
|
* @param Array $fields If supplied, only fields in the list will be returned |
61
|
|
|
* @param $relations Not used |
|
|
|
|
62
|
|
|
* @return String JSON |
63
|
|
|
*/ |
64
|
|
|
public function convertDataObject(DataObjectInterface $obj, $fields = null, $relations = null) |
65
|
|
|
{ |
66
|
|
|
return Convert::array2json($this->convertDataObjectToJSONObject($obj, $fields, $relations)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Internal function to do the conversion of a single data object. It builds an empty object and dynamically |
71
|
|
|
* adds the properties it needs to it. If it's done as a nested array, json_encode or equivalent won't use |
72
|
|
|
* JSON object notation { ... }. |
73
|
|
|
* @param DataObjectInterface $obj |
74
|
|
|
* @param $fields |
75
|
|
|
* @param $relations |
76
|
|
|
* @return EmptyJSONObject |
|
|
|
|
77
|
|
|
*/ |
78
|
|
|
public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields = null, $relations = null) |
79
|
|
|
{ |
80
|
|
|
$className = get_class($obj); |
81
|
|
|
$id = $obj->ID; |
82
|
|
|
|
83
|
|
|
$serobj = ArrayData::array_to_object(); |
84
|
|
|
|
85
|
|
|
foreach ($this->getFieldsForObj($obj) as $fieldName => $fieldType) { |
86
|
|
|
// Field filtering |
87
|
|
|
if ($fields && !in_array($fieldName, $fields)) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$fieldValue = $obj->obj($fieldName)->forTemplate(); |
92
|
|
|
$serobj->$fieldName = $fieldValue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($this->relationDepth > 0) { |
96
|
|
|
foreach ($obj->hasOne() as $relName => $relClass) { |
97
|
|
|
if (!singleton($relClass)->stat('api_access')) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Field filtering |
102
|
|
|
if ($fields && !in_array($relName, $fields)) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
if ($this->customRelations && !in_array($relName, $this->customRelations)) { |
|
|
|
|
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$fieldName = $relName . 'ID'; |
110
|
|
|
if ($obj->$fieldName) { |
111
|
|
|
$href = Director::absoluteURL($this->config()->api_base . "$relClass/" . $obj->$fieldName); |
112
|
|
|
} else { |
113
|
|
|
$href = Director::absoluteURL($this->config()->api_base . "$className/$id/$relName"); |
114
|
|
|
} |
115
|
|
|
$serobj->$relName = ArrayData::array_to_object(array( |
116
|
|
|
"className" => $relClass, |
117
|
|
|
"href" => "$href.json", |
118
|
|
|
"id" => $obj->$fieldName |
119
|
|
|
)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
foreach ($obj->hasMany() + $obj->manyMany() as $relName => $relClass) { |
123
|
|
|
//remove dot notation from relation names |
124
|
|
|
$parts = explode('.', $relClass); |
125
|
|
|
$relClass = array_shift($parts); |
126
|
|
|
|
127
|
|
|
if (!singleton($relClass)->stat('api_access')) { |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Field filtering |
132
|
|
|
if ($fields && !in_array($relName, $fields)) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
if ($this->customRelations && !in_array($relName, $this->customRelations)) { |
|
|
|
|
136
|
|
|
continue; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$innerParts = array(); |
140
|
|
|
$items = $obj->$relName(); |
141
|
|
|
foreach ($items as $item) { |
142
|
|
|
//$href = Director::absoluteURL($this->config()->api_base . "$className/$id/$relName/$item->ID"); |
|
|
|
|
143
|
|
|
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID"); |
144
|
|
|
$innerParts[] = ArrayData::array_to_object(array( |
145
|
|
|
"className" => $relClass, |
146
|
|
|
"href" => "$href.json", |
147
|
|
|
"id" => $item->ID |
148
|
|
|
)); |
149
|
|
|
} |
150
|
|
|
$serobj->$relName = $innerParts; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $serobj; |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Generate a JSON representation of the given {@link SS_List}. |
159
|
|
|
* |
160
|
|
|
* @param SS_List $set |
161
|
|
|
* @return String XML |
162
|
|
|
*/ |
163
|
|
|
public function convertDataObjectSet(SS_List $set, $fields = null) |
164
|
|
|
{ |
165
|
|
|
$items = array(); |
166
|
|
|
foreach ($set as $do) { |
167
|
|
|
if (!$do->canView()) { |
168
|
|
|
continue; |
169
|
|
|
} |
170
|
|
|
$items[] = $this->convertDataObjectToJSONObject($do, $fields); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$serobj = ArrayData::array_to_object(array( |
174
|
|
|
"totalSize" => (is_numeric($this->totalSize)) ? $this->totalSize : null, |
|
|
|
|
175
|
|
|
"items" => $items |
176
|
|
|
)); |
177
|
|
|
|
178
|
|
|
return Convert::array2json($serobj); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $strData |
183
|
|
|
* @return array|bool|void |
184
|
|
|
*/ |
185
|
|
|
public function convertStringToArray($strData) |
186
|
|
|
{ |
187
|
|
|
return Convert::json2array($strData); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths