PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.11.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.11.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Foundation / RequestGuard.php

RequestGuard.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.11.0, at vendor/wpfluent/framework/src/WPFluent/Foundation/RequestGuard.php

171 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Foundation;
4
5 use FluentCommunity\Framework\Foundation\App;
6 use FluentCommunity\Framework\Validator\ValidationException;
7
8 /**
9 * @property \FluentCommunity\Framework\Http\Request\Request $request
10 */
11 abstract class RequestGuard
12 {
13 /**
14 * The request instance.
15 * @var \FluentCommunity\Framework\Http\Request\Request
16 */
17 protected $request;
18
19 /**
20 * Retrive the validation rules
21 * @return array
22 */
23 public function rules()
24 {
25 return [];
26 }
27
28 /**
29 * Retrive the validation messages set by the developer
30 * @return array
31 */
32 public function messages()
33 {
34 return [];
35 }
36
37 /**
38 * Allow the developer tinker with data before the validation.
39 * @return array
40 */
41 public function beforeValidation()
42 {
43 return [];
44 }
45
46 /**
47 * Allow the developer tinker with data after the validation.
48 * @return array
49 */
50 public function afterValidation($validator)
51 {
52 return [];
53 }
54
55 /**
56 * Validate the request.
57 *
58 * @param array $rules Optional
59 * @param array $messages Optional
60 * @return array Request Data
61 * @throws \FluentCommunity\Framework\Validator\ValidationException
62 */
63 public function validate($rules = [], $messages = [])
64 {
65 try {
66 return $this->request->validate(
67 $rules ?: (array) $this->rules(),
68 $messages ?: (array) $this->messages()
69 );
70 } catch (ValidationException $e) {
71
72 $validator = App::make('validator');
73
74 $validator->addError($e->errors());
75
76 if (!($errors = $validator->errors())) {
77 return $this->afterValidation($validator);
78 }
79
80 $e = new ValidationException(
81 'Unprocessable Entity!', 422, null, $e->errors()
82 );
83
84 if ($this->shouldThrowException()) {
85 throw $e;
86 } else {
87 App::make()->doCustomAction('handle_exception', $e);
88 }
89
90 return [];
91 }
92 }
93
94 /**
95 * Check if exceptions should be thrown.
96 *
97 * @return bool
98 */
99 protected function shouldThrowException()
100 {
101 $isTrue = $this->isRest();
102 $isTrue = $isTrue || str_contains(App::make()->env(), 'test');
103 $isTrue = $isTrue || str_contains(strtolower(php_sapi_name()), 'cli');
104 return $isTrue;
105 }
106
107 /**
108 * Handles validation including before and after calls
109 *
110 * @throws \FluentCommunity\Framework\Validator\ValidationException
111 * @return void
112 */
113 public static function applyValidation()
114 {
115 $instance = new static;
116
117 $request = App::make('request');
118
119 $request->merge($instance->beforeValidation());
120
121 $instance->validate();
122
123 $request->merge($instance->afterValidation(App::make('validator')));
124 }
125
126 /**
127 * Get an input element from the request.
128 *
129 * @param string $key
130 * @return mixed
131 */
132 public function __get($key)
133 {
134 return $this->get($key);
135 }
136
137 /**
138 * Set an input element to the request.
139 *
140 * @param string $key
141 * @return mixed
142 */
143 public function __set($key, $value)
144 {
145 return $this->set($key, $value);
146 }
147
148 /**
149 * Set the request instance.
150 *
151 * @param \FluentCommunity\Framework\Http\Request\Request $request
152 */
153 public function setRequestInstance($request)
154 {
155 $this->request = $request;
156 }
157
158 /**
159 * Handle the dynamic method calls
160 * @param string $method
161 * @param array $params
162 * @return mixed
163 */
164 public function __call($method, $params)
165 {
166 return call_user_func_array(
167 [$this->request, $method], $params
168 );
169 }
170 }
171