10 defined(
'JPATH_PLATFORM') or die;
27 protected static $cache = array();
42 public function objectToString($object, $options = array())
48 foreach (get_object_vars($object) as $key => $value)
51 if (is_object($value))
55 $local[] =
'[' . $key .
']';
58 foreach (get_object_vars($value) as $k => $v)
60 $local[] = $k .
'=' . $this->getValueAsINI($v);
66 $global[] = $key .
'=' . $this->getValueAsINI($value);
70 return implode(
"\n", array_merge($global, $local));
83 public function stringToObject($data, array $options = array())
85 $sections = (isset($options[
'processSections'])) ? $options[
'processSections'] :
false;
88 $hash = md5($data .
':' . (
int) $sections);
90 if (isset(self::$cache[$hash]))
92 return self::$cache[$hash];
103 $lines = explode(
"\n", $data);
106 foreach ($lines as $line)
112 if (empty($line) || ($line{0} ==
';'))
119 $length = strlen($line);
122 if (($line[0] ==
'[') && ($line[$length - 1] ==
']'))
124 $section = substr($line, 1, $length - 2);
125 $obj->$section =
new stdClass;
129 elseif ($line{0} ==
'[')
135 if (!strpos($line,
'='))
142 list ($key, $value) = explode(
'=', $line, 2);
145 if (preg_match(
'/[^A-Z0-9_]/i', $key))
152 $length = strlen($value);
154 if ($length && ($value[0] ==
'"') && ($value[$length - 1] ==
'"'))
157 $value = stripcslashes(substr($value, 1, ($length - 2)));
158 $value = str_replace(
'\n',
"\n", $value);
165 if ($value ==
'false')
170 elseif ($value ==
'true')
175 elseif (is_numeric($value))
178 if (strpos($value,
'.') !==
false)
180 $value = (float) $value;
184 $value = (int) $value;
192 $obj->$section->$key = $value;
201 self::$cache[$hash] = clone ($obj);
215 protected function getValueAsINI($value)
219 switch (gettype($value))
227 $string = $value ?
'true' :
'false';
232 $string =
'"' . str_replace(array(
"\r\n",
"\n"),
'\\n', $value) .
'"';