Completed
Push — master ( c6a664...3473b8 )
by Alex
05:09
created

ResponseParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
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