1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexWells\ApiDocsGenerator\Parsers; |
4
|
|
|
|
5
|
|
|
use AlexWells\ApiDocsGenerator\Helpers; |
6
|
|
|
|
7
|
|
|
class ResponseParser extends AbstractStepTransformer |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Steps that should be taken to transform the string from raw to end result. |
11
|
|
|
* |
12
|
|
|
* @return array |
13
|
|
|
*/ |
14
|
|
|
protected static function getTransformerSteps() |
15
|
|
|
{ |
16
|
|
|
return ['newlines', 'shortArray', 'keyNames', 'varsOfType', 'types', 'repeatedObject', 'decode']; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Replace \n and similar symbols with nothing. |
21
|
|
|
* |
22
|
|
|
* @param $content |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
protected static function transformNewlines($content) |
27
|
|
|
{ |
28
|
|
|
return Helpers::clearNewlines($content); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Replace `int[]` with `[ :: int ]`. |
33
|
|
|
* |
34
|
|
|
* @param $content |
35
|
|
|
* |
36
|
|
|
* @return string|null |
37
|
|
|
*/ |
38
|
|
|
protected static function transformShortArray($content) |
39
|
|
|
{ |
40
|
|
|
return preg_replace(Helpers::regexExcludeInQuotes("(\w+)\[\]"), '[ :: $1 ]', $content); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Replace `nested: {}` with `"nested": {}`. |
45
|
|
|
* |
46
|
|
|
* @param $content |
47
|
|
|
* |
48
|
|
|
* @return string|null |
49
|
|
|
*/ |
50
|
|
|
protected static function transformKeyNames($content) |
51
|
|
|
{ |
52
|
|
|
return preg_replace(Helpers::regexExcludeInQuotes("(\w+)\s*:[^:]"), '"$1": ', $content); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Replace `year :: int` with `"year": {"$ref": "int"}`. |
57
|
|
|
* |
58
|
|
|
* @param $content |
59
|
|
|
* |
60
|
|
|
* @return string|null |
61
|
|
|
*/ |
62
|
|
|
protected static function transformVarsOfType($content) |
63
|
|
|
{ |
64
|
|
|
return preg_replace(Helpers::regexExcludeInQuotes("(\w+)\s*::\s*(\w+)"), '"$1": {"$ref": "$2"}', $content); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Replace `:: int` with `{"$ref": "int"}`. |
69
|
|
|
* |
70
|
|
|
* @param $content |
71
|
|
|
* |
72
|
|
|
* @return string|null |
73
|
|
|
*/ |
74
|
|
|
protected static function transformTypes($content) |
75
|
|
|
{ |
76
|
|
|
return preg_replace(Helpers::regexExcludeInQuotes("\s*::\s*(\w+)"), '{"$ref": "$1"}', $content); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Replace `:: {}` with `{"$ref": {}}`. |
81
|
|
|
* |
82
|
|
|
* @param $content |
83
|
|
|
* |
84
|
|
|
* @return string|null |
85
|
|
|
*/ |
86
|
|
|
protected static function transformRepeatedObject($content) |
87
|
|
|
{ |
88
|
|
|
return preg_replace(Helpers::regexExcludeInQuotes("::\s*{(.*)}"), '{"$ref": {$1}}', $content); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Decode JSON string into PHP array. |
93
|
|
|
* |
94
|
|
|
* @param $content |
95
|
|
|
* |
96
|
|
|
* @return array|null |
97
|
|
|
*/ |
98
|
|
|
protected static function transformDecode($content) |
99
|
|
|
{ |
100
|
|
|
return json_decode($content, true); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|