Completed
Push — master ( b9ac23...46f1c6 )
by ARCANEDEV
7s
created

Util::convertStripeObjectToArray()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

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