PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / stripe / stripe-php / lib / Util / Util.php
ameliabooking / vendor / stripe / stripe-php / lib / Util Last commit date
ApiVersion.php 6 months ago CaseInsensitiveArray.php 6 months ago DefaultLogger.php 6 months ago EventTypes.php 6 months ago LoggerInterface.php 6 months ago ObjectTypes.php 6 months ago RandomGenerator.php 6 months ago RequestOptions.php 6 months ago Set.php 6 months ago Util.php 6 months ago
Util.php
361 lines
1 <?php
2
3 namespace AmeliaVendor\Stripe\Util;
4
5 use AmeliaVendor\Stripe\StripeObject;
6
7 abstract class Util
8 {
9 private static $isMbstringAvailable = null;
10 private static $isHashEqualsAvailable = null;
11
12 /**
13 * Whether the provided array (or other) is a list rather than a dictionary.
14 * A list is defined as an array for which all the keys are consecutive
15 * integers starting at 0. Empty arrays are considered to be lists.
16 *
17 * @param array|mixed $array
18 *
19 * @return bool true if the given object is a list
20 */
21 public static function isList($array)
22 {
23 if (!\is_array($array)) {
24 return false;
25 }
26 if ([] === $array) {
27 return true;
28 }
29 if (\array_keys($array) !== \range(0, \count($array) - 1)) {
30 return false;
31 }
32
33 return true;
34 }
35
36 /**
37 * Converts a response from the Stripe API to the corresponding PHP object.
38 *
39 * @param array $resp the response from the Stripe API
40 * @param array|RequestOptions $opts
41 * @param 'v1'|'v2' $apiMode whether the response is from a v1 or v2 API
42 *
43 * @return array|StripeObject
44 */
45 public static function convertToStripeObject($resp, $opts, $apiMode = 'v1')
46 {
47 $types = 'v1' === $apiMode ? ObjectTypes::mapping
48 : ObjectTypes::v2Mapping;
49 if (self::isList($resp)) {
50 $mapped = [];
51 foreach ($resp as $i) {
52 $mapped[] = self::convertToStripeObject($i, $opts, $apiMode);
53 }
54
55 return $mapped;
56 }
57 if (\is_array($resp)) {
58 if (isset($resp['object']) && \is_string($resp['object'])
59 && isset($types[$resp['object']])
60 ) {
61 $class = $types[$resp['object']];
62 if ('v2' === $apiMode && ('v2.core.event' === $resp['object'])) {
63 $eventTypes = EventTypes::thinEventMapping;
64 if (\array_key_exists('type', $resp) && \array_key_exists($resp['type'], $eventTypes)) {
65 $class = $eventTypes[$resp['type']];
66 } else {
67 $class = \AmeliaVendor\Stripe\V2\Event::class;
68 }
69 }
70 } elseif (\array_key_exists('data', $resp) && \array_key_exists('next_page_url', $resp)) {
71 // TODO: this is a horrible hack. The API needs
72 // to return something for `object` here.
73 $class = \AmeliaVendor\Stripe\V2\Collection::class;
74 } else {
75 $class = StripeObject::class;
76 }
77
78 return $class::constructFrom($resp, $opts, $apiMode);
79 }
80
81 return $resp;
82 }
83
84 /**
85 * @param mixed $json
86 * @param mixed $class
87 *
88 * @throws \ReflectionException
89 */
90 public static function json_decode_thin_event_object($json, $class)
91 {
92 $reflection = new \ReflectionClass($class);
93 $instance = $reflection->newInstanceWithoutConstructor();
94 $json = json_decode($json, true);
95 $properties = $reflection->getProperties();
96 foreach ($properties as $key => $property) {
97 if (\array_key_exists($property->getName(), $json)) {
98 if ('related_object' === $property->getName()) {
99 $related_object = new \AmeliaVendor\Stripe\RelatedObject();
100 $related_object->id = $json['related_object']['id'];
101 $related_object->url = $json['related_object']['url'];
102 $related_object->type = $json['related_object']['type'];
103 $property->setValue($instance, $related_object);
104 } elseif ('reason' === $property->getName()) {
105 $reason = new \AmeliaVendor\Stripe\Reason();
106 $reason->id = $json['reason']['id'];
107 $reason->idempotency_key = $json['reason']['idempotency_key'];
108 $property->setValue($instance, $reason);
109 } else {
110 $property->setAccessible(true);
111 $property->setValue($instance, $json[$property->getName()]);
112 }
113 }
114 }
115
116 return $instance;
117 }
118
119 /**
120 * @param mixed|string $value a string to UTF8-encode
121 *
122 * @return mixed|string the UTF8-encoded string, or the object passed in if
123 * it wasn't a string
124 */
125 public static function utf8($value)
126 {
127 if (null === self::$isMbstringAvailable) {
128 self::$isMbstringAvailable = \function_exists('mb_detect_encoding')
129 && \function_exists('mb_convert_encoding');
130
131 if (!self::$isMbstringAvailable) {
132 \trigger_error(
133 'It looks like the mbstring extension is not enabled. '
134 . 'UTF-8 strings will not properly be encoded. Ask your system '
135 . 'administrator to enable the mbstring extension, or write to '
136 . 'support@stripe.com if you have any questions.',
137 \E_USER_WARNING
138 );
139 }
140 }
141
142 if (\is_string($value) && self::$isMbstringAvailable
143 && 'UTF-8' !== \mb_detect_encoding($value, 'UTF-8', true)
144 ) {
145 return mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
146 }
147
148 return $value;
149 }
150
151 /**
152 * Compares two strings for equality. The time taken is independent of the
153 * number of characters that match.
154 *
155 * @param string $a one of the strings to compare
156 * @param string $b the other string to compare
157 *
158 * @return bool true if the strings are equal, false otherwise
159 */
160 public static function secureCompare($a, $b)
161 {
162 if (null === self::$isHashEqualsAvailable) {
163 self::$isHashEqualsAvailable = \function_exists('hash_equals');
164 }
165
166 if (self::$isHashEqualsAvailable) {
167 return \hash_equals($a, $b);
168 }
169 if (\strlen($a) !== \strlen($b)) {
170 return false;
171 }
172
173 $result = 0;
174 for ($i = 0; $i < \strlen($a); ++$i) {
175 $result |= \ord($a[$i]) ^ \ord($b[$i]);
176 }
177
178 return 0 === $result;
179 }
180
181 /**
182 * Recursively goes through an array of parameters. If a parameter is an instance of
183 * ApiResource, then it is replaced by the resource's ID.
184 * Also clears out null values.
185 *
186 * @param mixed $h
187 *
188 * @return mixed
189 */
190 public static function objectsToIds($h)
191 {
192 if ($h instanceof \AmeliaVendor\Stripe\ApiResource) {
193 return $h->id;
194 }
195 if (static::isList($h)) {
196 $results = [];
197 foreach ($h as $v) {
198 $results[] = static::objectsToIds($v);
199 }
200
201 return $results;
202 }
203 if (\is_array($h)) {
204 $results = [];
205 foreach ($h as $k => $v) {
206 if (null === $v) {
207 continue;
208 }
209 $results[$k] = static::objectsToIds($v);
210 }
211
212 return $results;
213 }
214
215 return $h;
216 }
217
218 /**
219 * @param array $params
220 * @param mixed $apiMode
221 *
222 * @return string
223 */
224 public static function encodeParameters($params, $apiMode = 'v1')
225 {
226 $flattenedParams = self::flattenParams($params, null, $apiMode);
227 $pieces = [];
228 foreach ($flattenedParams as $param) {
229 list($k, $v) = $param;
230 $pieces[] = self::urlEncode($k) . '=' . self::urlEncode($v);
231 }
232
233 return \implode('&', $pieces);
234 }
235
236 /**
237 * @param array $params
238 * @param null|string $parentKey
239 * @param mixed $apiMode
240 *
241 * @return array
242 */
243 public static function flattenParams(
244 $params,
245 $parentKey = null,
246 $apiMode = 'v1'
247 ) {
248 $result = [];
249
250 foreach ($params as $key => $value) {
251 $calculatedKey = $parentKey ? "{$parentKey}[{$key}]" : $key;
252 if (self::isList($value)) {
253 $result = \array_merge(
254 $result,
255 self::flattenParamsList($value, $calculatedKey, $apiMode)
256 );
257 } elseif (\is_array($value)) {
258 $result = \array_merge(
259 $result,
260 self::flattenParams($value, $calculatedKey, $apiMode)
261 );
262 } else {
263 $result[] = [$calculatedKey, $value];
264 }
265 }
266
267 return $result;
268 }
269
270 /**
271 * @param array $value
272 * @param string $calculatedKey
273 * @param mixed $apiMode
274 *
275 * @return array
276 */
277 public static function flattenParamsList(
278 $value,
279 $calculatedKey,
280 $apiMode = 'v1'
281 ) {
282 $result = [];
283
284 foreach ($value as $i => $elem) {
285 if (self::isList($elem)) {
286 $result = \array_merge(
287 $result,
288 self::flattenParamsList($elem, $calculatedKey)
289 );
290 } elseif (\is_array($elem)) {
291 $result = \array_merge(
292 $result,
293 self::flattenParams($elem, "{$calculatedKey}[{$i}]")
294 );
295 } else {
296 if ('v2' === $apiMode) {
297 $result[] = ["{$calculatedKey}", $elem];
298 } else {
299 $result[] = ["{$calculatedKey}[{$i}]", $elem];
300 }
301 }
302 }
303
304 return $result;
305 }
306
307 /**
308 * @param string $key a string to URL-encode
309 *
310 * @return string the URL-encoded string
311 */
312 public static function urlEncode($key)
313 {
314 $s = \urlencode((string) $key);
315
316 // Don't use strict form encoding by changing the square bracket control
317 // characters back to their literals. This is fine by the server, and
318 // makes these parameter strings easier to read.
319 $s = \str_replace('%5B', '[', $s);
320
321 return \str_replace('%5D', ']', $s);
322 }
323
324 public static function normalizeId($id)
325 {
326 if (\is_array($id)) {
327 // see https://github.com/stripe/stripe-php/pull/1602
328 if (!isset($id['id'])) {
329 return [null, $id];
330 }
331 $params = $id;
332 $id = $params['id'];
333 unset($params['id']);
334 } else {
335 $params = [];
336 }
337
338 return [$id, $params];
339 }
340
341 /**
342 * Returns UNIX timestamp in milliseconds.
343 *
344 * @return int current time in millis
345 */
346 public static function currentTimeMillis()
347 {
348 return (int) \round(\microtime(true) * 1000);
349 }
350
351 public static function getApiMode($path)
352 {
353 $apiMode = 'v1';
354 if ('/v2' === substr($path, 0, 3)) {
355 $apiMode = 'v2';
356 }
357
358 return $apiMode;
359 }
360 }
361