<?php
class Library_Json {
	static public function encode($data) {
		//kontorla CALLBACK
		$response = Zend_Json::encode($data);
		
		//kontrola JSONp
		if (isset($_GET["callback"])) {
			$response = $_GET["callback"] . "(" . $response . ")";
		}
		
		return $response;
	}
	
	static public function encodeResponse(Library_Response $response) {
		//kontorla dat
		foreach ($response->data as $key => $value) {
			//konotrla jestli se jedna o objekt
			if (!is_object($value)) {
				continue;
			}
			
			//kontorla jestli implementuje metodu toArray
			if (method_exists($value, "toArray")) {
				$response->data[$key] = $value->toArray();
			}
		}
		
		return self::encode($response);
	}
}