PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.91.6
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.91.6
2.1.0 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 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Foundation / RequestGuard.php

RequestGuard.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.91.6, 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 FluentBoards\Framework\Foundation;
4
5 use FluentBoards\Framework\Foundation\App;
6 use FluentBoards\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 FluentBoards\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 FluentBoards\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