PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PPUtils.php

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