10 defined(
'JPATH_PLATFORM') or die;
12 jimport('joomla.utilities.arrayhelper');
35 protected static $instances = array();
44 public function __construct($data = null)
47 $this->data =
new stdClass;
50 if (is_array($data) || is_object($data))
52 $this->bindData($this->data, $data);
54 elseif (!empty($data) && is_string($data))
56 $this->loadString($data);
67 public function __clone()
69 $this->data = unserialize(serialize($this->data));
79 public function __toString()
81 return $this->toString();
93 public function jsonSerialize()
108 public function def($key, $default =
'')
110 $value = $this->
get($key, $default);
111 $this->
set($key, $value);
125 public function exists($path)
128 if ($nodes = explode(
'.', $path))
134 for ($i = 0, $n = count($nodes); $i < $n; $i++)
136 if (isset($node->$nodes[$i]))
138 $node = $node->$nodes[$i];
165 public function get($path, $default = null)
169 if (!strpos($path,
'.'))
171 return (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !==
'') ? $this->data->$path : $default;
175 $nodes = explode(
'.', $path);
182 foreach ($nodes as $n)
184 if (isset($node->$n))
196 if ($found && $node !== null && $node !==
'')
217 public static function getInstance($id)
219 if (empty(self::$instances[$id]))
224 return self::$instances[$id];
236 public function loadArray($array)
238 $this->bindData($this->data, $array);
252 public function loadObject($object)
254 $this->bindData($this->data, $object);
270 public function loadFile($file, $format =
'JSON', $options = array())
272 $data = file_get_contents($file);
274 return $this->loadString($data, $format, $options);
288 public function loadString($data, $format =
'JSON', $options = array())
293 $obj = $handler->stringToObject($data, $options);
294 $this->loadObject($obj);
308 public function merge($source)
316 foreach ($source->toArray() as $k => $v)
318 if (($v !== null) && ($v !==
''))
320 $this->data->$k = $v;
337 public function set($path, $value)
346 $nodes = array_values(array_filter(explode(
'.', $path),
'strlen'));
354 for ($i = 0, $n = count($nodes) - 1; $i < $n; $i++)
356 if (!isset($node->$nodes[$i]) && ($i != $n))
358 $node->$nodes[$i] =
new stdClass;
361 $node = (object) $node->$nodes[$i];
365 $result = $node->$nodes[$i] = $value;
378 public function toArray()
380 return (array) $this->asArray($this->data);
390 public function toObject()
405 public function toString($format =
'JSON', $options = array())
410 return $handler->objectToString($this->data, $options);
423 protected function bindData($parent, $data)
426 if (is_object($data))
428 $data = get_object_vars($data);
432 $data = (array) $data;
435 foreach ($data as $k => $v)
439 $parent->$k =
new stdClass;
440 $this->bindData($parent->$k, $v);
458 protected function asArray($data)
462 foreach (get_object_vars((
object) $data) as $k => $v)
466 $array[$k] = $this->asArray($v);