Changeset View
Changeset View
Standalone View
Standalone View
externals/stripe-php/lib/Stripe/Util.php
| <?php | <?php | ||||
| abstract class Stripe_Util | abstract class Stripe_Util | ||||
| { | { | ||||
| /** | |||||
| * 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) | public static function isList($array) | ||||
| { | { | ||||
| if (!is_array($array)) | if (!is_array($array)) | ||||
| return false; | return false; | ||||
| // TODO: this isn't actually correct in general, but it's correct given Stripe's responses | |||||
| // TODO: generally incorrect, but it's correct given Stripe's response | |||||
| foreach (array_keys($array) as $k) { | foreach (array_keys($array) as $k) { | ||||
| if (!is_numeric($k)) | if (!is_numeric($k)) | ||||
| return false; | return false; | ||||
| } | } | ||||
| return true; | 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) | public static function convertStripeObjectToArray($values) | ||||
| { | { | ||||
| $results = array(); | $results = array(); | ||||
| foreach ($values as $k => $v) { | foreach ($values as $k => $v) { | ||||
| // FIXME: this is an encapsulation violation | // FIXME: this is an encapsulation violation | ||||
| if (Stripe_Object::$_permanentAttributes->includes($k)) { | if ($k[0] == '_') { | ||||
| continue; | continue; | ||||
| } | } | ||||
| if ($v instanceof Stripe_Object) { | if ($v instanceof Stripe_Object) { | ||||
| $results[$k] = $v->__toArray(true); | $results[$k] = $v->__toArray(true); | ||||
| } | } else if (is_array($v)) { | ||||
| else if (is_array($v)) { | |||||
| $results[$k] = self::convertStripeObjectToArray($v); | $results[$k] = self::convertStripeObjectToArray($v); | ||||
| } | } else { | ||||
| else { | |||||
| $results[$k] = $v; | $results[$k] = $v; | ||||
| } | } | ||||
| } | } | ||||
| return $results; | return $results; | ||||
| } | } | ||||
| /** | |||||
| * 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 | |||||
| */ | |||||
| public static function convertToStripeObject($resp, $apiKey) | public static function convertToStripeObject($resp, $apiKey) | ||||
| { | { | ||||
| $types = array('charge' => 'Stripe_Charge', | $types = array( | ||||
| 'card' => 'Stripe_Card', | |||||
| 'charge' => 'Stripe_Charge', | |||||
| 'customer' => 'Stripe_Customer', | 'customer' => 'Stripe_Customer', | ||||
| 'list' => 'Stripe_List', | |||||
| 'invoice' => 'Stripe_Invoice', | 'invoice' => 'Stripe_Invoice', | ||||
| 'invoiceitem' => 'Stripe_InvoiceItem', 'event' => 'Stripe_Event'); | 'invoiceitem' => 'Stripe_InvoiceItem', | ||||
| 'event' => 'Stripe_Event', | |||||
| 'transfer' => 'Stripe_Transfer', | |||||
| 'plan' => 'Stripe_Plan', | |||||
| 'recipient' => 'Stripe_Recipient', | |||||
| 'refund' => 'Stripe_Refund', | |||||
| 'subscription' => 'Stripe_Subscription' | |||||
| ); | |||||
| if (self::isList($resp)) { | if (self::isList($resp)) { | ||||
| $mapped = array(); | $mapped = array(); | ||||
| foreach ($resp as $i) | foreach ($resp as $i) | ||||
| array_push($mapped, self::convertToStripeObject($i, $apiKey)); | array_push($mapped, self::convertToStripeObject($i, $apiKey)); | ||||
| return $mapped; | return $mapped; | ||||
| } else if (is_array($resp)) { | } else if (is_array($resp)) { | ||||
| if (isset($resp['object']) && is_string($resp['object']) && isset($types[$resp['object']])) | if (isset($resp['object']) | ||||
| && is_string($resp['object']) | |||||
| && isset($types[$resp['object']])) { | |||||
| $class = $types[$resp['object']]; | $class = $types[$resp['object']]; | ||||
| else | } else { | ||||
| $class = 'Stripe_Object'; | $class = 'Stripe_Object'; | ||||
| } | |||||
| return Stripe_Object::scopedConstructFrom($class, $resp, $apiKey); | return Stripe_Object::scopedConstructFrom($class, $resp, $apiKey); | ||||
| } else { | } else { | ||||
| return $resp; | return $resp; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||