/includes/api/stripe-php/lib/Util/Util.php

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