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