4 * This file is a part of dflydev/dot-access-data.
6 * (c) Dragonfly Development Inc.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Dflydev\DotAccessData;
14 class Data implements DataInterface
17 * Internal representation of data data
26 * @param array|null $data
28 public function __construct(array $data = null)
30 $this->data = $data ?: array();
36 public function append($key, $value = null)
38 if (0 == strlen($key)) {
39 throw new \RuntimeException("Key cannot be an empty string");
42 $currentValue =& $this->data;
43 $keyPath = explode('.', $key);
45 if (1 == count($keyPath)) {
46 if (!isset($currentValue[$key])) {
47 $currentValue[$key] = array();
49 if (!is_array($currentValue[$key])) {
50 // Promote this key to an array.
51 // TODO: Is this really what we want to do?
52 $currentValue[$key] = array($currentValue[$key]);
54 $currentValue[$key][] = $value;
59 $endKey = array_pop($keyPath);
60 for ( $i = 0; $i < count($keyPath); $i++ ) {
61 $currentKey =& $keyPath[$i];
62 if ( ! isset($currentValue[$currentKey]) ) {
63 $currentValue[$currentKey] = array();
65 $currentValue =& $currentValue[$currentKey];
68 if (!isset($currentValue[$endKey])) {
69 $currentValue[$endKey] = array();
71 if (!is_array($currentValue[$endKey])) {
72 $currentValue[$endKey] = array($currentValue[$endKey]);
74 // Promote this key to an array.
75 // TODO: Is this really what we want to do?
76 $currentValue[$endKey][] = $value;
82 public function set($key, $value = null)
84 if (0 == strlen($key)) {
85 throw new \RuntimeException("Key cannot be an empty string");
88 $currentValue =& $this->data;
89 $keyPath = explode('.', $key);
91 if (1 == count($keyPath)) {
92 $currentValue[$key] = $value;
97 $endKey = array_pop($keyPath);
98 for ( $i = 0; $i < count($keyPath); $i++ ) {
99 $currentKey =& $keyPath[$i];
100 if (!isset($currentValue[$currentKey])) {
101 $currentValue[$currentKey] = array();
103 if (!is_array($currentValue[$currentKey])) {
104 throw new \RuntimeException("Key path at $currentKey of $key cannot be indexed into (is not an array)");
106 $currentValue =& $currentValue[$currentKey];
108 $currentValue[$endKey] = $value;
114 public function remove($key)
116 if (0 == strlen($key)) {
117 throw new \RuntimeException("Key cannot be an empty string");
120 $currentValue =& $this->data;
121 $keyPath = explode('.', $key);
123 if (1 == count($keyPath)) {
124 unset($currentValue[$key]);
129 $endKey = array_pop($keyPath);
130 for ( $i = 0; $i < count($keyPath); $i++ ) {
131 $currentKey =& $keyPath[$i];
132 if (!isset($currentValue[$currentKey])) {
135 $currentValue =& $currentValue[$currentKey];
137 unset($currentValue[$endKey]);
143 public function get($key, $default = null)
145 $currentValue = $this->data;
146 $keyPath = explode('.', $key);
148 for ( $i = 0; $i < count($keyPath); $i++ ) {
149 $currentKey = $keyPath[$i];
150 if (!isset($currentValue[$currentKey]) ) {
153 if (!is_array($currentValue)) {
156 $currentValue = $currentValue[$currentKey];
159 return $currentValue === null ? $default : $currentValue;
165 public function has($key)
167 $currentValue = &$this->data;
168 $keyPath = explode('.', $key);
170 for ( $i = 0; $i < count($keyPath); $i++ ) {
171 $currentKey = $keyPath[$i];
173 !is_array($currentValue) ||
174 !array_key_exists($currentKey, $currentValue)
178 $currentValue = &$currentValue[$currentKey];
187 public function getData($key)
189 $value = $this->get($key);
190 if (is_array($value) && Util::isAssoc($value)) {
191 return new Data($value);
194 throw new \RuntimeException("Value at '$key' could not be represented as a DataInterface");
200 public function import(array $data, $clobber = true)
202 $this->data = Util::mergeAssocArray($this->data, $data, $clobber);
208 public function importData(DataInterface $data, $clobber = true)
210 $this->import($data->export(), $clobber);
216 public function export()