PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.2
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / vendor / wpfluent / framework / src / WPFluent / Http / Controller.php

Controller.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.2, at vendor/wpfluent/framework/src/WPFluent/Http/Controller.php

160 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\Framework\Http;
4
5 use WP_REST_Response;
6 use ReflectionException;
7 use FluentForm\Framework\Foundation\App;
8 use FluentForm\Framework\Validator\ValidationException;
9
10 abstract class Controller
11 {
12 /**
13 * Application Instance
14 * @var \FluentForm\Framework\Foundation\Application
15 */
16 protected $app = null;
17
18 /**
19 * Request Instane
20 * @var \FluentForm\Framework\Http\Request\Request
21 */
22 protected $request = null;
23
24 /**
25 * Response Instane
26 * @var \FluentForm\Framework\Http\Response\Response
27 */
28 protected $response = null;
29
30 /**
31 * Validated data after validation has been passed
32 * @var array
33 */
34 private $__validated = [];
35
36 /**
37 * Construct the controller instance
38 */
39 public function __construct($app = null)
40 {
41 $this->app = $app ?: App::getInstance();
42 $this->request = $this->app['request'];
43 $this->response = $this->app['response'];
44 }
45
46 /**
47 * Validate the request data
48 * @param array $data
49 * @param array $rules
50 * @param array $messages
51 * @return array
52 */
53 public function validate($data, $rules, $messages = [])
54 {
55 try {
56 // @phpstan-ignore-next-line
57 $validator = $this->app->validator->make($data, $rules, $messages);
58
59 if ($validator->validate()->fails()) {
60 throw new ValidationException(
61 'Unprocessable Entity!', 422, null, $validator->errors()
62 );
63 }
64
65 $this->__validated = $this->request->validated($validator->validated());
66
67 return $data;
68
69 } catch (ValidationException $e) {
70
71 if (defined('REST_REQUEST') && REST_REQUEST) {
72 throw $e;
73 };
74
75 $this->app->doCustomAction('handle_exception', $e);
76 }
77 }
78
79 /**
80 * Get the valid data after validation has been passed.
81 *
82 * @return array
83 */
84 public function validated()
85 {
86 return (array) $this->__validated;
87 }
88
89 /**
90 * Send json response
91 * @param array $data
92 * @param integer $code
93 * @return string|false The JSON encoded string, or false if it cannot be encoded.
94 */
95 public function json($data = [], $code = 200)
96 {
97 return $this->response->json($data, $code);
98 }
99
100 /**
101 * Send json response
102 * @param array $data
103 * @param integer $code
104 * @return \WP_REST_Response
105 */
106 public function response($data = [], $code = 200)
107 {
108 return $this->send($data, $code);
109 }
110
111 /**
112 * Send json response
113 * @param array $data
114 * @param integer $code
115 * @return \WP_REST_Response
116 */
117 public function send($data = [], $code = 200)
118 {
119 return $this->response->send($data, $code);
120 }
121
122 /**
123 * Send a success json response.
124 *
125 * @param array $data
126 * @return \WP_REST_Response
127 */
128 public function sendSuccess($data = [])
129 {
130 return $this->response->sendSuccess($data, 200);
131 }
132
133 /**
134 * Send an error json response
135 * @param array $data
136 * @param integer $code
137 * @return \WP_REST_Response
138 */
139 public function sendError($data = [], $code = 422)
140 {
141 return $this->response->sendError($data, $code);
142 }
143
144 /**
145 * Dynamically Access components from container
146 * @param string $key
147 * @return mixed
148 * @throws ReflectionException
149 */
150 public function __get($key)
151 {
152 try {
153 return App::getInstance($key);
154 } catch(ReflectionException $e) {
155 $class = get_class($this);
156 wp_die("Undefined property {$key} in $class");
157 }
158 }
159 }
160