From f05e1a55cf0d44e9611e06a3d7b4fccdcb90ce7b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 21 Jun 2026 17:42:32 +0900 Subject: refactor(php-shim): split filter_var into per-filter functions Replace the dispatch-on-constant filter_var() and filter_var_with_options() with dedicated filter_var_boolean/url/email/ip and filter_var_int_with_range, dropping the FILTER_VALIDATE_* constants and updating all call sites. Co-Authored-By: Claude Opus 4.8 (1M context) --- crates/shirabe-php-shim/src/filter.rs | 56 +++++++++++++++-------------------- 1 file changed, 24 insertions(+), 32 deletions(-) (limited to 'crates/shirabe-php-shim/src') diff --git a/crates/shirabe-php-shim/src/filter.rs b/crates/shirabe-php-shim/src/filter.rs index a23752b..bd1a6fa 100644 --- a/crates/shirabe-php-shim/src/filter.rs +++ b/crates/shirabe-php-shim/src/filter.rs @@ -1,39 +1,31 @@ -use crate::PhpMixed; -use indexmap::IndexMap; +// Without FILTER_NULL_ON_FAILURE, php_filter_boolean trims surrounding +// whitespace, lowercases, and yields true only for "1"/"true"/"on"/"yes"; +// every other input (including the "0"/"false"/"off"/"no"/"" set) yields +// false. +pub fn filter_var_boolean(value: &str) -> bool { + let trimmed = value.trim_matches([' ', '\t', '\n', '\r', '\0', '\x0B']); + matches!( + trimmed.to_ascii_lowercase().as_str(), + "1" | "true" | "on" | "yes" + ) +} -pub const FILTER_VALIDATE_EMAIL: i64 = 274; +// TODO(phase-c): PHP's FILTER_VALIDATE_URL parses with php_url_parse_ex and +// additionally validates the host as a domain/IPv6 literal. reqwest::Url +// (WHATWG/RFC 3986) is stricter on some inputs and more lenient on others, +// so this is not a byte-for-byte compatible validator. +pub fn filter_var_url(value: &str) -> bool { + reqwest::Url::parse(value).is_ok() +} -pub const FILTER_VALIDATE_BOOLEAN: i64 = 258; -pub const FILTER_VALIDATE_URL: i64 = 273; -pub const FILTER_VALIDATE_IP: i64 = 275; -pub const FILTER_VALIDATE_INT: i64 = 257; +pub fn filter_var_email(_value: &str) -> bool { + todo!() +} -pub fn filter_var(value: &str, filter: i64) -> bool { - match filter { - // Without FILTER_NULL_ON_FAILURE, php_filter_boolean trims surrounding - // whitespace, lowercases, and yields true only for "1"/"true"/"on"/"yes"; - // every other input (including the "0"/"false"/"off"/"no"/"" set) yields - // false. - FILTER_VALIDATE_BOOLEAN => { - let trimmed = value.trim_matches([' ', '\t', '\n', '\r', '\0', '\x0B']); - matches!( - trimmed.to_ascii_lowercase().as_str(), - "1" | "true" | "on" | "yes" - ) - } - // TODO(phase-c): PHP's FILTER_VALIDATE_URL parses with php_url_parse_ex and - // additionally validates the host as a domain/IPv6 literal. reqwest::Url - // (WHATWG/RFC 3986) is stricter on some inputs and more lenient on others, - // so this is not a byte-for-byte compatible validator. - FILTER_VALIDATE_URL => reqwest::Url::parse(value).is_ok(), - _ => todo!(), - } +pub fn filter_var_ip(_value: &str) -> bool { + todo!() } -pub fn filter_var_with_options( - _value: &str, - _filter: i64, - _options: &IndexMap, -) -> PhpMixed { +pub fn filter_var_int_with_range(_value: &str, _min: i64, _max: i64) -> bool { todo!() } -- cgit v1.3.1