/** * Parse a JSON formatted string and convert it into an object. * * If the string is not in JSON format, this method will attempt to parse it as INI format. * * @param string JSON formatted string to convert. * @param array Options used by the formatter. * @return object Data object. */ public function stringToObject($data, $process_sections = false) { $data = trim($data); if (substr($data, 0, 1) != '{' && substr($data, -1, 1) != '}') { $ini =& RokCommon_Registry_Format::getInstance('INI'); $obj = $ini->stringToObject($data, $process_sections); } else { $obj = json_decode($data); } return $obj; }
/** * Returns a reference to a Format object, only creating it * if it doesn't already exist. * * @param string The format to load * @return object Registry format handler * @throws RokCommon_Loader_Exception * @since 1.5 */ public static function getInstance($type) { // Initialize static variable. if (!isset(self::$instances)) { self::$instances = array(); } // Sanitize format type. $type = strtoupper(preg_replace('/[^A-Z0-9_]/i', '', $type)); // Only instantiate the object if it doesn't already exist. if (!isset(self::$instances[$type])) { // Only load the file the class does not exist. $class = 'RokCommon_Registry_Format_' . $type; if (!class_exists($class)) { throw new RokCommon_Loader_Exception('Unable to find Registry format ' . $type); } self::$instances[$type] = new $class(); } return self::$instances[$type]; }
/** * Get a namespace in a given string format * * @param string Format to return the string in * @param mixed Parameters used by the formatter, see formatters for more info * * @return string Namespace in string format * @since 1.5 */ public function toString($format = 'JSON', $options = array()) { // Return a namespace in a given format $handler = RokCommon_Registry_Format::getInstance($format); return $handler->objectToString($this->data, $options); }