functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\MessageInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. use Psr\Http\Message\StreamInterface;
  6. use Psr\Http\Message\UriInterface;
  7. /**
  8. * Returns the string representation of an HTTP message.
  9. *
  10. * @param MessageInterface $message Message to convert to a string.
  11. *
  12. * @return string
  13. *
  14. * @deprecated str will be removed in guzzlehttp/psr7:2.0. Use Message::toString instead.
  15. */
  16. function str(MessageInterface $message)
  17. {
  18. return Message::toString($message);
  19. }
  20. /**
  21. * Returns a UriInterface for the given value.
  22. *
  23. * This function accepts a string or UriInterface and returns a
  24. * UriInterface for the given value. If the value is already a
  25. * UriInterface, it is returned as-is.
  26. *
  27. * @param string|UriInterface $uri
  28. *
  29. * @return UriInterface
  30. *
  31. * @throws \InvalidArgumentException
  32. *
  33. * @deprecated uri_for will be removed in guzzlehttp/psr7:2.0. Use Utils::uriFor instead.
  34. */
  35. function uri_for($uri)
  36. {
  37. return Utils::uriFor($uri);
  38. }
  39. /**
  40. * Create a new stream based on the input type.
  41. *
  42. * Options is an associative array that can contain the following keys:
  43. * - metadata: Array of custom metadata.
  44. * - size: Size of the stream.
  45. *
  46. * This method accepts the following `$resource` types:
  47. * - `Psr\Http\Message\StreamInterface`: Returns the value as-is.
  48. * - `string`: Creates a stream object that uses the given string as the contents.
  49. * - `resource`: Creates a stream object that wraps the given PHP stream resource.
  50. * - `Iterator`: If the provided value implements `Iterator`, then a read-only
  51. * stream object will be created that wraps the given iterable. Each time the
  52. * stream is read from, data from the iterator will fill a buffer and will be
  53. * continuously called until the buffer is equal to the requested read size.
  54. * Subsequent read calls will first read from the buffer and then call `next`
  55. * on the underlying iterator until it is exhausted.
  56. * - `object` with `__toString()`: If the object has the `__toString()` method,
  57. * the object will be cast to a string and then a stream will be returned that
  58. * uses the string value.
  59. * - `NULL`: When `null` is passed, an empty stream object is returned.
  60. * - `callable` When a callable is passed, a read-only stream object will be
  61. * created that invokes the given callable. The callable is invoked with the
  62. * number of suggested bytes to read. The callable can return any number of
  63. * bytes, but MUST return `false` when there is no more data to return. The
  64. * stream object that wraps the callable will invoke the callable until the
  65. * number of requested bytes are available. Any additional bytes will be
  66. * buffered and used in subsequent reads.
  67. *
  68. * @param resource|string|int|float|bool|StreamInterface|callable|\Iterator|null $resource Entity body data
  69. * @param array $options Additional options
  70. *
  71. * @return StreamInterface
  72. *
  73. * @throws \InvalidArgumentException if the $resource arg is not valid.
  74. *
  75. * @deprecated stream_for will be removed in guzzlehttp/psr7:2.0. Use Utils::streamFor instead.
  76. */
  77. function stream_for($resource = '', array $options = [])
  78. {
  79. return Utils::streamFor($resource, $options);
  80. }
  81. /**
  82. * Parse an array of header values containing ";" separated data into an
  83. * array of associative arrays representing the header key value pair data
  84. * of the header. When a parameter does not contain a value, but just
  85. * contains a key, this function will inject a key with a '' string value.
  86. *
  87. * @param string|array $header Header to parse into components.
  88. *
  89. * @return array Returns the parsed header values.
  90. *
  91. * @deprecated parse_header will be removed in guzzlehttp/psr7:2.0. Use Header::parse instead.
  92. */
  93. function parse_header($header)
  94. {
  95. return Header::parse($header);
  96. }
  97. /**
  98. * Converts an array of header values that may contain comma separated
  99. * headers into an array of headers with no comma separated values.
  100. *
  101. * @param string|array $header Header to normalize.
  102. *
  103. * @return array Returns the normalized header field values.
  104. *
  105. * @deprecated normalize_header will be removed in guzzlehttp/psr7:2.0. Use Header::normalize instead.
  106. */
  107. function normalize_header($header)
  108. {
  109. return Header::normalize($header);
  110. }
  111. /**
  112. * Clone and modify a request with the given changes.
  113. *
  114. * This method is useful for reducing the number of clones needed to mutate a
  115. * message.
  116. *
  117. * The changes can be one of:
  118. * - method: (string) Changes the HTTP method.
  119. * - set_headers: (array) Sets the given headers.
  120. * - remove_headers: (array) Remove the given headers.
  121. * - body: (mixed) Sets the given body.
  122. * - uri: (UriInterface) Set the URI.
  123. * - query: (string) Set the query string value of the URI.
  124. * - version: (string) Set the protocol version.
  125. *
  126. * @param RequestInterface $request Request to clone and modify.
  127. * @param array $changes Changes to apply.
  128. *
  129. * @return RequestInterface
  130. *
  131. * @deprecated modify_request will be removed in guzzlehttp/psr7:2.0. Use Utils::modifyRequest instead.
  132. */
  133. function modify_request(RequestInterface $request, array $changes)
  134. {
  135. return Utils::modifyRequest($request, $changes);
  136. }
  137. /**
  138. * Attempts to rewind a message body and throws an exception on failure.
  139. *
  140. * The body of the message will only be rewound if a call to `tell()` returns a
  141. * value other than `0`.
  142. *
  143. * @param MessageInterface $message Message to rewind
  144. *
  145. * @throws \RuntimeException
  146. *
  147. * @deprecated rewind_body will be removed in guzzlehttp/psr7:2.0. Use Message::rewindBody instead.
  148. */
  149. function rewind_body(MessageInterface $message)
  150. {
  151. Message::rewindBody($message);
  152. }
  153. /**
  154. * Safely opens a PHP stream resource using a filename.
  155. *
  156. * When fopen fails, PHP normally raises a warning. This function adds an
  157. * error handler that checks for errors and throws an exception instead.
  158. *
  159. * @param string $filename File to open
  160. * @param string $mode Mode used to open the file
  161. *
  162. * @return resource
  163. *
  164. * @throws \RuntimeException if the file cannot be opened
  165. *
  166. * @deprecated try_fopen will be removed in guzzlehttp/psr7:2.0. Use Utils::tryFopen instead.
  167. */
  168. function try_fopen($filename, $mode)
  169. {
  170. return Utils::tryFopen($filename, $mode);
  171. }
  172. /**
  173. * Copy the contents of a stream into a string until the given number of
  174. * bytes have been read.
  175. *
  176. * @param StreamInterface $stream Stream to read
  177. * @param int $maxLen Maximum number of bytes to read. Pass -1
  178. * to read the entire stream.
  179. *
  180. * @return string
  181. *
  182. * @throws \RuntimeException on error.
  183. *
  184. * @deprecated copy_to_string will be removed in guzzlehttp/psr7:2.0. Use Utils::copyToString instead.
  185. */
  186. function copy_to_string(StreamInterface $stream, $maxLen = -1)
  187. {
  188. return Utils::copyToString($stream, $maxLen);
  189. }
  190. /**
  191. * Copy the contents of a stream into another stream until the given number
  192. * of bytes have been read.
  193. *
  194. * @param StreamInterface $source Stream to read from
  195. * @param StreamInterface $dest Stream to write to
  196. * @param int $maxLen Maximum number of bytes to read. Pass -1
  197. * to read the entire stream.
  198. *
  199. * @throws \RuntimeException on error.
  200. *
  201. * @deprecated copy_to_stream will be removed in guzzlehttp/psr7:2.0. Use Utils::copyToStream instead.
  202. */
  203. function copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen = -1)
  204. {
  205. return Utils::copyToStream($source, $dest, $maxLen);
  206. }
  207. /**
  208. * Calculate a hash of a stream.
  209. *
  210. * This method reads the entire stream to calculate a rolling hash, based on
  211. * PHP's `hash_init` functions.
  212. *
  213. * @param StreamInterface $stream Stream to calculate the hash for
  214. * @param string $algo Hash algorithm (e.g. md5, crc32, etc)
  215. * @param bool $rawOutput Whether or not to use raw output
  216. *
  217. * @return string Returns the hash of the stream
  218. *
  219. * @throws \RuntimeException on error.
  220. *
  221. * @deprecated hash will be removed in guzzlehttp/psr7:2.0. Use Utils::hash instead.
  222. */
  223. function hash(StreamInterface $stream, $algo, $rawOutput = false)
  224. {
  225. return Utils::hash($stream, $algo, $rawOutput);
  226. }
  227. /**
  228. * Read a line from the stream up to the maximum allowed buffer length.
  229. *
  230. * @param StreamInterface $stream Stream to read from
  231. * @param int|null $maxLength Maximum buffer length
  232. *
  233. * @return string
  234. *
  235. * @deprecated readline will be removed in guzzlehttp/psr7:2.0. Use Utils::readLine instead.
  236. */
  237. function readline(StreamInterface $stream, $maxLength = null)
  238. {
  239. return Utils::readLine($stream, $maxLength);
  240. }
  241. /**
  242. * Parses a request message string into a request object.
  243. *
  244. * @param string $message Request message string.
  245. *
  246. * @return Request
  247. *
  248. * @deprecated parse_request will be removed in guzzlehttp/psr7:2.0. Use Message::parseRequest instead.
  249. */
  250. function parse_request($message)
  251. {
  252. return Message::parseRequest($message);
  253. }
  254. /**
  255. * Parses a response message string into a response object.
  256. *
  257. * @param string $message Response message string.
  258. *
  259. * @return Response
  260. *
  261. * @deprecated parse_response will be removed in guzzlehttp/psr7:2.0. Use Message::parseResponse instead.
  262. */
  263. function parse_response($message)
  264. {
  265. return Message::parseResponse($message);
  266. }
  267. /**
  268. * Parse a query string into an associative array.
  269. *
  270. * If multiple values are found for the same key, the value of that key value
  271. * pair will become an array. This function does not parse nested PHP style
  272. * arrays into an associative array (e.g., `foo[a]=1&foo[b]=2` will be parsed
  273. * into `['foo[a]' => '1', 'foo[b]' => '2'])`.
  274. *
  275. * @param string $str Query string to parse
  276. * @param int|bool $urlEncoding How the query string is encoded
  277. *
  278. * @return array
  279. *
  280. * @deprecated parse_query will be removed in guzzlehttp/psr7:2.0. Use Query::parse instead.
  281. */
  282. function parse_query($str, $urlEncoding = true)
  283. {
  284. return Query::parse($str, $urlEncoding);
  285. }
  286. /**
  287. * Build a query string from an array of key value pairs.
  288. *
  289. * This function can use the return value of `parse_query()` to build a query
  290. * string. This function does not modify the provided keys when an array is
  291. * encountered (like `http_build_query()` would).
  292. *
  293. * @param array $params Query string parameters.
  294. * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
  295. * to encode using RFC3986, or PHP_QUERY_RFC1738
  296. * to encode using RFC1738.
  297. *
  298. * @return string
  299. *
  300. * @deprecated build_query will be removed in guzzlehttp/psr7:2.0. Use Query::build instead.
  301. */
  302. function build_query(array $params, $encoding = PHP_QUERY_RFC3986)
  303. {
  304. return Query::build($params, $encoding);
  305. }
  306. /**
  307. * Determines the mimetype of a file by looking at its extension.
  308. *
  309. * @param string $filename
  310. *
  311. * @return string|null
  312. *
  313. * @deprecated mimetype_from_filename will be removed in guzzlehttp/psr7:2.0. Use MimeType::fromFilename instead.
  314. */
  315. function mimetype_from_filename($filename)
  316. {
  317. return MimeType::fromFilename($filename);
  318. }
  319. /**
  320. * Maps a file extensions to a mimetype.
  321. *
  322. * @param $extension string The file extension.
  323. *
  324. * @return string|null
  325. *
  326. * @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
  327. * @deprecated mimetype_from_extension will be removed in guzzlehttp/psr7:2.0. Use MimeType::fromExtension instead.
  328. */
  329. function mimetype_from_extension($extension)
  330. {
  331. return MimeType::fromExtension($extension);
  332. }
  333. /**
  334. * Parses an HTTP message into an associative array.
  335. *
  336. * The array contains the "start-line" key containing the start line of
  337. * the message, "headers" key containing an associative array of header
  338. * array values, and a "body" key containing the body of the message.
  339. *
  340. * @param string $message HTTP request or response to parse.
  341. *
  342. * @return array
  343. *
  344. * @internal
  345. *
  346. * @deprecated _parse_message will be removed in guzzlehttp/psr7:2.0. Use Message::parseMessage instead.
  347. */
  348. function _parse_message($message)
  349. {
  350. return Message::parseMessage($message);
  351. }
  352. /**
  353. * Constructs a URI for an HTTP request message.
  354. *
  355. * @param string $path Path from the start-line
  356. * @param array $headers Array of headers (each value an array).
  357. *
  358. * @return string
  359. *
  360. * @internal
  361. *
  362. * @deprecated _parse_request_uri will be removed in guzzlehttp/psr7:2.0. Use Message::parseRequestUri instead.
  363. */
  364. function _parse_request_uri($path, array $headers)
  365. {
  366. return Message::parseRequestUri($path, $headers);
  367. }
  368. /**
  369. * Get a short summary of the message body.
  370. *
  371. * Will return `null` if the response is not printable.
  372. *
  373. * @param MessageInterface $message The message to get the body summary
  374. * @param int $truncateAt The maximum allowed size of the summary
  375. *
  376. * @return string|null
  377. *
  378. * @deprecated get_message_body_summary will be removed in guzzlehttp/psr7:2.0. Use Message::bodySummary instead.
  379. */
  380. function get_message_body_summary(MessageInterface $message, $truncateAt = 120)
  381. {
  382. return Message::bodySummary($message, $truncateAt);
  383. }
  384. /**
  385. * Remove the items given by the keys, case insensitively from the data.
  386. *
  387. * @param iterable<string> $keys
  388. *
  389. * @return array
  390. *
  391. * @internal
  392. *
  393. * @deprecated _caseless_remove will be removed in guzzlehttp/psr7:2.0. Use Utils::caselessRemove instead.
  394. */
  395. function _caseless_remove($keys, array $data)
  396. {
  397. return Utils::caselessRemove($keys, $data);
  398. }