-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassHelper.class.php
More file actions
107 lines (80 loc) · 2.36 KB
/
ClassHelper.class.php
File metadata and controls
107 lines (80 loc) · 2.36 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
<?PHP
/*
#LICENSE BEGIN
**********************************************************************
* OgerArch - Archaeological Database is released under the GNU General Public License (GPL) <http://www.gnu.org/licenses>
* Copyright (C) Gerhard Öttl <gerhard.oettl@ogersoft.at>
**********************************************************************
#LICENSE END
*/
/**
* Helper class for class and object handling
*/
class ClassHelper {
/**
* get only public properties from an object
*/
public static function getObjectVars($obj) {
return get_object_vars($obj);
}
/**
* assign public variables for this object from elsewhere
* Only existing variables of the TO object are updated
* @preserve:
* - true: variables not existing in FROM are preserved
* - false: (default) variables not existing in FROM are set to null
*/
public static function assignTo(&$to, $from, $preserve = false, $guess = false) {
// if values are from an object then convert to array
if (is_object($from))
$from = get_object_vars($from);
// force array
if (!is_array($from))
$from = array();
// assign each public object variable from array
foreach(get_object_vars($to) as $key => $dummy) {
// reset temp vars
unset($searchKeys);
unset($value);
// create possible keys
$searchKeys[] = $key;
// if guess is allowed than also search for lowercase and uppercase key
if ($guess) {
$searchKeys[] = strtolower($key);
$searchKeys[] = strtoupper($key);
}
// look if key exists
foreach ($searchKeys as $searchKey) {
if (array_key_exists($searchKey, $from)) {
$value = $from[$searchKey];
$found = true;
break;
}
}
// if preserve is set than assign only if value is found
if ($preserve && !$found)
continue;
// now assign
$to->$key = $value;
} // end of loop over public properties
return $to;
}
/**
* assign public variables from elsewhere to this
* All variables of the FROM object are transfered
*/
public static function assignFrom($from, &$to) {
// if values are from an object then convert to array
if (is_object($from))
$from = get_object_vars($from);
// force array
if (!is_array($from))
$from = array();
// assign each public object variable from array
foreach($from as $key => $value) {
$to->$key = $value;
} // end of loop over public properties
return $to;
}
}
?>