[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/stripe-php/lib/Stripe/ -> Util.php (source)

   1  <?php
   2  
   3  abstract class Stripe_Util
   4  {
   5    /**
   6     * Whether the provided array (or other) is a list rather than a dictionary.
   7     *
   8     * @param array|mixed $array
   9     * @return boolean True if the given object is a list.
  10     */
  11    public static function isList($array)
  12    {
  13      if (!is_array($array))
  14        return false;
  15  
  16      // TODO: generally incorrect, but it's correct given Stripe's response
  17      foreach (array_keys($array) as $k) {
  18        if (!is_numeric($k))
  19          return false;
  20      }
  21      return true;
  22    }
  23  
  24    /**
  25     * Recursively converts the PHP Stripe object to an array.
  26     *
  27     * @param array $values The PHP Stripe object to convert.
  28     * @return array
  29     */
  30    public static function convertStripeObjectToArray($values)
  31    {
  32      $results = array();
  33      foreach ($values as $k => $v) {
  34        // FIXME: this is an encapsulation violation
  35        if ($k[0] == '_') {
  36          continue;
  37        }
  38        if ($v instanceof Stripe_Object) {
  39          $results[$k] = $v->__toArray(true);
  40        } else if (is_array($v)) {
  41          $results[$k] = self::convertStripeObjectToArray($v);
  42        } else {
  43          $results[$k] = $v;
  44        }
  45      }
  46      return $results;
  47    }
  48  
  49    /**
  50     * Converts a response from the Stripe API to the corresponding PHP object.
  51     *
  52     * @param array $resp The response from the Stripe API.
  53     * @param string $apiKey
  54     * @return Stripe_Object|array
  55     */
  56    public static function convertToStripeObject($resp, $apiKey)
  57    {
  58      $types = array(
  59        'card' => 'Stripe_Card',
  60        'charge' => 'Stripe_Charge',
  61        'customer' => 'Stripe_Customer',
  62        'list' => 'Stripe_List',
  63        'invoice' => 'Stripe_Invoice',
  64        'invoiceitem' => 'Stripe_InvoiceItem',
  65        'event' => 'Stripe_Event',
  66        'transfer' => 'Stripe_Transfer',
  67        'plan' => 'Stripe_Plan',
  68        'recipient' => 'Stripe_Recipient',
  69        'refund' => 'Stripe_Refund',
  70        'subscription' => 'Stripe_Subscription'
  71      );
  72      if (self::isList($resp)) {
  73        $mapped = array();
  74        foreach ($resp as $i)
  75          array_push($mapped, self::convertToStripeObject($i, $apiKey));
  76        return $mapped;
  77      } else if (is_array($resp)) {
  78        if (isset($resp['object']) 
  79            && is_string($resp['object'])
  80            && isset($types[$resp['object']])) {
  81          $class = $types[$resp['object']];
  82        } else {
  83          $class = 'Stripe_Object';
  84        }
  85        return Stripe_Object::scopedConstructFrom($class, $resp, $apiKey);
  86      } else {
  87        return $resp;
  88      }
  89    }
  90  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1