Util.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Pingpp\Util;
  3. use Pingpp\PingppObject;
  4. use stdClass;
  5. abstract class Util
  6. {
  7. /**
  8. * Whether the provided array (or other) is a list rather than a dictionary.
  9. *
  10. * @param array|mixed $array
  11. * @return boolean True if the given object is a list.
  12. */
  13. public static function isList($array)
  14. {
  15. if (!is_array($array))
  16. return false;
  17. // TODO: generally incorrect, but it's correct given Pingpp's response
  18. foreach (array_keys($array) as $k) {
  19. if (!is_numeric($k))
  20. return false;
  21. }
  22. return true;
  23. }
  24. /**
  25. * Recursively converts the PHP Pingpp object to an array.
  26. *
  27. * @param array $values The PHP Pingpp object to convert.
  28. * @param bool
  29. * @return array
  30. */
  31. public static function convertPingppObjectToArray($values, $keep_object = false)
  32. {
  33. $results = array();
  34. foreach ($values as $k => $v) {
  35. // FIXME: this is an encapsulation violation
  36. if ($k[0] == '_') {
  37. continue;
  38. }
  39. if ($v instanceof PingppObject) {
  40. $results[$k] = $keep_object ? $v->__toStdObject(true) : $v->__toArray(true);
  41. } else if (is_array($v)) {
  42. $results[$k] = self::convertPingppObjectToArray($v, $keep_object);
  43. } else {
  44. $results[$k] = $v;
  45. }
  46. }
  47. return $results;
  48. }
  49. /**
  50. * Recursively converts the PHP Pingpp object to an stdObject.
  51. *
  52. * @param array $values The PHP Pingpp object to convert.
  53. * @return array
  54. */
  55. public static function convertPingppObjectToStdObject($values)
  56. {
  57. $results = new stdClass;
  58. foreach ($values as $k => $v) {
  59. // FIXME: this is an encapsulation violation
  60. if ($k[0] == '_') {
  61. continue;
  62. }
  63. if ($v instanceof PingppObject) {
  64. $results->$k = $v->__toStdObject(true);
  65. } else if (is_array($v)) {
  66. $results->$k = self::convertPingppObjectToArray($v, true);
  67. } else {
  68. $results->$k = $v;
  69. }
  70. }
  71. return $results;
  72. }
  73. /**
  74. * Converts a response from the Pingpp API to the corresponding PHP object.
  75. *
  76. * @param stdObject $resp The response from the Pingpp API.
  77. * @param array $opts
  78. * @return PingppObject|array
  79. */
  80. public static function convertToPingppObject($resp, $opts)
  81. {
  82. $types = array(
  83. 'red_envelope'=>'Pingpp\\RedEnvelope',
  84. 'charge' => 'Pingpp\\Charge',
  85. 'list' => 'Pingpp\\Collection',
  86. 'refund' => 'Pingpp\\Refund',
  87. 'event' => 'Pingpp\\Event',
  88. 'transfer' => 'Pingpp\\Transfer',
  89. 'customer' => 'Pingpp\\Customer',
  90. 'card' => 'Pingpp\\Card',
  91. 'sms_code' => 'Pingpp\\SmsCode',
  92. 'card_info' => 'Pingpp\\CardInfo',
  93. 'token' => 'Pingpp\\Token'
  94. );
  95. if (self::isList($resp)) {
  96. $mapped = array();
  97. foreach ($resp as $i)
  98. array_push($mapped, self::convertToPingppObject($i, $opts));
  99. return $mapped;
  100. } else if (is_object($resp)) {
  101. if (isset($resp->object)
  102. && is_string($resp->object)
  103. && isset($types[$resp->object])) {
  104. $class = $types[$resp->object];
  105. } else {
  106. $class = 'Pingpp\\PingppObject';
  107. }
  108. return $class::constructFrom($resp, $opts);
  109. } else {
  110. return $resp;
  111. }
  112. }
  113. public static function getRequestHeaders()
  114. {
  115. if (function_exists('getallheaders')) {
  116. $headers = array();
  117. foreach (getallheaders() as $name => $value) {
  118. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $name))))] = $value;
  119. }
  120. return $headers;
  121. }
  122. $headers = array();
  123. foreach ($_SERVER as $name => $value) {
  124. if (substr($name, 0, 5) == 'HTTP_') {
  125. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  126. }
  127. }
  128. return $headers;
  129. }
  130. /**
  131. * @param string|mixed $value A string to UTF8-encode.
  132. *
  133. * @returns string|mixed The UTF8-encoded string, or the object passed in if
  134. * it wasn't a string.
  135. */
  136. public static function utf8($value)
  137. {
  138. if (is_string($value)
  139. && mb_detect_encoding($value, "UTF-8", TRUE) != "UTF-8") {
  140. return utf8_encode($value);
  141. } else {
  142. return $value;
  143. }
  144. }
  145. }