-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: filter values are not validated (#3795)
- Loading branch information
Showing
27 changed files
with
215 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* For detailed copyright and license information, please view the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\Filter; | ||
|
||
use Flarum\Foundation\ValidationException as FlarumValidationException; | ||
use Flarum\Locale\Translator; | ||
|
||
trait ValidateFilterTrait | ||
{ | ||
/** | ||
* @throws FlarumValidationException | ||
* @return array<string>|array<array> | ||
*/ | ||
protected function asStringArray($filterValue, bool $multidimensional = false): array | ||
{ | ||
if (is_array($filterValue)) { | ||
$value = array_map(function ($subValue) use ($multidimensional) { | ||
if (is_array($subValue) && ! $multidimensional) { | ||
$this->throwValidationException('core.api.invalid_filter_type.must_not_be_multidimensional_array_message'); | ||
} elseif (is_array($subValue)) { | ||
return $this->asStringArray($subValue, true); | ||
} else { | ||
return $this->asString($subValue); | ||
} | ||
}, $filterValue); | ||
} else { | ||
$value = explode(',', $this->asString($filterValue)); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
*/ | ||
protected function asString($filterValue): string | ||
{ | ||
if (is_array($filterValue)) { | ||
$this->throwValidationException('core.api.invalid_filter_type.must_not_be_array_message'); | ||
} | ||
|
||
return trim($filterValue, '"'); | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
*/ | ||
protected function asInt($filterValue): int | ||
{ | ||
if (! is_numeric($filterValue)) { | ||
$this->throwValidationException('core.api.invalid_filter_type.must_be_numeric_message'); | ||
} | ||
|
||
return (int) $this->asString($filterValue); | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
* @return array<int> | ||
*/ | ||
protected function asIntArray($filterValue): array | ||
{ | ||
return array_map(function ($value) { | ||
return $this->asInt($value); | ||
}, $this->asStringArray($filterValue)); | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
*/ | ||
protected function asBool($filterValue): bool | ||
{ | ||
return $this->asString($filterValue) === '1'; | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
*/ | ||
private function throwValidationException(string $messageCode): void | ||
{ | ||
$translator = resolve(Translator::class); | ||
|
||
throw new FlarumValidationException([ | ||
'message' => $translator->trans($messageCode, ['{filter}' => $this->getFilterKey()]), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.