PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/iwp-client/lib/amazon/guzzle/guzzle/src/Guzzle/Http/QueryString.php

https://gitlab.com/treighton/wpgit
PHP | 297 lines | 148 code | 39 blank | 110 comment | 23 complexity | 700632d6004f5e34b23c595c7bbabac2 MD5 | raw file
  1. <?php
  2. namespace Guzzle\Http;
  3. use Guzzle\Common\Collection;
  4. use Guzzle\Common\Exception\RuntimeException;
  5. use Guzzle\Http\QueryAggregator\DuplicateAggregator;
  6. use Guzzle\Http\QueryAggregator\QueryAggregatorInterface;
  7. use Guzzle\Http\QueryAggregator\PhpAggregator;
  8. /**
  9. * Query string object to handle managing query string parameters and aggregating those parameters together as a string.
  10. */
  11. class QueryString extends Collection
  12. {
  13. /** @var string Used to URL encode with rawurlencode */
  14. const RFC_3986 = 'RFC 3986';
  15. /** @var string Used to encode with urlencode */
  16. const FORM_URLENCODED = 'application/x-www-form-urlencoded';
  17. /** @var string Constant used to create blank query string values (e.g. ?foo) */
  18. const BLANK = "_guzzle_blank_";
  19. /** @var string The query string field separator (e.g. '&') */
  20. protected $fieldSeparator = '&';
  21. /** @var string The query string value separator (e.g. '=') */
  22. protected $valueSeparator = '=';
  23. /** @var bool URL encode fields and values */
  24. protected $urlEncode = 'RFC 3986';
  25. /** @var QueryAggregatorInterface */
  26. protected $aggregator;
  27. /** @var array Cached PHP aggregator */
  28. private static $defaultAggregator = null;
  29. /**
  30. * Parse a query string into a QueryString object
  31. *
  32. * @param string $query Query string to parse
  33. *
  34. * @return self
  35. */
  36. public static function fromString($query)
  37. {
  38. $q = new static();
  39. if ($query === '') {
  40. return $q;
  41. }
  42. $foundDuplicates = $foundPhpStyle = false;
  43. foreach (explode('&', $query) as $kvp) {
  44. $parts = explode('=', $kvp, 2);
  45. $key = rawurldecode($parts[0]);
  46. if ($paramIsPhpStyleArray = substr($key, -2) == '[]') {
  47. $foundPhpStyle = true;
  48. $key = substr($key, 0, -2);
  49. }
  50. if (isset($parts[1])) {
  51. $value = rawurldecode(str_replace('+', '%20', $parts[1]));
  52. if (isset($q[$key])) {
  53. $q->add($key, $value);
  54. $foundDuplicates = true;
  55. } elseif ($paramIsPhpStyleArray) {
  56. $q[$key] = array($value);
  57. } else {
  58. $q[$key] = $value;
  59. }
  60. } else {
  61. // Uses false by default to represent keys with no trailing "=" sign.
  62. $q->add($key, false);
  63. }
  64. }
  65. // Use the duplicate aggregator if duplicates were found and not using PHP style arrays
  66. if ($foundDuplicates && !$foundPhpStyle) {
  67. $q->setAggregator(new DuplicateAggregator());
  68. }
  69. return $q;
  70. }
  71. /**
  72. * Convert the query string parameters to a query string string
  73. *
  74. * @return string
  75. * @throws RuntimeException
  76. */
  77. public function __toString()
  78. {
  79. if (!$this->data) {
  80. return '';
  81. }
  82. $queryList = array();
  83. foreach ($this->prepareData($this->data) as $name => $value) {
  84. $queryList[] = $this->convertKvp($name, $value);
  85. }
  86. return implode($this->fieldSeparator, $queryList);
  87. }
  88. /**
  89. * Get the query string field separator
  90. *
  91. * @return string
  92. */
  93. public function getFieldSeparator()
  94. {
  95. return $this->fieldSeparator;
  96. }
  97. /**
  98. * Get the query string value separator
  99. *
  100. * @return string
  101. */
  102. public function getValueSeparator()
  103. {
  104. return $this->valueSeparator;
  105. }
  106. /**
  107. * Returns the type of URL encoding used by the query string
  108. *
  109. * One of: false, "RFC 3986", or "application/x-www-form-urlencoded"
  110. *
  111. * @return bool|string
  112. */
  113. public function getUrlEncoding()
  114. {
  115. return $this->urlEncode;
  116. }
  117. /**
  118. * Returns true or false if using URL encoding
  119. *
  120. * @return bool
  121. */
  122. public function isUrlEncoding()
  123. {
  124. return $this->urlEncode !== false;
  125. }
  126. /**
  127. * Provide a function for combining multi-valued query string parameters into a single or multiple fields
  128. *
  129. * @param null|QueryAggregatorInterface $aggregator Pass in a QueryAggregatorInterface object to handle converting
  130. * deeply nested query string variables into a flattened array.
  131. * Pass null to use the default PHP style aggregator. For legacy
  132. * reasons, this function accepts a callable that must accepts a
  133. * $key, $value, and query object.
  134. * @return self
  135. * @see \Guzzle\Http\QueryString::aggregateUsingComma()
  136. */
  137. public function setAggregator(QueryAggregatorInterface $aggregator = null)
  138. {
  139. // Use the default aggregator if none was set
  140. if (!$aggregator) {
  141. if (!self::$defaultAggregator) {
  142. self::$defaultAggregator = new PhpAggregator();
  143. }
  144. $aggregator = self::$defaultAggregator;
  145. }
  146. $this->aggregator = $aggregator;
  147. return $this;
  148. }
  149. /**
  150. * Set whether or not field names and values should be rawurlencoded
  151. *
  152. * @param bool|string $encode Set to TRUE to use RFC 3986 encoding (rawurlencode), false to disable encoding, or
  153. * form_urlencoding to use application/x-www-form-urlencoded encoding (urlencode)
  154. * @return self
  155. */
  156. public function useUrlEncoding($encode)
  157. {
  158. $this->urlEncode = ($encode === true) ? self::RFC_3986 : $encode;
  159. return $this;
  160. }
  161. /**
  162. * Set the query string separator
  163. *
  164. * @param string $separator The query string separator that will separate fields
  165. *
  166. * @return self
  167. */
  168. public function setFieldSeparator($separator)
  169. {
  170. $this->fieldSeparator = $separator;
  171. return $this;
  172. }
  173. /**
  174. * Set the query string value separator
  175. *
  176. * @param string $separator The query string separator that will separate values from fields
  177. *
  178. * @return self
  179. */
  180. public function setValueSeparator($separator)
  181. {
  182. $this->valueSeparator = $separator;
  183. return $this;
  184. }
  185. /**
  186. * Returns an array of url encoded field names and values
  187. *
  188. * @return array
  189. */
  190. public function urlEncode()
  191. {
  192. return $this->prepareData($this->data);
  193. }
  194. /**
  195. * URL encodes a value based on the url encoding type of the query string object
  196. *
  197. * @param string $value Value to encode
  198. *
  199. * @return string
  200. */
  201. public function encodeValue($value)
  202. {
  203. if ($this->urlEncode == self::RFC_3986) {
  204. return rawurlencode($value);
  205. } elseif ($this->urlEncode == self::FORM_URLENCODED) {
  206. return urlencode($value);
  207. } else {
  208. return (string) $value;
  209. }
  210. }
  211. /**
  212. * Url encode parameter data and convert nested query strings into a flattened hash.
  213. *
  214. * @param array $data The data to encode
  215. *
  216. * @return array Returns an array of encoded values and keys
  217. */
  218. protected function prepareData(array $data)
  219. {
  220. // If no aggregator is present then set the default
  221. if (!$this->aggregator) {
  222. $this->setAggregator(null);
  223. }
  224. $temp = array();
  225. foreach ($data as $key => $value) {
  226. if ($value === false || $value === null) {
  227. // False and null will not include the "=". Use an empty string to include the "=".
  228. $temp[$this->encodeValue($key)] = $value;
  229. } elseif (is_array($value)) {
  230. $temp = array_merge($temp, $this->aggregator->aggregate($key, $value, $this));
  231. } else {
  232. $temp[$this->encodeValue($key)] = $this->encodeValue($value);
  233. }
  234. }
  235. return $temp;
  236. }
  237. /**
  238. * Converts a key value pair that can contain strings, nulls, false, or arrays
  239. * into a single string.
  240. *
  241. * @param string $name Name of the field
  242. * @param mixed $value Value of the field
  243. * @return string
  244. */
  245. private function convertKvp($name, $value)
  246. {
  247. if ($value === self::BLANK || $value === null || $value === false) {
  248. return $name;
  249. } elseif (!is_array($value)) {
  250. return $name . $this->valueSeparator . $value;
  251. }
  252. $result = '';
  253. foreach ($value as $v) {
  254. $result .= $this->convertKvp($name, $v) . $this->fieldSeparator;
  255. }
  256. return rtrim($result, $this->fieldSeparator);
  257. }
  258. }