ec4ae90970a47e677ef7b46fa6877420941fdbd4
[WP-Plugins/glm-member-db.git] /
1 <?php 
2 namespace PayPal\Common;
3
4 /**
5  * Generic Model class that all API domain classes extend
6  * Stores all member data in a hashmap that enables easy 
7  * JSON encoding/decoding
8  */
9 class PPModel {
10
11         private $_propMap = array();    
12                 
13         public function __get($key) {
14                 return $this->_propMap[$key];
15         }
16         
17         public function __set($key, $value) {
18                 $this->_propMap[$key] = $value;
19         }
20         
21         public function __isset($key) {
22                 return isset($this->_propMap[$key]);
23         }
24         
25         public function __unset($key) {
26                 unset($this->_propMap[$key]);
27         }
28         
29         
30         private function _convertToArray($param) {
31                 $ret = array();         
32                 foreach($param as $k => $v) {
33                         if($v instanceof PPModel ) {                            
34                                 $ret[$k] = $v->toArray();
35                         } else if (is_array($v)) {
36                                 $ret[$k] = $this->_convertToArray($v);
37                         } else {
38                                 $ret[$k] = $v;
39                         }
40                 }
41                 return $ret;
42         }
43         
44         public function fromArray($arr) {
45                 
46                 foreach($arr as $k => $v) {
47                         if(is_array($v)) {
48                                 $clazz = PPReflectionUtil::getPropertyClass(get_class($this), $k);
49                                 
50                                 if(PPArrayUtil::isAssocArray($v)) {
51                                         $o = new $clazz();
52                                         $o->fromArray($v);
53                                         $this->__set($k, $o);
54                                 } else {
55                                         $arr =  array();                
56                                         foreach($v as $nk => $nv) {
57                                                 if(is_array($nv)) {
58                                                         $o = new $clazz();
59                                                         $o->fromArray($nv);
60                                                         $arr[$nk] = $o;
61                                                 } else {
62                                                         $arr[$nk] = $nv;
63                                                 }
64                                         }
65                                         $this->__set($k, $arr);
66                                 } 
67                         }else {
68                                 $this->$k = $v;
69                         }
70                 }
71         }
72         
73         public function fromJson($json) {
74                 $this->fromArray(json_decode($json, true));
75         }
76         
77         public function toArray() {             
78                 return $this->_convertToArray($this->_propMap);
79         }
80         
81         public function toJSON() {              
82                 return json_encode($this->toArray());
83         }
84 }