PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.20
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.20
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Http / Controller.php

Controller.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.20, at vendor/wpfluent/framework/src/WPFluent/Http/Controller.php

159 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 FluentBoards\Framework\Http;
4
5 use WP_REST_Response;
6 use ReflectionException;
7 use FluentBoards\Framework\Foundation\App;
8 use FluentBoards\Framework\Validator\ValidationException;
9
10 abstract class Controller
11 {
12 /**
13 * Application Instance
14 * @var \FluentBoards\Framework\Foundation\Application
15 */
16 protected $app = null;
17
18 /**
19 * Request Instane
20 * @var \FluentBoards\Framework\Request\Request
21 */
22 protected $request = null;
23
24 /**
25 * Response Instane
26 * @var \FluentBoards\Framework\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 $validator = $this->app->validator->make($data, $rules, $messages);
57
58 if ($validator->validate()->fails()) {
59 throw new ValidationException(
60 'Unprocessable Entity!', 422, null, $validator->errors()
61 );
62 }
63
64 $this->__validated = $this->request->validated($validator->validated());
65
66 return $data;
67
68 } catch (ValidationException $e) {
69
70 if (defined('REST_REQUEST') && REST_REQUEST) {
71 throw $e;
72 };
73
74 $this->app->doCustomAction('handle_exception', $e);
75 }
76 }
77
78 /**
79 * Get the valid data after validation has been passed.
80 *
81 * @return array
82 */
83 public function validated()
84 {
85 return (array) $this->__validated;
86 }
87
88 /**
89 * Send json response
90 * @param array $data
91 * @param integer $code
92 * @return string|false The JSON encoded string, or false if it cannot be encoded.
93 */
94 public function json($data = [], $code = 200)
95 {
96 return $this->response->json($data, $code);
97 }
98
99 /**
100 * Send json response
101 * @param array $data
102 * @param integer $code
103 * @return \WP_REST_Response
104 */
105 public function response($data = [], $code = 200)
106 {
107 return $this->send($data, $code);
108 }
109
110 /**
111 * Send json response
112 * @param array $data
113 * @param integer $code
114 * @return \WP_REST_Response
115 */
116 public function send($data = [], $code = 200)
117 {
118 return $this->response->send($data, $code);
119 }
120
121 /**
122 * Send a success json response
123 * @param array $data
124 * @param integer $code
125 * @return \WP_REST_Response
126 */
127 public function sendSuccess($data = [])
128 {
129 return $this->response->sendSuccess($data, 200);
130 }
131
132 /**
133 * Send an error json response
134 * @param array $data
135 * @param integer $code
136 * @return \WP_REST_Response
137 */
138 public function sendError($data = [], $code = 422)
139 {
140 return $this->response->sendError($data, $code);
141 }
142
143 /**
144 * Dynamically Access components from container
145 * @param string $key
146 * @return mixed
147 * @throws ReflectionException
148 */
149 public function __get($key)
150 {
151 try {
152 return App::getInstance($key);
153 } catch(ReflectionException $e) {
154 $class = get_class($this);
155 wp_die("Undefined property {$key} in $class");
156 }
157 }
158 }
159