From 05f5b0ed34db0e0ed2c4ea804581e1b4a4eb3bdb Mon Sep 17 00:00:00 2001 From: Sarjuuk Date: Mon, 6 Oct 2025 16:55:29 +0200 Subject: [PATCH] Response/Params * so we can't directly use BackedEnum::tryFrom as validator, because if the Enum is of and the string is not what php considers numeric, we get a straight TypeError Exception instead of null for failing the tryFrom. --- endpoints/data/data.php | 2 +- endpoints/guide/edit.php | 2 +- endpoints/locale/locale.php | 2 +- includes/components/response/baseresponse.class.php | 7 +++++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/endpoints/data/data.php b/endpoints/data/data.php index 174e24c2..2376b38c 100644 --- a/endpoints/data/data.php +++ b/endpoints/data/data.php @@ -9,7 +9,7 @@ if (!defined('AOWOW_REVISION')) class DataBaseResponse extends TextResponse { protected array $expectedGET = array( - 'locale' => ['filter' => FILTER_CALLBACK, 'options' => [Locale::class, 'tryFrom'] ], + 'locale' => ['filter' => FILTER_CALLBACK, 'options' => [self::class, 'checkLocale' ]], 't' => ['filter' => FILTER_CALLBACK, 'options' => [self::class, 'checkTextLine' ]], 'catg' => ['filter' => FILTER_VALIDATE_INT ], 'skill' => ['filter' => FILTER_CALLBACK, 'options' => [self::class, 'checkSkill' ]], diff --git a/endpoints/guide/edit.php b/endpoints/guide/edit.php index b2897167..9f7af2b2 100644 --- a/endpoints/guide/edit.php +++ b/endpoints/guide/edit.php @@ -45,7 +45,7 @@ class GuideEditResponse extends TemplateResponse 'description' => ['filter' => FILTER_CALLBACK, 'options' => [self::class, 'checkDescription'] ], 'changelog' => ['filter' => FILTER_CALLBACK, 'options' => [self::class, 'checkTextBlob'] ], 'body' => ['filter' => FILTER_CALLBACK, 'options' => [self::class, 'checkTextBlob'] ], - 'locale' => ['filter' => FILTER_CALLBACK, 'options' => [Locale::class, 'tryFrom'] ], + 'locale' => ['filter' => FILTER_CALLBACK, 'options' => [self::class, 'checkLocale'] ], 'category' => ['filter' => FILTER_VALIDATE_INT, 'options' => ['min_value' => 1, 'max_value' => 9] ], 'specId' => ['filter' => FILTER_VALIDATE_INT, 'options' => ['min_value' => -1, 'max_value' => 2, 'default' => -1]], 'classId' => ['filter' => FILTER_VALIDATE_INT, 'options' => ['min_value' => 1, 'max_value' => 11, 'default' => 0]] diff --git a/endpoints/locale/locale.php b/endpoints/locale/locale.php index fccf2962..260ae500 100644 --- a/endpoints/locale/locale.php +++ b/endpoints/locale/locale.php @@ -9,7 +9,7 @@ if (!defined('AOWOW_REVISION')) class LocaleBaseResponse extends TextResponse { protected array $expectedGET = array( - 'locale' => ['filter' => FILTER_CALLBACK, 'options' => [Locale::class, 'tryFrom']] + 'locale' => ['filter' => FILTER_CALLBACK, 'options' => [self::class, 'checkLocale']] ); protected function generate() : void diff --git a/includes/components/response/baseresponse.class.php b/includes/components/response/baseresponse.class.php index 986fa509..99d3a8dd 100644 --- a/includes/components/response/baseresponse.class.php +++ b/includes/components/response/baseresponse.class.php @@ -645,6 +645,13 @@ abstract class BaseResponse return preg_replace('/ +/', ' ', trim($str)); } + protected static function checkLocale(string $localeId) : ?Locale + { + if (Util::checkNumeric($localeId, NUM_CAST_INT)) + return Locale::tryFrom($localeId); + return null; + } + /********************/ /* child implements */