Completed
Pull Request — master (#20)
by ARCANEDEV
27:38 queued 12:37
created

Util::isInAvailableResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\Stripe\Utilities;
2
3
use Arcanedev\Stripe\Contracts\Utilities\UtilInterface;
4
use Arcanedev\Stripe\StripeObject;
5
6
/**
7
 * Class     Util
8
 *
9
 * @package  Arcanedev\Stripe\Utilities
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
abstract class Util implements UtilInterface
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * Available Resources.
20
     *
21
     * @var array
22
     */
23
    private static $resources = [
24
        'account'             => 'Arcanedev\\Stripe\\Resources\\Account',
25
        'alipay_account'      => 'Arcanedev\\Stripe\\Resources\\AlipayAccount',
26
        'balance_transaction' => 'Arcanedev\\Stripe\\Resources\\BalanceTransaction',
27
        'bank_account'        => 'Arcanedev\\Stripe\\Resources\\BankAccount',
28
        'bitcoin_receiver'    => 'Arcanedev\\Stripe\\Resources\\BitcoinReceiver',
29
        'bitcoin_transaction' => 'Arcanedev\\Stripe\\Resources\\BitcoinTransaction',
30
        'card'                => 'Arcanedev\\Stripe\\Resources\\Card',
31
        'charge'              => 'Arcanedev\\Stripe\\Resources\\Charge',
32
        'country_spec'        => 'Arcanedev\\Stripe\\Resources\\CountrySpec',
33
        'coupon'              => 'Arcanedev\\Stripe\\Resources\\Coupon',
34
        'customer'            => 'Arcanedev\\Stripe\\Resources\\Customer',
35
        'discount'            => 'Arcanedev\\Stripe\\Resources\\Discount',
36
        'dispute'             => 'Arcanedev\\Stripe\\Resources\\Dispute',
37
        'event'               => 'Arcanedev\\Stripe\\Resources\\Event',
38
        'fee_refund'          => 'Arcanedev\\Stripe\\Resources\\ApplicationFeeRefund',
39
        'file_upload'         => 'Arcanedev\\Stripe\\Resources\\FileUpload',
40
        'invoice'             => 'Arcanedev\\Stripe\\Resources\\Invoice',
41
        'invoiceitem'         => 'Arcanedev\\Stripe\\Resources\\InvoiceItem',
42
        'list'                => 'Arcanedev\\Stripe\\Collection',                     // List Object
43
        'order'               => 'Arcanedev\\Stripe\\Resources\\Order',
44
        'order_item'          => 'Arcanedev\\Stripe\\Resources\\OrderItem',
45
        'order_return'        => 'Arcanedev\\Stripe\\Resources\\OrderReturn',
46
        'plan'                => 'Arcanedev\\Stripe\\Resources\\Plan',
47
        'product'             => 'Arcanedev\\Stripe\\Resources\\Product',
48
        'recipient'           => 'Arcanedev\\Stripe\\Resources\\Recipient',
49
        'refund'              => 'Arcanedev\\Stripe\\Resources\\Refund',
50
        'sku'                 => 'Arcanedev\\Stripe\\Resources\\Sku',
51
        'subscription'        => 'Arcanedev\\Stripe\\Resources\\Subscription',
52
        'token'               => 'Arcanedev\\Stripe\\Resources\\Token',
53
        'transfer'            => 'Arcanedev\\Stripe\\Resources\\Transfer',
54
        'transfer_reversal'   => 'Arcanedev\\Stripe\\Resources\\TransferReversal',
55
    ];
56
57
    /* ------------------------------------------------------------------------------------------------
58
     |  Main Functions
59
     | ------------------------------------------------------------------------------------------------
60
     */
61
    /**
62
     * Recursively converts the PHP Stripe object to an array.
63
     *
64
     * @param  array  $values
65
     *
66
     * @return array
67
     */
68
    public static function convertStripeObjectToArray($values)
69
    {
70
        $results = [];
71
72
        foreach ($values as $k => $v) {
73
            // FIXME: this is an encapsulation violation
74
            if ($k[0] == '_') continue;
75
76 25
            if ($v instanceof StripeObject) {
77
                $results[$k] = $v->toArray(true);
78 25
            }
79
            elseif (is_array($v)) {
80 25
                $results[$k] = self::convertStripeObjectToArray($v);
81
            }
82 25
            else {
83
                $results[$k] = $v;
84 25
            }
85 15
        }
86 12
87 25
        return $results;
88 10
    }
89 8
90
    /**
91 25
     * Converts a response from the Stripe API to the corresponding PHP object.
92
     *
93 20
     * @param  array  $response
94
     * @param  array  $options
95 25
     *
96
     * @return \Arcanedev\Stripe\StripeObject|\Arcanedev\Stripe\StripeResource|\Arcanedev\Stripe\Collection|array
97
     */
98
    public static function convertToStripeObject($response, $options)
99
    {
100
        if (self::isList($response)) {
101
            return array_map(function($i) use ($options) {
102
                return self::convertToStripeObject($i, $options);
103
            }, $response);
104
        }
105
        elseif (is_array($response)) {
106 669
            return StripeObject::scopedConstructFrom(
107
                self::getClassTypeObject($response),
108 669
                $response,
109 610
                $options
110 435
            );
111 610
        }
112
113 669
        return $response;
114 669
    }
115 669
116 535
    /**
117
     * Get Class Type.
118 535
     *
119
     * @param  array  $response
120
     *
121 669
     * @return string
122
     */
123
    private static function getClassTypeObject($response)
124
    {
125
        return self::isClassTypeObjectExist($response)
126
            ? self::$resources[ $response['object'] ]
127
            : 'Arcanedev\\Stripe\\StripeObject';
128
    }
129
130
    /* ------------------------------------------------------------------------------------------------
131 669
     |  Check Functions
132
     | ------------------------------------------------------------------------------------------------
133 669
     */
134 649
    /**
135 649
     * Whether the provided array (or other) is a list rather than a dictionary.
136
     *
137
     * @param  mixed  $array
138 185
     *
139
     * @return bool
140
     */
141
    public static function isList($array)
142
    {
143
        if ( ! is_array($array)) return false;
144
145
        // TODO: generally incorrect, but it's correct given Stripe's response
146
        foreach (array_keys($array) as $k) {
147
            if ( ! is_numeric($k)) return false;
148 649
        }
149
150 649
        return true;
151
    }
152
153
    /**
154
     * Check if the object is a resource.
155
     *
156
     * @param  array  $response
157
     *
158
     * @return bool
159
     */
160
    private static function isClassTypeObjectExist($response)
161
    {
162
        if (isset($response['object']) && is_string($response['object'])) {
163
            return array_key_exists($response['object'], self::$resources);
164 674
        }
165
166 674
        return false;
167
    }
168
}
169