-
Notifications
You must be signed in to change notification settings - Fork 74
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)?; | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I'd suggest removing this.