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