Merge pull request #216 from kijin/pr/numeric-array-json-encoding

JSON 결과 반환시 산술 배열의 인코딩 호환성 개선
This commit is contained in:
Kijin Sung 2016-02-06 15:15:37 +09:00
commit f040c767e3

View file

@ -9,14 +9,49 @@ class JSONDisplayHandler
* @param ModuleObject $oModule the module object
* @return string
*/
function toDoc(&$oModule)
public function toDoc($oModule)
{
$variables = $oModule->getVariables();
$variables['error'] = $oModule->getError();
$variables['message'] = $oModule->getMessage();
return json_encode($variables);
$temp = array();
foreach ($variables as $key => $value)
{
if (self::_isNumericArray($value))
{
$temp[$key] = array_values($value);
}
else
{
$temp[$key] = $value;
}
}
return json_encode($temp);
}
/**
* Check if an array only has numeric keys.
*
* @param array $array
* @return bool
*/
protected static function _isNumericArray($array)
{
if (!is_array($array) || !count($array))
{
return false;
}
foreach ($array as $key => $value)
{
if (intval($key) != $key)
{
return false;
}
}
return true;
}
}
/* End of file JSONDisplayHandler.class.php */
/* Location: ./classes/display/JSONDisplayHandler.class.php */