Util.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 'transfer_reversal' => 'Stripe\\TransferReversal',
  79. 'order' => 'Stripe\\Order',
  80. 'order_return' => 'Stripe\\OrderReturn',
  81. 'plan' => 'Stripe\\Plan',
  82. 'product' => 'Stripe\\Product',
  83. 'recipient' => 'Stripe\\Recipient',
  84. 'refund' => 'Stripe\\Refund',
  85. 'sku' => 'Stripe\\SKU',
  86. 'source' => 'Stripe\\Source',
  87. 'subscription' => 'Stripe\\Subscription',
  88. 'subscription_item' => 'Stripe\\SubscriptionItem',
  89. 'three_d_secure' => 'Stripe\\ThreeDSecure',
  90. 'fee_refund' => 'Stripe\\ApplicationFeeRefund',
  91. 'bitcoin_receiver' => 'Stripe\\BitcoinReceiver',
  92. 'bitcoin_transaction' => 'Stripe\\BitcoinTransaction',
  93. );
  94. if (self::isList($resp)) {
  95. $mapped = array();
  96. foreach ($resp as $i) {
  97. array_push($mapped, self::convertToStripeObject($i, $opts));
  98. }
  99. return $mapped;
  100. } elseif (is_array($resp)) {
  101. if (isset($resp['object']) && is_string($resp['object']) && isset($types[$resp['object']])) {
  102. $class = $types[$resp['object']];
  103. } else {
  104. $class = 'Stripe\\StripeObject';
  105. }
  106. return $class::constructFrom($resp, $opts);
  107. } else {
  108. return $resp;
  109. }
  110. }
  111. /**
  112. * @param string|mixed $value A string to UTF8-encode.
  113. *
  114. * @return string|mixed The UTF8-encoded string, or the object passed in if
  115. * it wasn't a string.
  116. */
  117. public static function utf8($value)
  118. {
  119. if (self::$isMbstringAvailable === null) {
  120. self::$isMbstringAvailable = function_exists('mb_detect_encoding');
  121. if (!self::$isMbstringAvailable) {
  122. trigger_error("It looks like the mbstring extension is not enabled. " .
  123. "UTF-8 strings will not properly be encoded. Ask your system " .
  124. "administrator to enable the mbstring extension, or write to " .
  125. "support@stripe.com if you have any questions.", E_USER_WARNING);
  126. }
  127. }
  128. if (is_string($value) && self::$isMbstringAvailable && mb_detect_encoding($value, "UTF-8", true) != "UTF-8") {
  129. return utf8_encode($value);
  130. } else {
  131. return $value;
  132. }
  133. }
  134. }