[go: up one dir, main page]

Menu

[3822d7]: / core / lib / Data.php  Maximize  Restore  History

Download this file

193 lines (167 with data), 4.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace ICT\Core;
/* * ***************************************************************
* Copyright © 2015 ICT Innovations Pakistan All Rights Reserved *
* Developed By: Nasir Iqbal *
* Website : http://www.ictinnovations.com/ *
* Mail : nasir@ictinnovations.com *
* *************************************************************** */
use ArrayAccess;
use Countable;
use Iterator;
class Data implements ArrayAccess, Iterator, Countable
{
/**
* @var Array
*/
protected $data = NULL;
public function __construct(&$data = array())
{
$this->data = &$data;
}
public function merge(Data $oData)
{
$srcData = (array) $this->getDataCopy();
$newData = (array) $oData->getDataCopy();
$this->data = array_replace_recursive($srcData, $newData);
}
public function getDataCopy()
{
return $this->data;
}
protected function &locate_parent(&$offset)
{
$offset = explode(':', trim($offset, '[]'));
if (1 >= count($offset)) {
return $this->data; // either parent does not exist or we already there
} else {
return $this->locate_parent_callback($offset, $this->data); // go deep to locate
}
}
protected function &locate_parent_callback(&$offset, &$data)
{
$index = array_shift($offset);
if (is_array($data) && isset($data[$index])) {
$data = &$data[$index];
} else if (is_object($data) && isset($data->{$index})) {
$data = &$data->{$index};
} else { // probably parent does not exist at all
array_unshift($offset, $index);
return $data;
}
if (1 < count($offset)) { // yet we have to go more deep ?
return $this->locate_parent_callback($offset, $data);
} else { // it is here! done !!!
return $data;
}
}
public function offsetExists($offset)
{
if (NULL !== $this->offsetGet($offset)) {
return TRUE;
}
return FALSE;
}
public function offsetGet($offset)
{
$data = &$this->locate_parent($offset);
$output = NULL;
if (1 == count($offset)) {
$index = array_shift($offset);
if (is_array($data) && isset($data[$index])) {
$output = &$data[$index];
} else if (is_object($data)) {
if (in_array($index, get_object_vars($data))) {
$output = &$data->{$index}; // only public variable can be used as reference
} else if (isset($data->{$index})) {
$output = $data->{$index};
}
}
}
return $output;
}
public function offsetSet($offset, $value)
{
$data = &$this->locate_parent($offset);
while ($index = array_shift($offset)) {
if (count($offset)) { // if we need to go deep
$new_value = array();
} else {
$new_value = &$value;
}
if (is_array($data)) {
$data[$index] = &$new_value;
$data = &$data[$index];
} else if (is_object($data) && isset($data->{$index})) {
$data->{$index} = null; // required to resolve: Cannot assign by reference to overloaded object
$data->{$index} = &$new_value;
$data = &$data->{$index};
}
}
}
public function offsetUnset($offset)
{
$data = &$this->locate_parent($offset);
if (1 == count($offset)) {
$index = array_shift($offset);
if (is_array($data)) {
unset($data[$index]);
} else if (is_object($data)) {
unset($data->{$index});
}
}
}
public function __isset($name)
{
return $this->offsetExists($name);
}
public function &__get($name)
{
$value = $this->offsetGet($name);
return $value;
}
public function __set($name, $value)
{
$this->offsetSet($name, $value);
}
public function __unset($name)
{
$this->offsetUnset($name);
}
public function __toString()
{
if (is_object($this->data)) {
if (method_exists($this->data, '__toString')) {
return $this->data->__toString();
}
return '';
} else if (!is_array($this->data)) { // arrays are not supported
return $this->data;
}
return '';
}
public function current()
{
return current($this->data);
}
public function key()
{
return key($this->data);
}
public function next()
{
return next($this->data);
}
public function rewind()
{
return reset($this->data);
}
public function valid()
{
return $this->offsetExists($this->key());
}
public function count()
{
return count($this->data);
}
}