PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / trunk
GiveWP – Donation Plugin and Fundraising Platform vtrunk
4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / vendor / stripe / stripe-php / lib / Util / Util.php
give / vendor / stripe / stripe-php / lib / Util Last commit date
CaseInsensitiveArray.php 2 years ago DefaultLogger.php 4 years ago LoggerInterface.php 4 years ago ObjectTypes.php 2 years ago RandomGenerator.php 2 years ago RequestOptions.php 4 years ago Set.php 2 years ago Util.php 4 years ago
Util.php
266 lines
1 <?php
2
3 namespace Stripe\Util;
4
5 use 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 $opts
41 *
42 * @return array|StripeObject
43 */
44 public static function convertToStripeObject($resp, $opts)
45 {
46 $types = \Stripe\Util\ObjectTypes::mapping;
47 if (self::isList($resp)) {
48 $mapped = [];
49 foreach ($resp as $i) {
50 $mapped[] = self::convertToStripeObject($i, $opts);
51 }
52
53 return $mapped;
54 }
55 if (\is_array($resp)) {
56 if (isset($resp['object']) && \is_string($resp['object']) && isset($types[$resp['object']])) {
57 $class = $types[$resp['object']];
58 } else {
59 $class = \Stripe\StripeObject::class;
60 }
61
62 return $class::constructFrom($resp, $opts);
63 }
64
65 return $resp;
66 }
67
68 /**
69 * @param mixed|string $value a string to UTF8-encode
70 *
71 * @return mixed|string the UTF8-encoded string, or the object passed in if
72 * it wasn't a string
73 */
74 public static function utf8($value)
75 {
76 if (null === self::$isMbstringAvailable) {
77 self::$isMbstringAvailable = \function_exists('mb_detect_encoding');
78
79 if (!self::$isMbstringAvailable) {
80 \trigger_error('It looks like the mbstring extension is not enabled. ' .
81 'UTF-8 strings will not properly be encoded. Ask your system ' .
82 'administrator to enable the mbstring extension, or write to ' .
83 'support@stripe.com if you have any questions.', \E_USER_WARNING);
84 }
85 }
86
87 if (\is_string($value) && self::$isMbstringAvailable && 'UTF-8' !== \mb_detect_encoding($value, 'UTF-8', true)) {
88 return \utf8_encode($value);
89 }
90
91 return $value;
92 }
93
94 /**
95 * Compares two strings for equality. The time taken is independent of the
96 * number of characters that match.
97 *
98 * @param string $a one of the strings to compare
99 * @param string $b the other string to compare
100 *
101 * @return bool true if the strings are equal, false otherwise
102 */
103 public static function secureCompare($a, $b)
104 {
105 if (null === self::$isHashEqualsAvailable) {
106 self::$isHashEqualsAvailable = \function_exists('hash_equals');
107 }
108
109 if (self::$isHashEqualsAvailable) {
110 return \hash_equals($a, $b);
111 }
112 if (\strlen($a) !== \strlen($b)) {
113 return false;
114 }
115
116 $result = 0;
117 for ($i = 0; $i < \strlen($a); ++$i) {
118 $result |= \ord($a[$i]) ^ \ord($b[$i]);
119 }
120
121 return 0 === $result;
122 }
123
124 /**
125 * Recursively goes through an array of parameters. If a parameter is an instance of
126 * ApiResource, then it is replaced by the resource's ID.
127 * Also clears out null values.
128 *
129 * @param mixed $h
130 *
131 * @return mixed
132 */
133 public static function objectsToIds($h)
134 {
135 if ($h instanceof \Stripe\ApiResource) {
136 return $h->id;
137 }
138 if (static::isList($h)) {
139 $results = [];
140 foreach ($h as $v) {
141 $results[] = static::objectsToIds($v);
142 }
143
144 return $results;
145 }
146 if (\is_array($h)) {
147 $results = [];
148 foreach ($h as $k => $v) {
149 if (null === $v) {
150 continue;
151 }
152 $results[$k] = static::objectsToIds($v);
153 }
154
155 return $results;
156 }
157
158 return $h;
159 }
160
161 /**
162 * @param array $params
163 *
164 * @return string
165 */
166 public static function encodeParameters($params)
167 {
168 $flattenedParams = self::flattenParams($params);
169 $pieces = [];
170 foreach ($flattenedParams as $param) {
171 list($k, $v) = $param;
172 $pieces[] = self::urlEncode($k) . '=' . self::urlEncode($v);
173 }
174
175 return \implode('&', $pieces);
176 }
177
178 /**
179 * @param array $params
180 * @param null|string $parentKey
181 *
182 * @return array
183 */
184 public static function flattenParams($params, $parentKey = null)
185 {
186 $result = [];
187
188 foreach ($params as $key => $value) {
189 $calculatedKey = $parentKey ? "{$parentKey}[{$key}]" : $key;
190
191 if (self::isList($value)) {
192 $result = \array_merge($result, self::flattenParamsList($value, $calculatedKey));
193 } elseif (\is_array($value)) {
194 $result = \array_merge($result, self::flattenParams($value, $calculatedKey));
195 } else {
196 \array_push($result, [$calculatedKey, $value]);
197 }
198 }
199
200 return $result;
201 }
202
203 /**
204 * @param array $value
205 * @param string $calculatedKey
206 *
207 * @return array
208 */
209 public static function flattenParamsList($value, $calculatedKey)
210 {
211 $result = [];
212
213 foreach ($value as $i => $elem) {
214 if (self::isList($elem)) {
215 $result = \array_merge($result, self::flattenParamsList($elem, $calculatedKey));
216 } elseif (\is_array($elem)) {
217 $result = \array_merge($result, self::flattenParams($elem, "{$calculatedKey}[{$i}]"));
218 } else {
219 \array_push($result, ["{$calculatedKey}[{$i}]", $elem]);
220 }
221 }
222
223 return $result;
224 }
225
226 /**
227 * @param string $key a string to URL-encode
228 *
229 * @return string the URL-encoded string
230 */
231 public static function urlEncode($key)
232 {
233 $s = \urlencode((string) $key);
234
235 // Don't use strict form encoding by changing the square bracket control
236 // characters back to their literals. This is fine by the server, and
237 // makes these parameter strings easier to read.
238 $s = \str_replace('%5B', '[', $s);
239
240 return \str_replace('%5D', ']', $s);
241 }
242
243 public static function normalizeId($id)
244 {
245 if (\is_array($id)) {
246 $params = $id;
247 $id = $params['id'];
248 unset($params['id']);
249 } else {
250 $params = [];
251 }
252
253 return [$id, $params];
254 }
255
256 /**
257 * Returns UNIX timestamp in milliseconds.
258 *
259 * @return int current time in millis
260 */
261 public static function currentTimeMillis()
262 {
263 return (int) \round(\microtime(true) * 1000);
264 }
265 }
266