aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-20 00:31:45 +0900
committernsfisis <nsfisis@gmail.com>2026-06-20 00:31:49 +0900
commitcac18ef73a39b4ac41fa4d6ccb753804d4c42cb7 (patch)
tree968f438437042e8092752aa521c2d7e6ae28307e /crates/shirabe-php-shim
parent7c5162e7c2f9d466e96248b7482aa98350244e8b (diff)
downloadphp-shirabe-cac18ef73a39b4ac41fa4d6ccb753804d4c42cb7.tar.gz
php-shirabe-cac18ef73a39b4ac41fa4d6ccb753804d4c42cb7.tar.zst
php-shirabe-cac18ef73a39b4ac41fa4d6ccb753804d4c42cb7.zip
feat(php-shim): implement count()
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-php-shim')
-rw-r--r--crates/shirabe-php-shim/src/lib.rs20
1 files changed, 18 insertions, 2 deletions
diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs
index 5821819..d643260 100644
--- a/crates/shirabe-php-shim/src/lib.rs
+++ b/crates/shirabe-php-shim/src/lib.rs
@@ -2137,8 +2137,20 @@ pub fn trim(s: &str, chars: Option<&str>) -> String {
s.trim_matches(|c| mask.contains(&c)).to_string()
}
-pub fn count(_value: &PhpMixed) -> i64 {
- todo!()
+pub fn count(value: &PhpMixed) -> usize {
+ match value {
+ PhpMixed::List(items) => items.len(),
+ PhpMixed::Array(entries) => entries.len(),
+ PhpMixed::Object(object) => object.count(),
+ // PHP 8 throws a `TypeError` for non-countable arguments.
+ PhpMixed::Null
+ | PhpMixed::Bool(_)
+ | PhpMixed::Int(_)
+ | PhpMixed::Float(_)
+ | PhpMixed::String(_) => {
+ panic!("count(): Argument #1 ($value) must be of type Countable|array")
+ }
+ }
}
pub fn array_shift<T>(_array: &mut Vec<T>) -> Option<T> {
@@ -2800,6 +2812,10 @@ impl ArrayObject {
pub fn to_array(&self) -> IndexMap<String, Box<PhpMixed>> {
self.data.clone()
}
+
+ pub fn count(&self) -> usize {
+ self.data.len()
+ }
}
#[derive(Debug)]