Chris@17: data = $data ?: array(); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function append($key, $value = null) Chris@17: { Chris@17: if (0 == strlen($key)) { Chris@17: throw new \RuntimeException("Key cannot be an empty string"); Chris@17: } Chris@17: Chris@17: $currentValue =& $this->data; Chris@17: $keyPath = explode('.', $key); Chris@17: Chris@17: if (1 == count($keyPath)) { Chris@17: if (!isset($currentValue[$key])) { Chris@17: $currentValue[$key] = array(); Chris@17: } Chris@17: if (!is_array($currentValue[$key])) { Chris@17: // Promote this key to an array. Chris@17: // TODO: Is this really what we want to do? Chris@17: $currentValue[$key] = array($currentValue[$key]); Chris@17: } Chris@17: $currentValue[$key][] = $value; Chris@17: Chris@17: return; Chris@17: } Chris@17: Chris@17: $endKey = array_pop($keyPath); Chris@17: for ( $i = 0; $i < count($keyPath); $i++ ) { Chris@17: $currentKey =& $keyPath[$i]; Chris@17: if ( ! isset($currentValue[$currentKey]) ) { Chris@17: $currentValue[$currentKey] = array(); Chris@17: } Chris@17: $currentValue =& $currentValue[$currentKey]; Chris@17: } Chris@17: Chris@17: if (!isset($currentValue[$endKey])) { Chris@17: $currentValue[$endKey] = array(); Chris@17: } Chris@17: if (!is_array($currentValue[$endKey])) { Chris@17: $currentValue[$endKey] = array($currentValue[$endKey]); Chris@17: } Chris@17: // Promote this key to an array. Chris@17: // TODO: Is this really what we want to do? Chris@17: $currentValue[$endKey][] = $value; Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function set($key, $value = null) Chris@17: { Chris@17: if (0 == strlen($key)) { Chris@17: throw new \RuntimeException("Key cannot be an empty string"); Chris@17: } Chris@17: Chris@17: $currentValue =& $this->data; Chris@17: $keyPath = explode('.', $key); Chris@17: Chris@17: if (1 == count($keyPath)) { Chris@17: $currentValue[$key] = $value; Chris@17: Chris@17: return; Chris@17: } Chris@17: Chris@17: $endKey = array_pop($keyPath); Chris@17: for ( $i = 0; $i < count($keyPath); $i++ ) { Chris@17: $currentKey =& $keyPath[$i]; Chris@17: if (!isset($currentValue[$currentKey])) { Chris@17: $currentValue[$currentKey] = array(); Chris@17: } Chris@17: if (!is_array($currentValue[$currentKey])) { Chris@17: throw new \RuntimeException("Key path at $currentKey of $key cannot be indexed into (is not an array)"); Chris@17: } Chris@17: $currentValue =& $currentValue[$currentKey]; Chris@17: } Chris@17: $currentValue[$endKey] = $value; Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function remove($key) Chris@17: { Chris@17: if (0 == strlen($key)) { Chris@17: throw new \RuntimeException("Key cannot be an empty string"); Chris@17: } Chris@17: Chris@17: $currentValue =& $this->data; Chris@17: $keyPath = explode('.', $key); Chris@17: Chris@17: if (1 == count($keyPath)) { Chris@17: unset($currentValue[$key]); Chris@17: Chris@17: return; Chris@17: } Chris@17: Chris@17: $endKey = array_pop($keyPath); Chris@17: for ( $i = 0; $i < count($keyPath); $i++ ) { Chris@17: $currentKey =& $keyPath[$i]; Chris@17: if (!isset($currentValue[$currentKey])) { Chris@17: return; Chris@17: } Chris@17: $currentValue =& $currentValue[$currentKey]; Chris@17: } Chris@17: unset($currentValue[$endKey]); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function get($key, $default = null) Chris@17: { Chris@17: $currentValue = $this->data; Chris@17: $keyPath = explode('.', $key); Chris@17: Chris@17: for ( $i = 0; $i < count($keyPath); $i++ ) { Chris@17: $currentKey = $keyPath[$i]; Chris@17: if (!isset($currentValue[$currentKey]) ) { Chris@17: return $default; Chris@17: } Chris@17: if (!is_array($currentValue)) { Chris@17: return $default; Chris@17: } Chris@17: $currentValue = $currentValue[$currentKey]; Chris@17: } Chris@17: Chris@17: return $currentValue === null ? $default : $currentValue; Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function has($key) Chris@17: { Chris@17: $currentValue = &$this->data; Chris@17: $keyPath = explode('.', $key); Chris@17: Chris@17: for ( $i = 0; $i < count($keyPath); $i++ ) { Chris@17: $currentKey = $keyPath[$i]; Chris@17: if ( Chris@17: !is_array($currentValue) || Chris@17: !array_key_exists($currentKey, $currentValue) Chris@17: ) { Chris@17: return false; Chris@17: } Chris@17: $currentValue = &$currentValue[$currentKey]; Chris@17: } Chris@17: Chris@17: return true; Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function getData($key) Chris@17: { Chris@17: $value = $this->get($key); Chris@17: if (is_array($value) && Util::isAssoc($value)) { Chris@17: return new Data($value); Chris@17: } Chris@17: Chris@17: throw new \RuntimeException("Value at '$key' could not be represented as a DataInterface"); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function import(array $data, $clobber = true) Chris@17: { Chris@17: $this->data = Util::mergeAssocArray($this->data, $data, $clobber); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function importData(DataInterface $data, $clobber = true) Chris@17: { Chris@17: $this->import($data->export(), $clobber); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function export() Chris@17: { Chris@17: return $this->data; Chris@17: } Chris@17: }