Passed
Pull Request — master (#48)
by
unknown
02:06
created

RedirectionForm::getDefaultViewRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Shetabit\Multipay;
4
5
use JsonSerializable;
6
use Illuminate\Support\Facades\Response;
7
8
class RedirectionForm implements JsonSerializable
9
{
10
    /**
11
     * Form's method
12
     *
13
     * @var string
14
     */
15
    protected $method = 'POST';
16
17
    /**
18
     * Form's inputs
19
     *
20
     * @var array
21
     */
22
    protected $inputs = [];
23
24
    /**
25
     * Form's action
26
     *
27
     * @var string
28
     */
29
    protected $action;
30
31
    /**
32
     * Redirection form view's path
33
     *
34
     * @var string
35
     */
36
    protected static $viewPath;
37
38
    /**
39
     * The callable function that renders the given view
40
     *
41
     * @var callable
42
     */
43
    protected static $viewRenderer;
44
45
    /**
46
     * Redirection form constructor.
47
     *
48
     * @param string $action
49
     * @param array $inputs
50
     * @param string $method
51
     */
52
    public function __construct(string $action, array $inputs = [], string $method = 'POST')
53
    {
54
        $this->action = $action;
55
        $this->inputs = $inputs;
56
        $this->method = $method;
57
    }
58
59
    /**
60
     * Retrieve default view path.
61
     *
62
     * @return string
63
     */
64
    public static function getDefaultViewPath() : string
65
    {
66
        return dirname(__DIR__).'/resources/views/redirect-form.php';
67
    }
68
69
    /**
70
     * Set view path
71
     *
72
     * @param string $path
73
     *
74
     * @return void
75
     */
76
    public static function setViewPath(string $path)
77
    {
78
        static::$viewPath = $path;
79
    }
80
81
    /**
82
     * Retrieve view path.
83
     *
84
     * @return string
85
     */
86
    public static function getViewPath() : string
87
    {
88
        return static::$viewPath ?? static::getDefaultViewPath();
89
    }
90
91
    /**
92
     * Set view renderer
93
     *
94
     * @param callable $renderer
95
     */
96
    public static function setViewRenderer(callable $renderer)
97
    {
98
        static::$viewRenderer = $renderer;
99
    }
100
101
    /**
102
     * Retrieve default view renderer.
103
     *
104
     * @return callable
105
     */
106
    protected function getDefaultViewRenderer() : callable
107
    {
108
        return function (string $view, string $action, array $inputs, string $method) {
109
            ob_start();
110
111
            require($view);
112
113
            return ob_get_clean();
114
        };
115
    }
116
117
    /**
118
     * Retrieve associated method.
119
     *
120
     * @return string
121
     */
122
    public function getMethod() : string
123
    {
124
        return $this->method;
125
    }
126
127
    /**
128
     * Retrieve associated inputs
129
     *
130
     * @return array
131
     */
132
    public function getInputs() : array
133
    {
134
        return $this->inputs;
135
    }
136
137
    /**
138
     * Retrieve associated action
139
     *
140
     * @return string
141
     */
142
    public function getAction() : string
143
    {
144
        return $this->action;
145
    }
146
147
    /**
148
     * Alias for getAction method.
149
     *
150
     * @alias getAction
151
     *
152
     * @return string
153
     */
154
    public function getUrl() : string
155
    {
156
        return $this->getAction();
157
    }
158
159
    /**
160
     * Render form.
161
     *
162
     * @return string
163
     */
164
    public function render() : string
165
    {
166
        $data = [
167
            "view" => static::getViewPath(),
168
            "action" => $this->getAction(),
169
            "inputs" => $this->getInputs(),
170
            "method" => $this->getMethod(),
171
        ];
172
173
        $renderer = is_callable(static::$viewRenderer) ? static::$viewRenderer : $this->getDefaultViewRenderer();
174
175
        return call_user_func_array($renderer, $data);
176
    }
177
178
    /**
179
     * Retrieve JSON format of redirection form.
180
     *
181
     * @param $options
182
     *
183
     * @return \Illuminate\Http\JsonResponse
184
     */
185
    public function toJson($options = JSON_UNESCAPED_UNICODE) : : \Illuminate\Http\JsonResponse
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ':' on line 185 at column 64
Loading history...
186
    {
187
        return Response::json($this)->setEncodingOptions($options);
188
    }
189
190
    /**
191
     * Retrieve JSON format of redirection form.
192
     *
193
     * @return string
194
     */
195
    public function toString() : string
196
    {
197
        return $this->render();
198
    }
199
200
    /**
201
     * Serialize to json
202
     *
203
     * @return mixed
204
     */
205
    public function jsonSerialize()
206
    {
207
        return [
208
            'action' => $this->getAction(),
209
            'inputs' => $this->getInputs(),
210
            'method' => $this->getMethod(),
211
        ];
212
    }
213
214
    /**
215
     * Retrieve string format of redirection form.
216
     *
217
     * @return string
218
     */
219
    public function __toString()
220
    {
221
        return $this->toString();
222
    }
223
}
224