-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataCollection.php
More file actions
172 lines (155 loc) · 3.56 KB
/
DataCollection.php
File metadata and controls
172 lines (155 loc) · 3.56 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
<?php
/**
* Class DataCollection
* @abstract
*/
abstract class DataCollection
{
/**
* Collection data
*
* @var array
*/
protected $x_data___ = array();
/**
* Protected Collection Data
* @var array
*/
protected $x_protected_data___ = array();
/**
* DATA COLLECTION
* @todo Help The Data cached save & some data could to be inserted here
*/
/**
* Getting all saved data
*
* @return array
*/
final public function getAllData()
{
return $this->x_data___;
}
/**
* @param string $index
*
* @return bool
*/
final public function removeData($index)
{
if (is_array($index) || is_object($index)) {
return false;
}
unset($this->x_data___[$index]);
if (in_array($index, $this->x_protected_data___)) {
$search = array_search($index, $this->x_protected_data___, true);
unset($this->x_protected_data___[$search]);
}
return true;
}
/**
* @param string $index
*
* @return bool
*/
final public function hasData($index)
{
if (is_string($index) || is_int($index)) {
return array_key_exists($index, $this->x_data___);
}
return false;
}
/**
* Check if is on protected
*
* @param string $index
*
* @return bool
*/
final public function isProtectedData($index)
{
if ($this->hasData($index)) {
return in_array($index, $this->x_protected_data___);
} elseif(in_array($index, $this->x_protected_data___)) {
unset($this->x_protected_data___[$index]);
}
return false;
}
/**
* @param string $index
*
* @return $this
*/
final public function protectData($index = null)
{
if ($index === null && !$this->isProtectedData($index)) {
$data_key = array_keys($this->x_data___);
$index = end($data_key);
}
if ($this->hasData($index)) {
$this->x_protected_data___[] = $index;
}
return $this;
}
/**
* @param string $index
* @param mixed $value
* @param bool $protected
*
* @return $this
*/
final public function setData($index, $value = null, $protected = false)
{
if (is_array($index)) {
foreach ($index as $key => $val) {
$this->x_data___[$key] = $val;
if ($protected) {
$this->protectData($key);
}
}
return $this;
}
if (!is_object($index)) {
if (!$this->isProtectedData($index)) {
$this->x_data___[$index] = $value;
$protected && $this->protectData($index);
}
}
return $this;
}
/**
* @param string $index
* @param mixed $default
*
* @return mixed
*/
final public function getData($index, $default = null)
{
return $this->hasData($index)
? $this->x_data___[$index]
: $default;
}
/**
* Clear Collection Data
*/
public function clearAll()
{
$this->x_data___ = array();
$this->x_protected_data___ = array();
}
/**
* Remove record
*
* @param string $name
*/
public function __unset($name)
{
$this->removeData($name);
}
/**
* Magic method Destruct
*/
public function __destruct()
{
$this->clearAll();
}
}