Skip to content

Commit d635c28

Browse files
author
Admin
committed
Refactor getFiltered for symfony 7.x
1 parent abb97bd commit d635c28

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

config/laravel_crud_wizard.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
return [
44
'LIVE_MODE' => \env('LIVE_MODE'),
5+
'REQUEST_GET_FILTERED_MACRO_SHOULD_THROW_ON_FAILURE' =>
6+
\env('REQUEST_GET_FILTERED_MACRO_SHOULD_THROW_ON_FAILURE', false),
57
];

src/Providers/CrudProvider.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function (...$arguments): Model {
7373
Request::macro(
7474
'getFiltered',
7575
/**
76-
* @param string|int $filter can be 'alpha', 'alnum', 'digits', 'int', 'boolean' or an int flag
76+
* @param string|int $filter can be 'alpha', 'alnum', 'digits' or an int flag
7777
* @see ParameterBag::filter(), \filter_var() for $filter and $options
7878
* @see ParameterBag::getAlpha()
7979
* @see ParameterBag::getAlnum()
@@ -89,16 +89,28 @@ function (
8989
): mixed {
9090
/** @var Request $this */
9191
if (\is_int($filter)) {
92-
if ($this->attributes->has($key)) {
93-
return $this->attributes->filter($key, $default, $filter, $options);
94-
}
95-
96-
if ($this->query->has($key)) {
97-
return $this->query->filter($key, $default, $filter, $options);
98-
}
99-
100-
if ($this->request->has($key)) {
101-
return $this->request->filter($key, $default, $filter, $options);
92+
try {
93+
if ($this->attributes->has($key)) {
94+
return $this->attributes->filter($key, $default, $filter, $options);
95+
}
96+
97+
if ($this->query->has($key)) {
98+
return $this->query->filter($key, $default, $filter, $options);
99+
}
100+
101+
if ($this->request->has($key)) {
102+
return $this->request->filter($key, $default, $filter, $options);
103+
}
104+
} catch (\UnexpectedValueException $e) {
105+
if (\config('crud.REQUEST_GET_FILTERED_MACRO_SHOULD_THROW_ON_FAILURE', false)) {
106+
throw $e;
107+
}
108+
109+
if (!\is_array($options) && $options) {
110+
$options = ['flags' => $options];
111+
}
112+
113+
return ($options['flags'] ?? 0) & \FILTER_NULL_ON_FAILURE ? null : false;
102114
}
103115

104116
return $default;
@@ -107,7 +119,7 @@ function (
107119
if ($filter !== '') {
108120
$filter = \ucfirst(\strtolower($filter));
109121

110-
if (!\in_array($filter, ['Alpha', 'Alnum', 'Digits', 'Int', 'Boolean'], true)) {
122+
if (!\in_array($filter, ['Alpha', 'Alnum', 'Digits'], true)) {
111123
$filter = '';
112124
}
113125
}
@@ -122,8 +134,10 @@ function (
122134
if ($this->request->has($key)) {
123135
return $this->request->{'get' . $filter}($key, $default);
124136
}
137+
} catch (\UnexpectedValueException) {
125138
} catch (\Throwable $e) {
126-
Log::error($e->getMessage(), $e->getTrace());
139+
Log::error(__FILE__ . ':' . __LINE__ . ' Request::getFiltered macro error: ' .
140+
$e->getMessage() . ' for: ' . \json_encode(\func_get_args()), $e->getTrace());
127141
}
128142

129143
return $default;

0 commit comments

Comments
 (0)