You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added Transformer and Transformation feature. Included unit tests for Transformation classes. Updated CHANGELOG and README to reflect this change. Fixes#2
Copy file name to clipboardexpand all lines: README.md
+140-5
Original file line number
Diff line number
Diff line change
@@ -8,12 +8,11 @@ This is an extension for Symphony CMS. Add it to your `/extensions` folder in yo
8
8
9
9
### Requirements
10
10
11
-
This extension requires the **[Symfony HTTP Foundation](https://github.com/symfony/http-foundation)** (`symfony/http-foundation`) and **[Symphony PDO](https://github.com/pointybeard/symphony-pdo)** (`pointybeard/symphony-pdo`) to be installed via Composer. Either require both of these in your main composer.json file, or run `composer install` on the `extension/api_framework` directory.
11
+
This extension requires the **[Symfony HTTP Foundation](https://github.com/symfony/http-foundation)** (`symfony/http-foundation`) to be installed via Composer. Either require both of these in your main composer.json file, or run `composer install` on the `extension/api_framework` directory.
12
12
13
13
"require": {
14
14
"php": ">=5.6.6",
15
-
"symfony/http-foundation": "^3.0@dev",
16
-
"pointybeard/symphony-pdo": "~0.1"
15
+
"symfony/http-foundation": "^3.0@dev"
17
16
}
18
17
19
18
## Usage
@@ -113,8 +112,6 @@ Would result in the following JSON
113
112
}
114
113
```
115
114
116
-
**Note that when there is a single, in the case, `<entry>` element, the a JSON array is not produced. This is a known limitation (see [https://github.com/pointybeard/api_framework/issues/2](https://github.com/pointybeard/api_framework/issues/2))**
117
-
118
115
### Controller Event
119
116
120
117
Use the `API Framework: Controller` event to listen for PUT, POST, PATCH and DELETE requests. To create your own controller, make a folder called `controllers` in your `/workspace` directory.
@@ -240,3 +237,141 @@ Here is an example of a completed controller:
240
237
return $this->render($response, $output);
241
238
}
242
239
}
240
+
241
+
### Transformers
242
+
243
+
Prior to converting the XML into JSON, transformers are run over it. Transformers mutate the result based on a test and action.
244
+
245
+
`@jsonForceArray`
246
+
247
+
This transformation will look for the attribute `jsonForceArray` on any XML elements. If it is set to "true", this transformation is applied. It relates to **[#issue-2](https://github.com/pointybeard/api_framework/issues/2)**. When there are multiple elements of the same name, for example 'entry', the JSON encode process will treat these as an array. E.g.
248
+
249
+
```
250
+
<data>
251
+
<entries>
252
+
<entry>
253
+
<id>2</id>
254
+
<title>Another Entry</title>
255
+
</entry>
256
+
<entry>
257
+
<id>1</id>
258
+
<title>An Entry</title>
259
+
</entry>
260
+
</entries>
261
+
</data>
262
+
```
263
+
264
+
becomes
265
+
266
+
267
+
```
268
+
{
269
+
"entries": {
270
+
"entry": [
271
+
{
272
+
"id": "2",
273
+
"title": "Another Entry",
274
+
},
275
+
{
276
+
"id": "1",
277
+
"title": "An Entry",
278
+
}
279
+
]
280
+
}
281
+
}
282
+
```
283
+
284
+
However, if there is only a single 'entry' element, it is treated as an object. This is because internally it is just an associtive array, not an indexed array of 'entry' objects. E.g.
285
+
286
+
```
287
+
<data>
288
+
<entries>
289
+
<entry>
290
+
<id>2</id>
291
+
<title>Another Entry</title>
292
+
</entry>
293
+
</entries>
294
+
</data>
295
+
```
296
+
297
+
results in
298
+
299
+
```
300
+
{
301
+
"entries": {
302
+
"entry": {
303
+
"id": "1",
304
+
"title": "An Entry",
305
+
}
306
+
}
307
+
}
308
+
```
309
+
310
+
Notice that 'entry' is a JSON object. The problem with this is inconsistent data. It changes depending on how many entries are present. The solution is to set `jsonForceArray="true"` on the 'entry' element to trigger the transformation:
311
+
312
+
```
313
+
<data>
314
+
<entries>
315
+
<entry jsonForceArray="true">
316
+
<id>2</id>
317
+
<title>Another Entry</title>
318
+
</entry>
319
+
</entries>
320
+
</data>
321
+
```
322
+
323
+
Which results in JSON
324
+
325
+
```
326
+
{
327
+
"entries": {
328
+
"entry": [
329
+
{
330
+
"id": "2",
331
+
"title": "Another Entry",
332
+
}
333
+
]
334
+
}
335
+
}
336
+
```
337
+
338
+
### Creating new Transformers
339
+
340
+
This extention provides the delegate `APIFrameworkJSONRendererAppendTransformations` on all frontend pages with the `JSON` type. The context includes an instance of `Lib\Transformer`. Use the `append()` method to add your own transformations. E.g.
0 commit comments