diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-06 06:25:10 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-06 06:25:30 +0900 |
| commit | 791ef1cd465597ff43dab4216c4b00e9e4160da8 (patch) | |
| tree | ec6c3bc45f81576146325350faa6dcea638987b4 /crates/shirabe-php-shim | |
| parent | a86bbd67954f7bbc38bb09138edb335d82666526 (diff) | |
| download | php-shirabe-791ef1cd465597ff43dab4216c4b00e9e4160da8.tar.gz php-shirabe-791ef1cd465597ff43dab4216c4b00e9e4160da8.tar.zst php-shirabe-791ef1cd465597ff43dab4216c4b00e9e4160da8.zip | |
refactor(php-shim): split in_array into strict and loose variants
Diffstat (limited to 'crates/shirabe-php-shim')
| -rw-r--r-- | crates/shirabe-php-shim/src/array.rs | 24 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/lib.rs | 10 |
2 files changed, 23 insertions, 11 deletions
diff --git a/crates/shirabe-php-shim/src/array.rs b/crates/shirabe-php-shim/src/array.rs index 29338aae..86a78dd2 100644 --- a/crates/shirabe-php-shim/src/array.rs +++ b/crates/shirabe-php-shim/src/array.rs @@ -585,18 +585,20 @@ fn php_slice_bounds(len: i64, offset: i64, length: Option<i64>) -> (usize, usize (start as usize, end as usize) } -pub fn in_array(needle: PhpMixed, haystack: &PhpMixed, strict: bool) -> bool { - let values: Vec<&PhpMixed> = match haystack { - PhpMixed::List(items) => items.iter().collect(), - PhpMixed::Array(map) => map.values().collect(), - _ => return false, - }; +pub fn in_array_strict<'a>( + needle: impl Into<PhpMixed>, + haystack: impl IntoIterator<Item = &'a PhpMixed>, +) -> bool { + let needle = needle.into(); + haystack.into_iter().any(|value| *value == needle) +} - if strict { - values.iter().any(|value| **value == needle) - } else { - values.iter().any(|value| loose_eq(value, &needle)) - } +pub fn in_array_loose<'a>( + needle: impl Into<PhpMixed>, + haystack: impl IntoIterator<Item = &'a PhpMixed>, +) -> bool { + let needle = needle.into(); + haystack.into_iter().any(|value| loose_eq(value, &needle)) } /// PHP numeric-string-aware conversion to a number, used by loose comparison. diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs index 62287c23..7d22633c 100644 --- a/crates/shirabe-php-shim/src/lib.rs +++ b/crates/shirabe-php-shim/src/lib.rs @@ -209,6 +209,16 @@ impl PhpMixed { } } + /// Values of an array in insertion order. Panics on non-array values, mirroring the TypeError + /// PHP raises when an array is expected. + pub fn values(&self) -> Vec<&PhpMixed> { + match self { + PhpMixed::List(l) => l.iter().collect(), + PhpMixed::Array(a) | PhpMixed::Object(a) => a.values().collect(), + other => panic!("expected an array, got {:?}", other), + } + } + pub fn is_null(&self) -> bool { matches!(self, PhpMixed::Null) } |
