123bf4badbf1a69a559c5d36cf945ea61a3c2594
[WP-Plugins/glm-member-db-registrations.git] /
1 <?php
2 namespace PayPal\Core;
3 /**
4  * @author 
5  */
6 abstract class PPXmlMessage
7 {
8
9         /**
10          * @return string
11          */
12         public function toSOAP()
13         {
14                 return $this->toXMLString();
15         }
16
17
18
19         /**
20          * @return string
21          */
22         public function toXMLString()
23         {
24                 if (count($properties = get_object_vars($this)) >= 2 && array_key_exists('value', $properties)) {
25                         $attributes = array();
26                         foreach (array_keys($properties) as $property) {
27                                 if ($property === 'value') continue;
28                                 if (($annots = PPUtils::propertyAnnotations($this, $property)) && isset($annots['attribute'])) {
29                                         if (($propertyValue = $this->{$property}) === NULL || $propertyValue == NULL) {
30                                                 $attributes[] = NULL;
31                                                 continue;
32                                         }
33
34                                         $attributes[] = $property . '="' . PPUtils::escapeInvalidXmlCharsRegex($propertyValue) . '"';
35                                 }
36                         }
37
38                         if (count($attributes)) {
39                                 return implode(' ', $attributes) . '>' . PPUtils::escapeInvalidXmlCharsRegex($this->value);
40                         }
41                 }
42
43                 $xml = array();
44                 foreach ($properties as $property => $defaultValue) {
45                         if (($propertyValue = $this->{$property}) === NULL || $propertyValue == NULL) {
46                                 continue;
47                         }
48
49                         if (is_array($defaultValue) || is_array($propertyValue)) {
50                                 foreach ($propertyValue as $item) {
51                                         if (!is_object($item)) {
52                                                 $xml[] = $this->buildProperty($property, $item);
53                                         }else{
54                                                 $xml[] = $this->buildProperty($property, $item);
55                                         }
56                                 }
57
58                         } else {
59                                 $xml[] = $this->buildProperty($property, $propertyValue);
60                         }
61                 }
62
63                 return implode($xml);
64         }
65
66
67
68         /**
69          * @param string $property
70          * @param PPXmlMessage|string $value
71          * @param string $namespace
72          * @return string
73          */
74         private function buildProperty($property, $value, $namespace = 'ebl')
75         {
76                 $annotations = PPUtils::propertyAnnotations($this, $property);
77                 if (!empty($annotations['namespace'])) {
78                         $namespace = $annotations['namespace'];
79                 }
80                 if (!empty($annotations['name'])) {
81                         $property = $annotations['name'];
82                 }
83
84                 $el = '<' . $namespace . ':' . $property;
85                 if (!is_object($value)) {
86                         $el .= '>' . PPUtils::escapeInvalidXmlCharsRegex($value);
87
88                 } else {
89                         if (substr($value = $value->toXMLString(), 0, 1) === '<' || $value=='') {
90                                 $el .= '>' . $value;
91
92                         } else {
93                                 $el .= ' ' . $value;
94                         }
95                 }
96
97                 return $el . '</' . $namespace . ':' . $property . '>';
98         }
99
100
101
102         /**
103          * @param array $map
104          * @param string $prefix
105          */
106         public function init(array $map = array(), $prefix = '')
107         {
108                 if (empty($map)) {
109                         return;
110                 }
111
112                 if (($first = reset($map)) && !is_array($first) && !is_numeric(key($map))) {
113                         parent::init($map, $prefix);
114                         return;
115                 }
116
117                 $propertiesMap = PPUtils::objectProperties($this);
118                 $arrayCtr = array();            
119                 foreach ($map as $element) {
120                 
121                         if (empty($element) || empty($element['name'])) {
122                                 continue;
123
124                         } elseif (!array_key_exists($property = strtolower($element['name']), $propertiesMap)) {
125                                 if (!preg_match('~^(.+)[\[\(](\d+)[\]\)]$~', $property, $m)) {
126                                         continue;
127                                 }
128
129                                 $element['name'] = $m[1];
130                                 $element['num'] = $m[2];
131                         }
132                         $element['name'] = $propertiesMap[strtolower($element['name'])];
133                         if(PPUtils::isPropertyArray($this, $element['name'])) {                         
134                                 $arrayCtr[$element['name']] = isset($arrayCtr[$element['name']]) ? ($arrayCtr[$element['name']]+1) : 0;                         
135                                 $element['num'] = $arrayCtr[$element['name']];
136                         } 
137                         if (!empty($element["attributes"]) && is_array($element["attributes"])) {
138                                 foreach ($element["attributes"] as $key => $val) {
139                                         $element["children"][] = array(
140                                                 'name' => $key,
141                                                 'text' => $val,
142                                         );
143                                 }
144
145                                 if (isset($element['text'])) {
146                                         $element["children"][] = array(
147                                                 'name' => 'value',
148                                                 'text' => $element['text'],
149                                         );
150                                 }
151
152                                 $this->fillRelation($element['name'], $element);
153
154                         } elseif (!empty($element['text'])) {
155                                 $this->{$element['name']} = $element['text'];
156
157                         } elseif (!empty($element["children"]) && is_array($element["children"])) {
158                                 $this->fillRelation($element['name'], $element);
159                         }
160                 }               
161         }
162
163
164
165         /**
166          * @param string $property
167          * @param array $element
168          */
169         private function fillRelation($property, array $element)
170         {
171                 if (!class_exists($type = PPUtils::propertyType($this, $property))) {
172                         trigger_error("Class $type not found.", E_USER_NOTICE);
173                         return; // just ignore
174                 }
175
176                 if (isset($element['num'])) { // array of objects
177                         $this->{$property}[$element['num']] = $item = new $type();
178                         $item->init($element['children']);
179
180                 } else {
181                         $this->{$property} = new $type();
182                         $this->{$property}->init($element["children"]);
183                 }
184         }
185
186 }