Skip to content

Add helpers for Zval coercion #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/types/zval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,92 @@ impl Zval {
FromZval::from_zval(self)
}

/// Coerce the value into a string. Mimics the PHP type coercion rules.
pub fn coerce_into_string(&mut self) -> Result<()> {
if self.is_string() {
return Ok(());
}

if let Some(val) = self.string() {
self.set_string(&val, false)?;
return Ok(());
} else if let Some(val) = self.double() {
self.set_string(&val.to_string(), false)?;
return Ok(());
} else if let Some(val) = self.long() {
self.set_string(&val.to_string(), false)?;
return Ok(());
} else if let Some(val) = self.bool() {
self.set_string(if val { "1" } else { "0" }, false)?;
return Ok(());
} else if self.is_array() {
self.set_string("Array", false)?;
Copy link
Contributor

@kakserpom kakserpom Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➜  ~ php -r '(string)[];'
PHP Warning:  Array to string conversion in Command line code on line 1

So I'd suggest removing this.

return Ok(());
}

Err(Error::ZvalConversion(self.get_type()))
}

/// Coerce the value into a boolean. Mimics the PHP type coercion rules.
pub fn coerce_into_bool(&mut self) -> Result<()> {
if self.is_bool() {
return Ok(());
}

if let Some(val) = self.long() {
self.set_bool(val != 0 );
return Ok(());
} else if let Some(val) = self.double() {
self.set_bool(val != 0.0 );
return Ok(());
} else if let Some(val) = self.string() {
self.set_bool(val != "0" && val != "");
return Ok(());
} else if let Some(val) = self.array() {
self.set_bool(val.len() != 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.is_empty()

}

Err(Error::ZvalConversion(self.get_type()))
}

/// Coerce the value into a long. Mimics the PHP type coercion rules.
pub fn coerce_into_long(&mut self) -> Result<()> {
if self.is_long() {
return Ok(());
}

if let Some(val) = self.double() {
self.set_long(val as i64);
return Ok(());
} else if let Some(val) = self.string() {
self.set_long(val.parse::<i64>().map_err(|_| Error::ZvalConversion(self.get_type()))?);
return Ok(());
} else if let Some(val) = self.array() {
self.set_long(if val.len() > 0 { 1 } else { 0 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val.is_empty()

}

Err(Error::ZvalConversion(self.get_type()))
}

/// Coerce the value into a double. Mimics the PHP type coercion rules.
pub fn coerce_into_double(&mut self) -> Result<()> {
if self.is_double() {
return Ok(());
}

if let Some(val) = self.long() {
self.set_double(val as f64);
return Ok(());
} else if let Some(val) = self.string() {
self.set_double(val.parse::<f64>().map_err(|_| Error::ZvalConversion(self.get_type()))?);
return Ok(());
} else if let Some(val) = self.array() {
self.set_double(if val.len() > 0 { 1.0 } else { 0.0 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val.is_empty()

}

Err(Error::ZvalConversion(self.get_type()))
}

/// Creates a shallow clone of the [`Zval`].
///
/// This copies the contents of the [`Zval`], and increments the reference
Expand Down
Loading