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