-
Notifications
You must be signed in to change notification settings - Fork 22
/
Ext.php
217 lines (187 loc) · 5.39 KB
/
Ext.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* DataTables PHP libraries.
*
* PHP libraries for DataTables and DataTables Editor.
*
* @author SpryMedia
* @copyright 2012 SpryMedia ( http://sprymedia.co.uk )
* @license http://editor.datatables.net/license DataTables Editor
*
* @see http://editor.datatables.net
*/
namespace DataTables;
/**
* Base class for DataTables classes.
*/
class Ext
{
/**
* Static method to instantiate a new instance of a class.
*
* A factory method that will create a new instance of the class
* that has extended 'Ext'. This allows classes to be instantiated
* and then chained - which otherwise isn't available until PHP 5.4.
* If using PHP 5.4 or later, simply create a 'new' instance of the
* target class and chain methods as normal.
*
* @return static Instantiated class
*/
public static function instantiate()
{
$rc = new \ReflectionClass(get_called_class());
$args = func_get_args();
return $rc->newInstanceArgs($args);
}
/**
* Static method to instantiate a new instance of a class (shorthand of
* 'instantiate').
*
* This method performs exactly the same actions as the 'instantiate'
* static method, but is simply shorter and easier to type!
*
* @return static class
*/
public static function inst()
{
$rc = new \ReflectionClass(get_called_class());
$args = func_get_args();
return $rc->newInstanceArgs($args);
}
/**
* Common getter / setter function for DataTables classes.
*
* This getter / setter method makes building getter / setting methods
* easier, by abstracting everything to a single function call.
*
* @param mixed &$prop The property to set
* @param mixed $val The value to set - if given as null, then we assume
* that the function is being used as a getter.
* @param bool $array Treat the target property as an array or not
* (default false). If used as an array, then values passed in are added
* to the $prop array.
*
* @return ($prop is null ? mixed : $this)
*/
protected function _getSet(&$prop, $val, $array = false)
{
// Get
if ($val === null) {
return $prop;
}
// Set
if ($array) {
// Property is an array, merge or add to array
is_array($val) ?
$prop = array_merge($prop, $val) :
$prop[] = $val;
} else {
// Property is just a value
$prop = $val;
}
return $this;
}
/**
* Determine if a property is available in a data set (allowing `null` to be
* a valid value).
*
* @param string $name Javascript dotted object name to write to
* @param array $data Data source array to read from
*
* @return bool true if present, false otherwise
*/
protected function _propExists($name, $data)
{
if (strpos($name, '.') === false) {
return isset($data[$name]);
}
$names = explode('.', $name);
$inner = $data;
for ($i = 0; $i < count($names) - 1; ++$i) {
if (!isset($inner[$names[$i]])) {
return false;
}
$inner = $inner[$names[$i]];
}
if (isset($names[count($names) - 1])) {
$idx = $names[count($names) - 1];
return isset($inner[$idx]);
}
return false;
}
/**
* Read a value from a data structure, using Javascript dotted object
* notation. This is the inverse of the `_writeProp` method and provides
* the same support, matching DataTables' ability to read nested JSON
* data objects.
*
* @param string $name Javascript dotted object name to write to
* @param array $data Data source array to read from
*
* @return mixed The read value, or null if no value found.
*/
protected function _readProp($name, $data)
{
if (strpos($name, '.') === false) {
return isset($data[$name]) ?
$data[$name] :
null;
}
$names = explode('.', $name);
$inner = $data;
for ($i = 0; $i < count($names) - 1; ++$i) {
if (!isset($inner[$names[$i]])) {
return null;
}
$inner = $inner[$names[$i]];
}
if (isset($names[count($names) - 1])) {
$idx = $names[count($names) - 1];
return isset($inner[$idx]) ?
$inner[$idx] :
null;
}
return null;
}
/**
* Write the field's value to an array structure, using Javascript dotted
* object notation to indicate JSON data structure. For example `name.first`
* gives the data structure: `name: { first: ... }`. This matches DataTables
* own ability to do this on the client-side, although this doesn't
* implement implement quite such a complex structure (no array / function
* support).
*
* @param array &$out Array to write the data to
* @param string $name Javascript dotted object name to write to
* @param mixed $value Value to write
*/
protected function _writeProp(&$out, $name, $value)
{
if (strpos($name, '.') === false) {
$out[$name] = $value;
return;
}
$names = explode('.', $name);
$inner = &$out;
for ($i = 0; $i < count($names) - 1; ++$i) {
$loopName = $names[$i];
if (!isset($inner[$loopName])) {
$inner[$loopName] = [];
} elseif (!is_array($inner[$loopName])) {
throw new \Exception(
'A property with the name `' . $name . '` already exists. This ' .
'can occur if you have properties which share a prefix - ' .
'for example `name` and `name.first`.'
);
}
$inner = &$inner[$loopName];
}
if (isset($inner[$names[count($names) - 1]])) {
throw new \Exception(
'Duplicate field detected - a field with the name `' . $name . '` ' .
'already exists.'
);
}
$inner[$names[count($names) - 1]] = $value;
}
}