mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-03 16:51:40 +09:00
- 아래에 이미 author 언급이 있으므로 중복되는 저작권 표기는 제거 - 클래스 하단에 불필요한 end of file 표시 제거 (파일 하나에 클래스 하나씩이므로 파일이 중간에 끊겼다면 클래스가 닫히지 않아 쉽게 알 수 있음)
75 lines
1.4 KiB
PHP
75 lines
1.4 KiB
PHP
<?php
|
|
|
|
class JSONDisplayHandler
|
|
{
|
|
|
|
/**
|
|
* Produce JSON compliant content given a module object.\n
|
|
* @param ModuleObject $oModule the module object
|
|
* @return string
|
|
*/
|
|
public function toDoc($oModule)
|
|
{
|
|
$variables = $oModule->getVariables();
|
|
$variables['error'] = $oModule->getError();
|
|
$variables['message'] = $oModule->getMessage();
|
|
|
|
self::_convertCompat($variables, Context::getRequestMethod());
|
|
return json_encode($variables);
|
|
}
|
|
|
|
/**
|
|
* Convert arrays in a format that is compatible with XE.
|
|
*
|
|
* @param array $array
|
|
* @param string $compat_type
|
|
* @return array
|
|
*/
|
|
protected static function _convertCompat(&$array, $compat_type = 'JSON')
|
|
{
|
|
foreach ($array as $key => &$value)
|
|
{
|
|
if (is_object($value))
|
|
{
|
|
$value = get_object_vars($value);
|
|
}
|
|
if (is_array($value))
|
|
{
|
|
self::_convertCompat($value, $compat_type);
|
|
if (self::_isNumericArray($value))
|
|
{
|
|
if ($compat_type === 'XMLRPC')
|
|
{
|
|
$value = array('item' => array_values($value));
|
|
}
|
|
else
|
|
{
|
|
$value = array_values($value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 (!is_numeric($key))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|