-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.php
92 lines (83 loc) · 2.16 KB
/
Object.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
<?php
/**
* Basic object implementation
*
* @author Vitaly Glibin
*/
class Qw_Common_Object extends Qw_Common_GetterSetter
{
public function __construct($data = array())
{
parent::__construct();
if (is_array($data))
foreach($data as $key => $value) {
$this->{$key} = $value;
}
}
/**
* Converts our class to simple stdClass object with recursive check for properties as objects
*
* @return stdClass
*/
public function toObject()
{
$result = new stdClass();
foreach($this->_object as $key => $value) {
$result->{$key} = $this->_convertToObject($value);
}
return $result;
}
protected function _convertToObject($object)
{
$result = null;
if (is_object($object) && method_exists($object, 'toObject')) {
$result = $object->toObject();
}
else if (is_array($object)) {
$result = array();
foreach($object as $k => $item) {
$result[$k] = $this->_convertToObject($item);
}
}
else {
$result = $object;
}
return $result;
}
/**
* Converts our class to simple array with recursive check for properties as objects
*
* @return array
*/
public function toArray()
{
$result = array();
foreach($this->_object as $key => $value) {
if (is_object($value) && method_exists($value, 'toArray')) {
$result[$key] = $value->toArray();
}
else {
$result[$key] = $value;
}
}
return $result;
}
/**
* Converts our class to json string
* Also used by Zend_Json then encoding objects
* @see Zend_Json::encode
*
* @return string
*
*/
public function toJson()
{
return json_encode($this->toObject());
}
public function __clone() {
$this->_object = clone $this->_object;
}
public function __destruct() {
//unset($this->_object);
}
}