Completed
Push — master ( 8bbda0...ede634 )
by ARCANEDEV
8s
created

Util::convertToStripeObject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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