82be10ab1f0f2159be6ab8430de335c4c36105ac
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Core;
3 use PayPal\Core\PPUtils;
4 /**
5  * @author 
6  */
7 abstract class PPMessage
8 {
9
10         /**
11          * @param string $prefix
12          * @return string
13          */
14         public function toNVPString($prefix = '')
15         {
16                 $nvp = array();
17                 foreach (get_object_vars($this) as $property => $defaultValue) {
18                         
19                         if (($propertyValue = $this->{$property}) === NULL || $propertyValue == NULL) {
20                                 continue;
21                         }
22
23                         if (is_object($propertyValue)) {
24                                 $nvp[] = $propertyValue->toNVPString($prefix . $property . '.'); // prefix
25
26                         } elseif (is_array($defaultValue) || is_array($propertyValue)) {
27                                 foreach (array_values($propertyValue) as $i => $item) {
28                                         if (!is_object($item)){
29                         $nvp[] = $prefix . $property . "($i)" . '=' . urlencode($item);
30                                         }else{
31                         $nvp[] = $item->toNVPString($prefix . $property . "($i).");
32                     }
33                                 }
34
35                         } else {
36                                 // Handle classes with attributes
37                                 if($property == 'value' && ($anno = PPUtils::propertyAnnotations($this, $property)) != NULL && isset($anno['value']) ) {
38                                         $nvpKey = substr($prefix, 0, -1); // Remove the ending '.'
39                                 } else {
40                                         $nvpKey = $prefix . $property ;
41                                 }
42                                 $nvp[] = $nvpKey . '=' . urlencode($propertyValue);
43                         }
44                 }
45
46                 return implode('&', $nvp);
47         }
48
49
50
51         /**
52          * @param array $map
53          * @param string $prefix
54          */
55         public function init(array $map = array(), $prefix = '')
56         {
57                 if (empty($map)) {
58                         return;
59                 }
60
61                 $map = PPUtils::lowerKeys($map);
62
63                 foreach (get_object_vars($this) as $property => $defaultValue) {
64                         if (array_key_exists($propKey = strtolower($prefix . $property), $map) &&
65                                         $this->isBuiltInType(($type = PPUtils::propertyType($this, $property)))){
66                                 $type = PPUtils::propertyType($this, $property);                                
67                                 $this->{$property} = urldecode($map[$propKey]);
68                                 continue; // string
69
70                         } elseif (!$filtered = PPUtils::filterKeyPrefix($map, $propKey)) {
71                                 continue; // NULL
72                         }
73                 
74                         if (!class_exists($type = PPUtils::propertyType($this, $property)) && !$this->isBuiltInType($type)) {
75                                 trigger_error("Class $type not found.", E_USER_NOTICE);
76                                 continue; // just ignore
77                         }
78
79                         if (is_array($defaultValue) || PPUtils::isPropertyArray($this, $property)) { // array of objects                                
80                                 if($this->isBuiltInType($type)) { // Array of simple types                                      
81                                         foreach($filtered as $key => $value) {
82                                                 $this->{$property}[trim($key, "()")] = urldecode($value);
83                                         }
84                                 } else { // Array of complex objects    
85                                         $delim = '.';                                   
86                                         for ($i = 0; $itemValues = PPUtils::filterKeyPrefix($filtered, "($i)") ;$i++) {                                 
87                                                 $this->{$property}[$i] = $item = new $type();
88                                                 $item->init(PPUtils::filterKeyPrefix($itemValues, "."));
89                                                 if(array_key_exists("", $itemValues)) {
90                                                         $item->value = urldecode($itemValues[""]);
91                                                 }
92                                         }
93                                         // Handle cases where we have a list of objects
94                                         // with just the value present and all attributes values are null 
95                                         foreach($filtered as $key => $value) {                                          
96                                                 $idx = trim($key, "()");                                                
97                                                 if(is_numeric($idx) && (is_null($this->{$property}) || !array_key_exists($idx, $this->{$property})) ) {
98                                                         $this->{$property}[$idx] = new $type;
99                                                         $this->{$property}[$idx]->value = urldecode($value);
100                                                 }                                               
101                                         }
102                                 }                               
103                         } else { // one object
104                                 $this->{$property} = new $type();
105                                 $this->{$property}->init(PPUtils::filterKeyPrefix($filtered, '.')); // unprefix
106                                 if(array_key_exists("", $filtered)) {
107                                         $this->{$property}->value = urldecode($filtered[""]);
108                                 }
109                         }
110                 }
111         }
112
113         private function isBuiltInType($typeName) {
114                 static $types = array('string', 'int', 'integer', 'bool', 'boolean', 'float', 'decimal', 'long', 'datetime', 'double');
115                 return in_array(strtolower($typeName), $types);
116         }
117 }