Stripe\Util\Util::convertToStripeObject PHP Method

convertToStripeObject() public static method

Converts a response from the Stripe API to the corresponding PHP object.
public static convertToStripeObject ( array $resp, array $opts ) : Stripe\StripeObject | array
$resp array The response from the Stripe API.
$opts array
return Stripe\StripeObject | array
    public static function convertToStripeObject($resp, $opts)
    {
        $types = array('account' => 'Stripe\\Account', 'alipay_account' => 'Stripe\\AlipayAccount', 'apple_pay_domain' => 'Stripe\\ApplePayDomain', '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', 'transfer_reversal' => 'Stripe\\TransferReversal', 'order' => 'Stripe\\Order', 'order_return' => 'Stripe\\OrderReturn', 'plan' => 'Stripe\\Plan', 'product' => 'Stripe\\Product', 'recipient' => 'Stripe\\Recipient', 'refund' => 'Stripe\\Refund', 'sku' => 'Stripe\\SKU', 'source' => 'Stripe\\Source', 'subscription' => 'Stripe\\Subscription', 'subscription_item' => 'Stripe\\SubscriptionItem', 'three_d_secure' => 'Stripe\\ThreeDSecure', '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;
        }
    }

Usage Example

Example #1
0
 /**
  * Refreshes this object using the provided values.
  *
  * @param array $values
  * @param array $opts
  * @param boolean $partial Defaults to false.
  */
 public function refreshFrom($values, $opts, $partial = false)
 {
     $this->_opts = $opts;
     // Wipe old state before setting new.  This is useful for e.g. updating a
     // customer, where there is no persistent card parameter.  Mark those values
     // which don't persist as transient
     if ($partial) {
         $removed = new Util\Set();
     } else {
         $removed = array_diff(array_keys($this->_values), array_keys($values));
     }
     foreach ($removed as $k) {
         if (self::$permanentAttributes->includes($k)) {
             continue;
         }
         unset($this->{$k});
     }
     foreach ($values as $k => $v) {
         if (self::$permanentAttributes->includes($k) && isset($this[$k])) {
             continue;
         }
         if (self::$nestedUpdatableAttributes->includes($k) && is_array($v)) {
             $this->_values[$k] = AttachedObject::constructFrom($v, $opts);
         } else {
             $this->_values[$k] = Util\Util::convertToStripeObject($v, $opts);
         }
         $this->_transientValues->discard($k);
         $this->_unsavedValues->discard($k);
     }
 }