2012-04-05 01:09:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
abstract class Stripe_Util
|
|
|
|
{
|
2014-07-13 18:19:07 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-04-05 01:09:29 +02:00
|
|
|
public static function isList($array)
|
|
|
|
{
|
|
|
|
if (!is_array($array))
|
|
|
|
return false;
|
2014-07-13 18:19:07 +02:00
|
|
|
|
|
|
|
// TODO: generally incorrect, but it's correct given Stripe's response
|
2012-04-05 01:09:29 +02:00
|
|
|
foreach (array_keys($array) as $k) {
|
|
|
|
if (!is_numeric($k))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-13 18:19:07 +02:00
|
|
|
/**
|
|
|
|
* Recursively converts the PHP Stripe object to an array.
|
|
|
|
*
|
|
|
|
* @param array $values The PHP Stripe object to convert.
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-04-05 01:09:29 +02:00
|
|
|
public static function convertStripeObjectToArray($values)
|
|
|
|
{
|
|
|
|
$results = array();
|
|
|
|
foreach ($values as $k => $v) {
|
|
|
|
// FIXME: this is an encapsulation violation
|
2014-07-13 18:19:07 +02:00
|
|
|
if ($k[0] == '_') {
|
2012-04-05 01:09:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($v instanceof Stripe_Object) {
|
|
|
|
$results[$k] = $v->__toArray(true);
|
2014-07-13 18:19:07 +02:00
|
|
|
} else if (is_array($v)) {
|
2012-04-05 01:09:29 +02:00
|
|
|
$results[$k] = self::convertStripeObjectToArray($v);
|
2014-07-13 18:19:07 +02:00
|
|
|
} else {
|
2012-04-05 01:09:29 +02:00
|
|
|
$results[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2014-07-13 18:19:07 +02:00
|
|
|
/**
|
|
|
|
* Converts a response from the Stripe API to the corresponding PHP object.
|
|
|
|
*
|
|
|
|
* @param array $resp The response from the Stripe API.
|
|
|
|
* @param string $apiKey
|
|
|
|
* @return Stripe_Object|array
|
|
|
|
*/
|
2012-04-05 01:09:29 +02:00
|
|
|
public static function convertToStripeObject($resp, $apiKey)
|
|
|
|
{
|
2014-07-13 18:19:07 +02:00
|
|
|
$types = array(
|
|
|
|
'card' => 'Stripe_Card',
|
|
|
|
'charge' => 'Stripe_Charge',
|
|
|
|
'customer' => 'Stripe_Customer',
|
|
|
|
'list' => 'Stripe_List',
|
|
|
|
'invoice' => 'Stripe_Invoice',
|
|
|
|
'invoiceitem' => 'Stripe_InvoiceItem',
|
|
|
|
'event' => 'Stripe_Event',
|
|
|
|
'transfer' => 'Stripe_Transfer',
|
|
|
|
'plan' => 'Stripe_Plan',
|
|
|
|
'recipient' => 'Stripe_Recipient',
|
|
|
|
'refund' => 'Stripe_Refund',
|
|
|
|
'subscription' => 'Stripe_Subscription'
|
|
|
|
);
|
2012-04-05 01:09:29 +02:00
|
|
|
if (self::isList($resp)) {
|
|
|
|
$mapped = array();
|
|
|
|
foreach ($resp as $i)
|
|
|
|
array_push($mapped, self::convertToStripeObject($i, $apiKey));
|
|
|
|
return $mapped;
|
|
|
|
} else if (is_array($resp)) {
|
2014-07-13 18:19:07 +02:00
|
|
|
if (isset($resp['object'])
|
|
|
|
&& is_string($resp['object'])
|
|
|
|
&& isset($types[$resp['object']])) {
|
2012-04-05 01:09:29 +02:00
|
|
|
$class = $types[$resp['object']];
|
2014-07-13 18:19:07 +02:00
|
|
|
} else {
|
2012-04-05 01:09:29 +02:00
|
|
|
$class = 'Stripe_Object';
|
2014-07-13 18:19:07 +02:00
|
|
|
}
|
2012-04-05 01:09:29 +02:00
|
|
|
return Stripe_Object::scopedConstructFrom($class, $resp, $apiKey);
|
|
|
|
} else {
|
|
|
|
return $resp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|