EOT;
/**
* Class with a helper routine to fix the special values
*/
class configSpecialValuesFixer
{
/**
*@desc helper routine that converts config values that match certain strings such as ''BOOLEAN_FALSE'' into php data type values such as 'false'
* We need this method cos, if we store the config as xml or ini, all values are "strings", even empty strings ""
* and tests can sometimes fail if an emtpy string is compared against null, false, true etc
*@param Zend_Config config object
*@return void
*/
public function convertSpecialConfigValuesToPHPDataTypesRECURSIVELY($config)
{
foreach ($config as $key => $value )
{
if (is_string($value))
{
//convert if a special string
switch ($value)
{
case 'BOOLEAN_FALSE':
$config->$key = false;
break;
case 'NULL':
$config->$key = null;
break;
default:
//do not do anything its
//probably a genuine string value
}
}
elseif($value instanceOf Zend_Config)
{
//Note: RECURSIVE method call
$this->convertSpecialConfigValuesToPHPDataTypesRECURSIVELY($value);
}
}
}
}
/**
*Writes out the config data for this code example
*@param Zend_Config config object
*@return void
*/
function writeAnimal($animalConfig)
{
echo ''. $animalConfig->type .'
';
echo '';
echo print_r($animalConfig->toArray(), TRUE);
if (!($animalConfig->number_of_legs
== null))
{
echo "\n";
echo 'They tend to have '
. $animalConfig->number_of_legs
. ' legs' ;
}
if (!($animalConfig->number_of_fins
== null))
{
echo "\n";
echo 'They tend to have '
. $animalConfig->number_of_fins
.' fins' ;
}
echo '';
}
//--------------
//set up the congfig adaptors with the config data
require_once('Zend/Config/Xml.php');
$dogConfig = new Zend_Config_Xml( $xmlString,
'dog',
TRUE);
$fishConfig = new Zend_Config_Xml( $xmlString,
'fish',
TRUE);
//lets see what we've got
echo 'Before Fix
';
writeAnimal($dogConfig);
writeAnimal($fishConfig);
//convert values of special strings into proper data types
$configFixer = new configSpecialValuesFixer();
//fix dog
$configFixer->convertSpecialConfigValuesToPHPDataTypesRECURSIVELY($dogConfig);
$dogConfig->setReadOnly(TRUE);
//fix fish
$configFixer->convertSpecialConfigValuesToPHPDataTypesRECURSIVELY($fishConfig);
//make it read-only now that we have done our fixes
$fishConfig->setReadOnly(TRUE);
//lets see what we've got now then. Oooooh the excitement!
echo 'After Fix
';
writeAnimal($dogConfig);
writeAnimal($fishConfig);
?>