3ef6d8b74bcab44c4e62f0dd4aacbe269701e9a4
[WP-Plugins/glm-member-db-registrations.git] /
1 <?php
2 namespace PayPal\Core;
3 class PPUtils
4 {
5
6         /**
7          *
8          * Convert a Name Value Pair (NVP) formatted string into
9          * an associative array taking care to urldecode array values
10          *
11          * @param string $nvpString
12          * @return array
13          */
14         public static function nvpToMap($nvpString)
15         {
16                 $ret = array();
17                 $params = explode("&", $nvpString);
18                 foreach ($params as $p) {
19                         list($k, $v) = explode("=", $p);
20                         $ret[$k] = urldecode($v);
21                 }
22                 return $ret;
23         }
24
25
26
27         /**
28          * Returns true if the array contains a key like $key
29          *
30          * @param array $map
31          * @param string $key
32          * @return bool
33          */
34         public static function array_match_key($map, $key)
35         {
36                 $replace = str_replace(array(
37                                 '(',
38                                 ')',
39                                 '.'
40                 ), array(
41                                 '\(',
42                                 '\)',
43                                 '\.'
44                 ), $key);
45
46                 $pattern = "/$replace*/";
47
48                 foreach ($map as $k => $v) {
49                         preg_match($pattern, $k, $matches);
50                         if (count($matches) > 0) {
51                                 return true;
52                         }
53                 }
54                 return false;
55         }
56
57
58
59         /**
60          * Get the local IP address. The client address is a required
61          * request parameter for some API calls
62          */
63         public static function getLocalIPAddress()
64         {
65                 if (array_key_exists("SERVER_ADDR", $_SERVER)) {
66                         // SERVER_ADDR is available only if we are running the CGI SAPI
67                         return $_SERVER['SERVER_ADDR'];
68
69                 } else {
70                         if (function_exists("gethostname")) {
71                                 // gethostname is available only in PHP >= v5.3
72                                 return gethostbyname(gethostname());
73
74                         } else {
75                                 // fallback if nothing works
76                                 return "127.0.0.1";
77                         }
78                 }
79         }
80
81         public static function xmlToArray($xmlInput)
82         {
83                 $xml = simplexml_load_string($xmlInput);
84
85                 $ns = $xml->getNamespaces(true);
86                 $soap = $xml->children($ns['SOAP-ENV']);
87                 $getChild = $soap->Body->children();
88                 $array = array();
89                 $ret = PPUtils::convertXmlObjToArr($getChild, $array);
90                 return $ret;
91         }
92
93
94
95         private static function convertXmlObjToArr($obj, &$arr)
96         {
97                 $children = $obj->children();
98                 foreach ($children as $elementName => $node) {
99                         $nextIdx = count($arr);
100                         $arr[$nextIdx] = array();
101                         $arr[$nextIdx]['name'] = strtolower((string)$elementName);
102                         $arr[$nextIdx]['attributes'] = array();
103                         $attributes = $node->attributes();
104                         foreach ($attributes as $attributeName => $attributeValue) {
105                                 $attribName = strtolower(trim((string)$attributeName));
106                                 $attribVal = trim((string)$attributeValue);
107                                 $arr[$nextIdx]['attributes'][$attribName] = $attribVal;
108                         }
109                         $text = (string)$node;
110                         $text = trim($text);
111                         if (strlen($text) > 0) {
112                                 $arr[$nextIdx]['text'] = $text;
113                         }
114                         $arr[$nextIdx]['children'] = array();
115                         PPutils::convertXmlObjToArr($node, $arr[$nextIdx]['children']);
116                 }
117                 return $arr;
118         }
119
120
121
122         /**
123          * Escapes invalid xml characters
124          *
125          * @param $textContent = xml data to be escaped
126          * @return string
127          */
128         public static function escapeInvalidXmlCharsRegex($textContent)
129         {
130                 return htmlspecialchars($textContent, (1 | 2), 'UTF-8', false);
131         }
132
133
134
135         /**
136          * @param array $map
137          * @param string $keyPrefix
138          * @return array
139          */
140         public static function filterKeyPrefix(array $map, $keyPrefix)
141         {
142                 $filtered = array();
143                 foreach ($map as $key => $val) {
144                         if (($pos = stripos($key, $keyPrefix)) !== 0) {
145                                 continue;
146                         }
147
148                         $filtered[substr_replace($key, '', 0, strlen($keyPrefix))] = $val;
149                 }
150
151                 return $filtered;
152         }
153
154
155
156         /**
157          * @var array|ReflectionProperty[]
158          */
159         private static $propertiesRefl = array();
160
161         /**
162          * @var array|string[]
163          */
164         private static $propertiesType = array();
165
166
167
168         /**
169          * @param string $class
170          * @param string $propertyName
171          * @throws RuntimeException
172          * @return string
173          */
174         public static function propertyAnnotations($class, $propertyName)
175         {
176                 $class = is_object($class) ? get_class($class) : $class;
177                 if (!class_exists('ReflectionProperty')) {
178                         throw new \RuntimeException("Property type of " . $class . "::{$propertyName} cannot be resolved");
179                 }
180
181                 if ($annotations =& self::$propertiesType[$class][$propertyName]) {
182                         return $annotations;
183                 }
184
185                 if (!($refl =& self::$propertiesRefl[$class][$propertyName])) {
186                         $refl = new \ReflectionProperty($class, $propertyName);
187                 }
188
189                 // todo: smarter regexp
190                 if (!preg_match_all('~\@([^\s@\(]+)[\t ]*(?:\(?([^\n@]+)\)?)?~i', $refl->getDocComment(), $annots, PREG_PATTERN_ORDER)) {
191                         return NULL;
192                 }
193                 foreach ($annots[1] as $i => $annot) {
194                         $annotations[strtolower($annot)] = empty($annots[2][$i]) ? TRUE : rtrim($annots[2][$i], " \t\n\r)");
195                 }
196
197                 return $annotations;
198         }
199
200         /**
201          * @param string $class
202          * @param string $propertyName
203          * @return string
204          */
205         public static function isAttributeProperty($class, $propertyName) {
206                 if (($annotations = self::propertyAnnotations($class, $property))) {
207                         return $annotations['attribute'];
208                 }
209                 return FALSE;
210         }
211
212         /**
213          * @param string $class
214          * @param string $propertyName
215          * @return string
216          */
217         public static function isPropertyArray($class, $propertyName) {
218                 if (($annotations = self::propertyAnnotations($class, $propertyName))) {
219                         if (isset($annotations['var']) && substr($annotations['var'], -2) === '[]') {
220                                 return TRUE;
221
222                         } elseif (isset($annotations['array'])) {
223                                 return TRUE;
224                         }
225                 }
226
227                 return FALSE;
228         }
229
230
231
232         /**
233          * @param string $class
234          * @param string $propertyName
235          * @throws RuntimeException
236          * @return string
237          */
238         public static function propertyType($class, $propertyName)
239         {
240                 if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['var'])) {
241                         if (substr($annotations['var'], -2) === '[]') {
242                                 return substr($annotations['var'], 0, -2);
243                         }
244
245                         return $annotations['var'];
246                 }
247
248                 return 'string';
249         }
250
251         /**
252          * @param object $object
253          * @return array
254          */
255         public static function objectProperties($object)
256         {
257                 $props = array();
258                 foreach (get_object_vars($object) as $property => $default) {
259                         $annotations = self::propertyAnnotations($object, $property);
260                         if (isset($annotations['name'])) {
261                                 $props[strtolower($annotations['name'])] = $property;
262                         }
263
264                         $props[strtolower($property)] = $property;
265                 }
266
267                 return $props;
268         }
269
270
271
272         /**
273          * @param array $array
274          * @return array
275          */
276         public static function lowerKeys(array $array)
277         {
278                 $ret = array();
279                 foreach ($array as $key => $value) {
280                         $ret[strtolower($key)] = $value;
281                 }
282
283                 return $ret;
284         }
285
286 }
287
288
289
290 /**
291  * XMLToArray Generator Class
292  *
293  * @author  :  MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
294  *             Moderator, phpResource (LINK1http://groups.yahoo.com/group/phpresource/LINK1)
295  *             URL: LINK2http://www.rupom.infoLINK2
296  * @version :  1.0
297  * @date       06/05/2006
298  * Purpose  : Creating Hierarchical Array from XML Data
299  * Released : Under GPL
300  */
301 class XmlToArray
302 {
303
304         var $xml = '';
305
306
307
308         /**
309          * Default Constructor
310          *
311          * @param $xml = xml data
312          * @return none
313          */
314         function XmlToArray($xml)
315         {
316                 $this->xml = $xml;
317         }
318
319
320
321         /**
322          * _struct_to_array($values, &$i)
323          *
324          * This is adds the contents of the return xml into the array for easier processing.
325          * Recursive, Static
326          *
327          * @access    private
328          * @param    array  $values this is the xml data in an array
329          * @param    int    $i  this is the current location in the array
330          * @return    Array
331          */
332         function _struct_to_array($values, &$i)
333         {
334                 $child = array();
335                 if (isset($values[$i]['value'])) {
336                         array_push($child, $values[$i]['value']);
337                 }
338
339                 while ($i++ < count($values)) {
340                         switch ($values[$i]['type']) {
341                                 case 'cdata':
342                                         array_push($child, $values[$i]['value']);
343                                         break;
344
345                                 case 'complete':
346                                         $name = $values[$i]['tag'];
347                                         if (!empty($name)) {
348                                                 $child[$name] = ($values[$i]['value']) ? ($values[$i]['value']) : '';
349                                                 if (isset($values[$i]['attributes'])) {
350                                                         $child[$name] = $values[$i]['attributes'];
351                                                 }
352                                         }
353                                         break;
354
355                                 case 'open':
356                                         $name = $values[$i]['tag'];
357                                         $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
358                                         $child[$name][$size] = $this->_struct_to_array($values, $i);
359                                         break;
360
361                                 case 'close':
362                                         return $child;
363                                         break;
364                         }
365                 }
366                 return $child;
367         }
368
369
370
371         /**
372          * createArray($data)
373          *
374          * This is adds the contents of the return xml into the array for easier processing.
375          *
376          * @access    public
377          * @return    Array
378          */
379         function createArray()
380         {
381                 $xml = $this->xml;
382                 $values = array();
383                 $index = array();
384                 $array = array();
385                 $parser = xml_parser_create();
386                 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
387                 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
388                 xml_parse_into_struct($parser, $xml, $values, $index);
389                 xml_parser_free($parser);
390                 $i = 0;
391                 $name = $values[$i]['tag'];
392                 $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
393                 $array[$name] = $this->_struct_to_array($values, $i);
394                 return $array;
395         }
396
397 }