PHP singleton util class for checking and returning valid boolean value for use in AMFPHP
Seasoned Flash developers should note that PHP booleans are converted to strings as follows: true -> “1″, false -> “”. This can cause some confusion when inserting Flash booleans into a database; a simple workaround is
$bool ? "true" : "false".
Taken from http://amfphp.org/docs/datatypes.html
As I had this problem with my Flex+AMFPHP project, I decided to write util class that will deal with this issue.
<?php /** * This class is singleton class for checking and returning valid boolean value * for use in AMFPHP (or other remoting services). * * For more info on this problem refer to: http://amfphp.org/docs/datatypes.html * * Use Example: * $stringBool = BoolUtil::getInstance()->getBool(false,true); //returns "false" * $stringBool = BoolUtil::getInstance()->getBool(true,true); //returns "true" * $stringBool = BoolUtil::getInstance()->getBool(0,true); //returns "false" * $stringBool = BoolUtil::getInstance()->getBool("off",true); //returns "false" * $stringBool = BoolUtil::getInstance()->getBool("True",true); //returns "true" * * * @author Marko Kecman <mkecman[at]gmail.com> */ class BoolUtil { /** Checks a variable to see if it should be considered a boolean true or false. * Also takes into account some text-based representations of true of false, * such as 'false','N','yes','on','off', etc. * @author Samuel Levy <sam+nospam@samuellevy.com> * @modified by Marko Kecman <mkecman[at]gmail.com> * @param mixed $value The variable to check * @param bool $strict If set to false, consider everything that is not false to be true. * @return string The string representation of boolean equivalent or null */ function getBool($value, $strict=false) { $out = null; // if not strict, we only have to check if something is false if (in_array($in,array('false', 'False', 'FALSE', 'no', 'No', 'n', 'N', '0', 'off','Off', 'OFF', false, 0, null), true)) { $out = "false"; } if ($strict) { // if strict, check the equivalent true values if (in_array($value,array('true', 'True', 'TRUE', 'yes', 'Yes', 'y', 'Y', '1','on', 'On', 'ON', true, 1), true)) { $out = "true"; } } else { // not strict? let the regular php bool check figure it out (will largely default to true) $out = ( $value ? "true" : "false" ); } return $out; } function &getInstance () // this implements the 'singleton' design pattern. { static $instance; if (!isset($instance)) { $c = __CLASS__; $instance = new $c; } // if return $instance; } // getInstance } ?>
There is also problem with converting string to boolean in ActionScript. Check this example for classic string to boolean casting (as you would expect to work correctly):
var test:Boolean = Boolean("false"); //returns true var test:Boolean = Boolean("0"); //returns true var test:Boolean = Boolean(""); //returns false
In order to avoid this issue, I wrote util class for ActionScript 3 that will do valid String to Boolean convert.
package org.xgeek.markokecman.business.util { public class BoolUtil { public static function StringToBoolean ( value:String ) : Boolean { if ( value.toLowerCase() == "false" ) { return false; } else if ( value.toLowerCase() == "true" ) { return true; } else { return null; } } } }
In next version, I’ll expand this class to something similar to PHP version above.
This article was written by 2501Tags: ActionScript, Boolean, Class, Conversion, Flash Boolean casting problem, PHP, String