public function executeJson(AgaviRequestDataHolder $rd) { // init $jsonData = array('result' => array('count' => 0, 'data' => array())); $model = $this->getContext()->getModel('Icinga.ApiSimpleDataProvider', 'Web'); $srcId = $rd->getParameter('src_id'); $filter = $rd->getParameter('filter'); $result = $model->setSourceId($srcId)->setFilter($filter)->fetch(); $jsonData['result']['data'] = $result; // store final count and convert $jsonData['result']['count'] = count($jsonData['result']['data']); if (($template = $model->getTemplateCode()) !== false) { $jsonData['result']['template'] = $template; } AppKitArrayUtil::toUTF8_recursive($jsonData); $jsonDataEnc = json_encode($jsonData); return $jsonDataEnc; }
/** * Converts an array containing ISO-8859-1 string to utf-8 * * @param array $obj */ public static function toUTF8_recursive(array &$obj) { foreach ($obj as $field => &$value) { if (is_string($value) && !AppKitStringUtil::isUTF8($value)) { $value = utf8_encode($value); } else { if (is_array($value)) { AppKitArrayUtil::toUTF8_recursive($value); } } } }
/** * @group AppKit */ public function testEncodingProcessor1() { $test = array('test1' => 'ÜÜöö%%123', 'test2' => array('ÖÖÖ', 'ÄÄÄ', 'ßßß')); $work = $test; AppKitArrayUtil::toISO_recursive($work); $this->assertNotEquals($test, $work); AppKitArrayUtil::toUTF8_recursive($work); $this->assertEquals($test, $work); }