Util.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Stripe\Util;
  3. use Stripe\StripeObject;
  4. abstract class Util
  5. {
  6. private static $isMbstringAvailable = null;
  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. }
  18. // TODO: generally incorrect, but it's correct given Stripe's response
  19. foreach (array_keys($array) as $k) {
  20. if (!is_numeric($k)) {
  21. return false;
  22. }
  23. }
  24. return true;
  25. }
  26. /**
  27. * Recursively converts the PHP Stripe object to an array.
  28. *
  29. * @param array $values The PHP Stripe object to convert.
  30. * @return array
  31. */
  32. public static function convertStripeObjectToArray($values)
  33. {
  34. $results = array();
  35. foreach ($values as $k => $v) {
  36. // FIXME: this is an encapsulation violation
  37. if ($k[0] == '_') {
  38. continue;
  39. }
  40. if ($v instanceof StripeObject) {
  41. $results[$k] = $v->__toArray(true);
  42. } elseif (is_array($v)) {
  43. $results[$k] = self::convertStripeObjectToArray($v);
  44. } else {
  45. $results[$k] = $v;
  46. }
  47. }
  48. return $results;
  49. }
  50. /**
  51. * Converts a response from the Stripe API to the corresponding PHP object.
  52. *
  53. * @param array $resp The response from the Stripe API.
  54. * @param array $opts
  55. * @return StripeObject|array
  56. */
  57. public static function convertToStripeObject($resp, $opts)
  58. {
  59. $types = array(
  60. 'account' => 'Stripe\\Account',
  61. 'alipay_account' => 'Stripe\\AlipayAccount',
  62. 'apple_pay_domain' => 'Stripe\\ApplePayDomain',
  63. 'bank_account' => 'Stripe\\BankAccount',
  64. 'balance_transaction' => 'Stripe\\BalanceTransaction',
  65. 'card' => 'Stripe\\Card',
  66. 'charge' => 'Stripe\\Charge',
  67. 'country_spec' => 'Stripe\\CountrySpec',
  68. 'coupon' => 'Stripe\\Coupon',
  69. 'customer' => 'Stripe\\Customer',
  70. 'dispute' => 'Stripe\\Dispute',
  71. 'list' => 'Stripe\\Collection',
  72. 'invoice' => 'Stripe\\Invoice',
  73. 'invoiceitem' => 'Stripe\\InvoiceItem',
  74. 'event' => 'Stripe\\Event',
  75. 'file' => 'Stripe\\FileUpload',
  76. 'token' => 'Stripe\\Token',
  77. 'transfer' => 'Stripe\\Transfer',
  78. 'order' => 'Stripe\\Order',
  79. 'order_return' => 'Stripe\\OrderReturn',
  80. 'plan' => 'Stripe\\Plan',
  81. 'product' => 'Stripe\\Product',
  82. 'recipient' => 'Stripe\\Recipient',
  83. 'refund' => 'Stripe\\Refund',
  84. 'sku' => 'Stripe\\SKU',
  85. 'source' => 'Stripe\\Source',
  86. 'subscription' => 'Stripe\\Subscription',
  87. 'three_d_secure' => 'Stripe\\ThreeDSecure',
  88. 'fee_refund' => 'Stripe\\ApplicationFeeRefund',
  89. 'bitcoin_receiver' => 'Stripe\\BitcoinReceiver',
  90. 'bitcoin_transaction' => 'Stripe\\BitcoinTransaction',
  91. );
  92. if (self::isList($resp)) {
  93. $mapped = array();
  94. foreach ($resp as $i) {
  95. array_push($mapped, self::convertToStripeObject($i, $opts));
  96. }
  97. return $mapped;
  98. } elseif (is_array($resp)) {
  99. if (isset($resp['object']) && is_string($resp['object']) && isset($types[$resp['object']])) {
  100. $class = $types[$resp['object']];
  101. } else {
  102. $class = 'Stripe\\StripeObject';
  103. }
  104. return $class::constructFrom($resp, $opts);
  105. } else {
  106. return $resp;
  107. }
  108. }
  109. /**
  110. * @param string|mixed $value A string to UTF8-encode.
  111. *
  112. * @return string|mixed The UTF8-encoded string, or the object passed in if
  113. * it wasn't a string.
  114. */
  115. public static function utf8($value)
  116. {
  117. if (self::$isMbstringAvailable === null) {
  118. self::$isMbstringAvailable = function_exists('mb_detect_encoding');
  119. if (!self::$isMbstringAvailable) {
  120. trigger_error("It looks like the mbstring extension is not enabled. " .
  121. "UTF-8 strings will not properly be encoded. Ask your system " .
  122. "administrator to enable the mbstring extension, or write to " .
  123. "support@stripe.com if you have any questions.", E_USER_WARNING);
  124. }
  125. }
  126. if (is_string($value) && self::$isMbstringAvailable && mb_detect_encoding($value, "UTF-8", true) != "UTF-8") {
  127. return utf8_encode($value);
  128. } else {
  129. return $value;
  130. }
  131. }
  132. }