PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.92
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.92
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 1.0.92, at vendor/wpfluent/framework/src/WPFluent/Foundation/RequestGuard.php

141 lines 3.2 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 abstract class RequestGuard
9 {
10 /**
11 * Retrive the validation rules
12 * @return array
13 */
14 public function rules()
15 {
16 return [];
17 }
18
19 /**
20 * Retrive the validation messages set by the developer
21 * @return array
22 */
23 public function messages()
24 {
25 return [];
26 }
27
28 /**
29 * Allow the developer tinker with data before the validation.
30 * @return array
31 */
32 public function beforeValidation()
33 {
34 return [];
35 }
36
37 /**
38 * Allow the developer tinker with data after the validation.
39 * @return array
40 */
41 public function afterValidation($validator)
42 {
43 return [];
44 }
45
46 /**
47 * Validate the request.
48 *
49 * @param array $rules Optional
50 * @param array $messages Optional
51 * @return array Request Data
52 * @throws FluentCommunity\Framework\Validator\ValidationException
53 */
54 public function validate($rules = [], $messages = [])
55 {
56 try {
57 return App::make('request')->validate(
58 $rules ?: (array) $this->rules(),
59 $messages ?: (array) $this->messages()
60 );
61 } catch (ValidationException $e) {
62
63 $validator = App::make('validator');
64
65 $validator->addError($e->errors());
66
67 $after = $this->afterValidation($validator);
68
69 if (!($errors = $validator->errors())) {
70 return;
71 }
72
73 $e = new ValidationException(
74 'Unprocessable Entity!', 422, null, $e->errors()
75 );
76
77 if ($this->shouldThrowException()) {
78 throw $e;
79 } else {
80 App::make()->doCustomAction('handle_exception', $e);
81 }
82 }
83 }
84
85 /**
86 * Check if exceptions should be thrown.
87 *
88 * @return bool
89 */
90 protected function shouldThrowException()
91 {
92 $isTrue = $this->isRest();
93 $isTrue = $isTrue || str_contains('test', App::make()->env());
94 $isTrue = $isTrue || str_contains(strtolower(php_sapi_name()), 'cli');
95 return $isTrue;
96 }
97
98 /**
99 * Handles validation including before and after calls
100 *
101 * @throws FluentCommunity\Framework\Validator\ValidationException
102 * @return null
103 */
104 public static function applyValidation()
105 {
106 $instance = new static;
107
108 $request = App::getInstance('request');
109
110 $request->merge($instance->beforeValidation());
111
112 $instance->validate();
113
114 $request->merge($instance->afterValidation());
115 }
116
117 /**
118 * Get an input element from the request.
119 *
120 * @param string $key
121 * @return mixed
122 */
123 public function __get($key)
124 {
125 return $this->get($key);
126 }
127
128 /**
129 * Handle the dynamic method calls
130 * @param string $method
131 * @param array $params
132 * @return mixed
133 */
134 public function __call($method, $params)
135 {
136 return call_user_func_array(
137 [App::make('request'), $method], $params
138 );
139 }
140 }
141