PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.25
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.25
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / vendor / wpfluent / framework / src / WPFluent / Http / Controller.php

Controller.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.25, 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 FluentCart\Framework\Http;
4
5 use WP_REST_Response;
6 use ReflectionException;
7 use FluentCart\Framework\Foundation\App;
8 use FluentCart\Framework\Validator\ValidationException;
9
10 abstract class Controller
11 {
12 /**
13 * Application Instance
14 * @var \FluentCart\Framework\Foundation\Application
15 */
16 protected $app = null;
17
18 /**
19 * Request Instane
20 * @var \FluentCart\Framework\Http\Request\Request
21 */
22 protected $request = null;
23
24 /**
25 * Response Instane
26 * @var \FluentCart\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