Passed
Pull Request — master (#9)
by
unknown
02:25
created

RedirectionForm::getInputs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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