1 | <?php |
||||
2 | /** |
||||
3 | * Created by PhpStorm. |
||||
4 | * User: alive |
||||
5 | * Date: 10/7/17 |
||||
6 | * Time: 12:19 PM |
||||
7 | */ |
||||
8 | |||||
9 | namespace Alive2212\LaravelRequestHelper; |
||||
10 | |||||
11 | |||||
12 | use Illuminate\Http\JsonResponse; |
||||
0 ignored issues
–
show
|
|||||
13 | use Illuminate\Http\Request; |
||||
0 ignored issues
–
show
The type
Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
14 | use Illuminate\Support\Collection; |
||||
15 | use Illuminate\Support\Facades\Validator; |
||||
16 | use Illuminate\Support\MessageBag; |
||||
17 | |||||
18 | class RequestHelper |
||||
19 | { |
||||
20 | //TODO some method of this class (like uniqueCheck) must be return $this |
||||
21 | /** |
||||
22 | * @var array |
||||
23 | */ |
||||
24 | protected $validatorMethods = ["POST", "PUT", "PATCH"]; |
||||
25 | |||||
26 | /** |
||||
27 | * @var array |
||||
28 | */ |
||||
29 | protected $uniqueFields = []; |
||||
30 | |||||
31 | /** |
||||
32 | * @var null |
||||
33 | */ |
||||
34 | protected $model = null; |
||||
35 | |||||
36 | /** |
||||
37 | * @param Request $request |
||||
38 | * @return array |
||||
39 | */ |
||||
40 | public function getJsonPaginateValues(Request $request) |
||||
41 | { |
||||
42 | $request = $request->toArray(); |
||||
43 | $size = 10; |
||||
44 | $number = 1; |
||||
45 | if (array_key_exists('page', $request)) { |
||||
46 | if (array_key_exists('size', $request['page'])) { |
||||
47 | $size = $request['page']['size']; |
||||
48 | } |
||||
49 | if (array_key_exists('number', $request['page'])) { |
||||
50 | $number = $request['page']['number']; |
||||
51 | } |
||||
52 | } |
||||
53 | return array($size, $number); |
||||
54 | } |
||||
55 | |||||
56 | /** |
||||
57 | * @param $param |
||||
58 | * @return bool |
||||
59 | */ |
||||
60 | public function isJson($param) |
||||
61 | { |
||||
62 | return collect(json_decode($param, true))->count() == 0 ? false : true; |
||||
63 | } |
||||
64 | |||||
65 | /** |
||||
66 | * @param $param |
||||
67 | * @return mixed|string |
||||
68 | */ |
||||
69 | public function getOperatorFromJson($param) |
||||
70 | { |
||||
71 | if (collect(json_decode($param, true))->get('operator') == null) { |
||||
72 | return '='; |
||||
73 | } |
||||
74 | return collect(json_decode($param, true))->get('operator'); |
||||
75 | } |
||||
76 | |||||
77 | /** |
||||
78 | * @param $param |
||||
79 | * @return mixed |
||||
80 | */ |
||||
81 | public function getValueFromJson($param) |
||||
82 | { |
||||
83 | if (collect(json_decode($param, true))->get('value') == null) { |
||||
84 | return json_decode($param, true); |
||||
85 | } |
||||
86 | return collect(json_decode($param, true))->get('value'); |
||||
87 | } |
||||
88 | |||||
89 | /** |
||||
90 | * @param $param |
||||
91 | * @return Collection |
||||
92 | */ |
||||
93 | public function getCollectFromJson($param) |
||||
94 | { |
||||
95 | return collect(json_decode($param, true)); |
||||
96 | } |
||||
97 | |||||
98 | /** |
||||
99 | * @param Request $request |
||||
100 | * @param $validatorArray |
||||
101 | * @param string $message |
||||
102 | * @return JsonResponse|MessageBag|null |
||||
103 | */ |
||||
104 | public function validator(Request $request, $validatorArray, $message = 'validation_fails') |
||||
0 ignored issues
–
show
The parameter
$message is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
105 | { |
||||
106 | $validationErrors = $this->checkRequestValidation($request, $validatorArray); |
||||
107 | if ($validationErrors != null) { |
||||
108 | return $validationErrors; |
||||
109 | } |
||||
110 | return $validationErrors; |
||||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * @param Request $request |
||||
115 | * @param $validationArray |
||||
116 | * @return MessageBag|null |
||||
117 | */ |
||||
118 | public function checkRequestValidation(Request $request, $validationArray) |
||||
119 | { |
||||
120 | $requestParams = $request->toArray(); |
||||
121 | $validator = Validator::make($request->all(), $validationArray); |
||||
122 | if ($validator->fails()) { |
||||
123 | return $validator->errors(); |
||||
124 | } |
||||
125 | if (is_numeric(array_search($request->getMethod(), $this->validatorMethods))) { |
||||
126 | $errors = new MessageBag(); |
||||
127 | foreach ($requestParams as $requestParamKey => $requestParamValue) { |
||||
128 | if (is_numeric(array_search($requestParamKey, $this->uniqueFields))) { |
||||
129 | if ($this->checkExistUniqueRecord($requestParamKey, $requestParamValue)) { |
||||
130 | $errors->add($requestParamKey, 'This ' . $requestParamKey . ' is exist try another.'); |
||||
131 | } |
||||
132 | } |
||||
133 | } |
||||
134 | if (collect($errors)->count() > 0) { |
||||
135 | return $errors; |
||||
136 | } |
||||
137 | } |
||||
138 | return null; |
||||
139 | } |
||||
140 | |||||
141 | /** |
||||
142 | * @param $key |
||||
143 | * @param $value |
||||
144 | * @return bool |
||||
145 | */ |
||||
146 | public function checkExistUniqueRecord($key, $value) |
||||
147 | { |
||||
148 | if (is_null($this->model)) { |
||||
149 | if ($this->model->where($key, $value)->count()) { |
||||
0 ignored issues
–
show
The method
where() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
150 | return true; |
||||
151 | } |
||||
152 | } |
||||
153 | return false; |
||||
154 | } |
||||
155 | |||||
156 | /** |
||||
157 | * @return array |
||||
158 | */ |
||||
159 | public function getUniqueFields() |
||||
160 | { |
||||
161 | return $this->uniqueFields; |
||||
162 | } |
||||
163 | |||||
164 | /** |
||||
165 | * @param array $uniqueFields |
||||
166 | */ |
||||
167 | public function setUniqueFields($uniqueFields) |
||||
168 | { |
||||
169 | $this->uniqueFields = $uniqueFields; |
||||
170 | } |
||||
171 | |||||
172 | /** |
||||
173 | * @return array |
||||
174 | */ |
||||
175 | public function getValidatorMethods() |
||||
176 | { |
||||
177 | return $this->validatorMethods; |
||||
178 | } |
||||
179 | |||||
180 | /** |
||||
181 | * @param array $validatorMethods |
||||
182 | */ |
||||
183 | public function setValidatorMethods($validatorMethods) |
||||
184 | { |
||||
185 | $this->validatorMethods = $validatorMethods; |
||||
186 | } |
||||
187 | |||||
188 | /** |
||||
189 | * @return mixed |
||||
190 | */ |
||||
191 | public function getModel() |
||||
192 | { |
||||
193 | return $this->model; |
||||
194 | } |
||||
195 | |||||
196 | /** |
||||
197 | * @param mixed $model |
||||
198 | */ |
||||
199 | public function setModel($model) |
||||
200 | { |
||||
201 | $this->model = $model; |
||||
202 | } |
||||
203 | |||||
204 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths