@@ -680,6 +680,92 @@ impl Zval {
680680 FromZval :: from_zval ( self )
681681 }
682682
683+ /// Coerce the value into a string. Mimics the PHP type coercion rules.
684+ pub fn coerce_into_string ( & mut self ) -> Result < ( ) > {
685+ if self . is_string ( ) {
686+ return Ok ( ( ) ) ;
687+ }
688+
689+ if let Some ( val) = self . string ( ) {
690+ self . set_string ( & val, false ) ?;
691+ return Ok ( ( ) ) ;
692+ } else if let Some ( val) = self . double ( ) {
693+ self . set_string ( & val. to_string ( ) , false ) ?;
694+ return Ok ( ( ) ) ;
695+ } else if let Some ( val) = self . long ( ) {
696+ self . set_string ( & val. to_string ( ) , false ) ?;
697+ return Ok ( ( ) ) ;
698+ } else if let Some ( val) = self . bool ( ) {
699+ self . set_string ( if val { "1" } else { "0" } , false ) ?;
700+ return Ok ( ( ) ) ;
701+ } else if self . is_array ( ) {
702+ self . set_string ( "Array" , false ) ?;
703+ return Ok ( ( ) ) ;
704+ }
705+
706+ Err ( Error :: ZvalConversion ( self . get_type ( ) ) )
707+ }
708+
709+ /// Coerce the value into a boolean. Mimics the PHP type coercion rules.
710+ pub fn coerce_into_bool ( & mut self ) -> Result < ( ) > {
711+ if self . is_bool ( ) {
712+ return Ok ( ( ) ) ;
713+ }
714+
715+ if let Some ( val) = self . long ( ) {
716+ self . set_bool ( val != 0 ) ;
717+ return Ok ( ( ) ) ;
718+ } else if let Some ( val) = self . double ( ) {
719+ self . set_bool ( val != 0.0 ) ;
720+ return Ok ( ( ) ) ;
721+ } else if let Some ( val) = self . string ( ) {
722+ self . set_bool ( val != "0" && val != "" ) ;
723+ return Ok ( ( ) ) ;
724+ } else if let Some ( val) = self . array ( ) {
725+ self . set_bool ( val. len ( ) != 0 ) ;
726+ }
727+
728+ Err ( Error :: ZvalConversion ( self . get_type ( ) ) )
729+ }
730+
731+ /// Coerce the value into a long. Mimics the PHP type coercion rules.
732+ pub fn coerce_into_long ( & mut self ) -> Result < ( ) > {
733+ if self . is_long ( ) {
734+ return Ok ( ( ) ) ;
735+ }
736+
737+ if let Some ( val) = self . double ( ) {
738+ self . set_long ( val as i64 ) ;
739+ return Ok ( ( ) ) ;
740+ } else if let Some ( val) = self . string ( ) {
741+ self . set_long ( val. parse :: < i64 > ( ) . map_err ( |_| Error :: ZvalConversion ( self . get_type ( ) ) ) ?) ;
742+ return Ok ( ( ) ) ;
743+ } else if let Some ( val) = self . array ( ) {
744+ self . set_long ( if val. len ( ) > 0 { 1 } else { 0 } ) ;
745+ }
746+
747+ Err ( Error :: ZvalConversion ( self . get_type ( ) ) )
748+ }
749+
750+ /// Coerce the value into a double. Mimics the PHP type coercion rules.
751+ pub fn coerce_into_double ( & mut self ) -> Result < ( ) > {
752+ if self . is_double ( ) {
753+ return Ok ( ( ) ) ;
754+ }
755+
756+ if let Some ( val) = self . long ( ) {
757+ self . set_double ( val as f64 ) ;
758+ return Ok ( ( ) ) ;
759+ } else if let Some ( val) = self . string ( ) {
760+ self . set_double ( val. parse :: < f64 > ( ) . map_err ( |_| Error :: ZvalConversion ( self . get_type ( ) ) ) ?) ;
761+ return Ok ( ( ) ) ;
762+ } else if let Some ( val) = self . array ( ) {
763+ self . set_double ( if val. len ( ) > 0 { 1.0 } else { 0.0 } ) ;
764+ }
765+
766+ Err ( Error :: ZvalConversion ( self . get_type ( ) ) )
767+ }
768+
683769 /// Creates a shallow clone of the [`Zval`].
684770 ///
685771 /// This copies the contents of the [`Zval`], and increments the reference
0 commit comments