id; } if (static::isList($h)) { $results = []; foreach ($h as $v) { $results[] = static::objectsToIds($v); } return $results; } if (\is_array($h)) { $results = []; foreach ($h as $k => $v) { if (null === $v) { continue; } $results[$k] = static::objectsToIds($v); } return $results; } return $h; } /** * @param array $params * @param mixed $apiMode * * @return string */ public static function encodeParameters($params, $apiMode = 'v1') { $flattenedParams = self::flattenParams($params, null, $apiMode); $pieces = []; foreach ($flattenedParams as $param) { list($k, $v) = $param; $pieces[] = self::urlEncode($k) . '=' . self::urlEncode($v); } return \implode('&', $pieces); } /** * @param array $params * @param null|string $parentKey * @param mixed $apiMode * * @return array */ public static function flattenParams( $params, $parentKey = null, $apiMode = 'v1' ) { $result = []; foreach ($params as $key => $value) { $calculatedKey = $parentKey ? "{$parentKey}[{$key}]" : $key; if (self::isList($value)) { $result = \array_merge( $result, self::flattenParamsList($value, $calculatedKey, $apiMode) ); } elseif (\is_array($value)) { $result = \array_merge( $result, self::flattenParams($value, $calculatedKey, $apiMode) ); } else { $result[] = [$calculatedKey, $value]; } } return $result; } /** * @param array $value * @param string $calculatedKey * @param mixed $apiMode * * @return array */ public static function flattenParamsList( $value, $calculatedKey, $apiMode = 'v1' ) { $result = []; foreach ($value as $i => $elem) { if (self::isList($elem)) { $result = \array_merge( $result, self::flattenParamsList($elem, $calculatedKey) ); } elseif (\is_array($elem)) { $result = \array_merge( $result, self::flattenParams($elem, "{$calculatedKey}[{$i}]") ); } else { // Always use indexed format for arrays $result[] = ["{$calculatedKey}[{$i}]", $elem]; } } return $result; } /** * @param string $key a string to URL-encode * * @return string the URL-encoded string */ public static function urlEncode($key) { $s = \urlencode((string) $key); // Don't use strict form encoding by changing the square bracket control // characters back to their literals. This is fine by the server, and // makes these parameter strings easier to read. $s = \str_replace('%5B', '[', $s); return \str_replace('%5D', ']', $s); } public static function normalizeId($id) { if (\is_array($id)) { // see https://github.com/stripe/stripe-php/pull/1602 if (!isset($id['id'])) { return [null, $id]; } $params = $id; $id = $params['id']; unset($params['id']); } else { $params = []; } return [$id, $params]; } /** * Returns UNIX timestamp in milliseconds. * * @return int current time in millis */ public static function currentTimeMillis() { return (int) \round(\microtime(true) * 1000); } public static function getApiMode($path) { $apiMode = 'v1'; if ('/v2' === substr($path, 0, 3)) { $apiMode = 'v2'; } return $apiMode; } /** * Useful for determining if we should trust the object type when turning a response into a StripeObject. * * @param 'delete'|'get'|'post' $method the HTTP method * @param 'v1'|'v2' $apiMode the API version * * @return bool true if the method is a DELETE request for v2 API, false otherwise */ public static function isV2DeleteRequest($method, $apiMode) { return 'delete' === $method && 'v2' === $apiMode; } }