QueryInterface.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * League.Uri (https://uri.thephpleague.com)
  4. *
  5. * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\Uri\Contracts;
  12. use BackedEnum;
  13. use Countable;
  14. use Deprecated;
  15. use Iterator;
  16. use IteratorAggregate;
  17. use League\Uri\QueryComposeMode;
  18. use League\Uri\StringCoercionMode;
  19. use Stringable;
  20. /**
  21. * @extends IteratorAggregate<array{0:string, 1:string|null}>
  22. *
  23. * @method string|null toFormData() Returns the string representation using the application/www-form-urlencoded rules
  24. * @method string|null toRFC3986() Returns the string representation using RFC3986 rules
  25. * @method string|null first(string $key) Returns the first value associated with the given name
  26. * @method string|null last(string $key) Returns the first value associated with the given name
  27. * @method int|null indexOf(string $key, int $nth = 0) Returns the offset of the pair based on its key and its nth occurrence; negative occurrences are supported
  28. * @method int|null indexOfValue(?string $value, int $nth = 0) Returns the offset of the pair based on its value and its nth occurrence; negative occurrences are supported
  29. * @method array pair(int $offset) Returns the key/value pair at the given numeric offset; negative occurrences are supported
  30. * @method int countDistinctKeys() Returns the total number of distinct keys
  31. * @method string|null valueAt(int $offset): Returns the value at the given numeric offset; negative occurrences are supported
  32. * @method string keyAt(int $offset): Returns the key at the given numeric offset; negative occurrences are supported
  33. * @method self normalize() returns the normalized string representation of the component
  34. * @method self withoutPairByKey(string ...$keys) Returns an instance without pairs with the specified keys.
  35. * @method self withoutPairByValue(array|BackedEnum|Stringable|string|int|bool|null $values, StringCoercionMode $coercionMode = StringCoercionMode::Native) Returns an instance without pairs with the specified values.
  36. * @method self withoutPairByKeyValue(string $key, BackedEnum|Stringable|string|int|bool|null $value, StringCoercionMode $coercionMode = StringCoercionMode::Native) Returns an instance without pairs with the specified key/value pair
  37. * @method bool hasPair(string $key, ?string $value) Tells whether the pair exists in the query.
  38. * @method array getList(string $name) Returns the list associated with the given name or an empty array if it does not exist.
  39. * @method bool hasList(string ...$names) Tells whether the parameter list exists in the query.
  40. * @method self appendList(string $name, array $values, QueryComposeMode $composeMode = QueryComposeMode::Native) Appends a parameter to the query string
  41. * @method self withList(string $name, array $values, QueryComposeMode $composeMode = QueryComposeMode::Native) Adds a new parameter to the query string and remove any previously set values
  42. * @method self withoutList(string ...$names) Removes any given list associated with the given names
  43. * @method self withoutLists() Removes all lists from the query string
  44. * @method self onlyLists() Removes all pairs without a valid PHP's bracket notation
  45. */
  46. interface QueryInterface extends Countable, IteratorAggregate, UriComponentInterface
  47. {
  48. /**
  49. * Returns the query separator.
  50. *
  51. * @return non-empty-string
  52. */
  53. public function getSeparator(): string;
  54. /**
  55. * Returns the number of key/value pairs present in the object.
  56. */
  57. public function count(): int;
  58. /**
  59. * Returns an iterator allowing to go through all key/value pairs contained in this object.
  60. *
  61. * The pair is represented as an array where the first value is the pair key
  62. * and the second value the pair value.
  63. *
  64. * The key of each pair is a string
  65. * The value of each pair is a scalar or the null value
  66. *
  67. * @return Iterator<int, array{0:string, 1:string|null}>
  68. */
  69. public function getIterator(): Iterator;
  70. /**
  71. * Returns an iterator allowing to go through all key/value pairs contained in this object.
  72. *
  73. * The return type is as an Iterator where its offset is the pair key and its value the pair value.
  74. *
  75. * The key of each pair is a string
  76. * The value of each pair is a scalar or the null value
  77. *
  78. * @return iterable<string, string|null>
  79. */
  80. public function pairs(): iterable;
  81. /**
  82. * Tells whether a list of pair with a specific key exists.
  83. *
  84. * @see https://url.spec.whatwg.org/#dom-urlsearchparams-has
  85. */
  86. public function has(string ...$keys): bool;
  87. /**
  88. * Returns the first value associated to the given pair name.
  89. *
  90. * If no value is found null is returned
  91. *
  92. * @see https://url.spec.whatwg.org/#dom-urlsearchparams-get
  93. */
  94. public function get(string $key): ?string;
  95. /**
  96. * Returns all the values associated to the given pair name as an array or all
  97. * the instance pairs.
  98. *
  99. * If no value is found an empty array is returned
  100. *
  101. * @see https://url.spec.whatwg.org/#dom-urlsearchparams-getall
  102. *
  103. * @return array<int, string|null>
  104. */
  105. public function getAll(string $key): array;
  106. /**
  107. * Returns the store PHP variables as elements of an array.
  108. *
  109. * The result is similar as PHP parse_str when used with its
  110. * second argument with the difference that variable names are
  111. * not mangled.
  112. *
  113. * @see http://php.net/parse_str
  114. * @see https://wiki.php.net/rfc/on_demand_name_mangling
  115. *
  116. * @return array the collection of stored PHP variables or the empty array if no input is given,
  117. */
  118. public function parameters(): array;
  119. /**
  120. * Returns the value attached to the specific key.
  121. *
  122. * The result is similar to PHP parse_str with the difference that variable
  123. * names are not mangled.
  124. *
  125. * If a key is submitted it will return the value attached to it or null
  126. *
  127. * @see http://php.net/parse_str
  128. * @see https://wiki.php.net/rfc/on_demand_name_mangling
  129. *
  130. * @return mixed the collection of stored PHP variables or the empty array if no input is given,
  131. * the single value of a stored PHP variable or null if the variable is not present in the collection
  132. */
  133. public function parameter(string $name): mixed;
  134. /**
  135. * Tells whether a list of variable with specific names exists.
  136. *
  137. * @see https://url.spec.whatwg.org/#dom-urlsearchparams-has
  138. */
  139. public function hasParameter(string ...$names): bool;
  140. /**
  141. * Returns the RFC1738 encoded query.
  142. */
  143. public function toRFC1738(): ?string;
  144. /**
  145. * Returns an instance with a different separator.
  146. *
  147. * This method MUST retain the state of the current instance, and return
  148. * an instance that contains the query component with a different separator
  149. */
  150. public function withSeparator(string $separator): self;
  151. /**
  152. * Returns an instance with the new pairs set to it.
  153. *
  154. * This method MUST retain the state of the current instance, and return
  155. * an instance that contains the modified query
  156. *
  157. * @see ::withPair
  158. */
  159. public function merge(Stringable|string $query): self;
  160. /**
  161. * Returns an instance with the new pairs appended to it.
  162. *
  163. * This method MUST retain the state of the current instance, and return
  164. * an instance that contains the modified query
  165. *
  166. * If the pair already exists the value will be added to it.
  167. */
  168. public function append(Stringable|string $query): self;
  169. /**
  170. * Returns a new instance with a specified key/value pair appended as a new pair.
  171. *
  172. * This method MUST retain the state of the current instance, and return
  173. * an instance that contains the modified query
  174. */
  175. public function appendTo(string $key, Stringable|string|int|bool|null $value): self;
  176. /**
  177. * Sorts the query string by offset, maintaining offset to data correlations.
  178. *
  179. * This method MUST retain the state of the current instance, and return
  180. * an instance that contains the modified query
  181. *
  182. * @see https://url.spec.whatwg.org/#dom-urlsearchparams-sort
  183. */
  184. public function sort(): self;
  185. /**
  186. * Returns an instance without duplicate key/value pair.
  187. *
  188. * This method MUST retain the state of the current instance, and return
  189. * an instance that contains the query component normalized by removing
  190. * duplicate pairs whose key/value are the same.
  191. */
  192. public function withoutDuplicates(): self;
  193. /**
  194. * Returns an instance without empty key/value where the value is the null value.
  195. *
  196. * This method MUST retain the state of the current instance, and return
  197. * an instance that contains the query component normalized by removing
  198. * empty pairs.
  199. *
  200. * A pair is considered empty if its value is equal to the null value
  201. */
  202. public function withoutEmptyPairs(): self;
  203. /**
  204. * Returns an instance where numeric indices associated to PHP's array like key are removed.
  205. *
  206. * This method MUST retain the state of the current instance, and return
  207. * an instance that contains the query component normalized so that numeric indexes
  208. * are removed from the pair key value.
  209. *
  210. * i.e.: toto[3]=bar[3]&foo=bar becomes toto[]=bar[3]&foo=bar
  211. */
  212. public function withoutNumericIndices(): self;
  213. /**
  214. * Returns an instance with a new key/value pair added to it.
  215. *
  216. * This method MUST retain the state of the current instance, and return
  217. * an instance that contains the modified query
  218. *
  219. * If the pair already exists the value will replace the existing value.
  220. *
  221. * @see https://url.spec.whatwg.org/#dom-urlsearchparams-set
  222. */
  223. public function withPair(string $key, Stringable|string|int|float|bool|null $value): self;
  224. /**
  225. * DEPRECATION WARNING! This method will be removed in the next major point release.
  226. *
  227. * @deprecated Since version 7.3.0
  228. * @codeCoverageIgnore
  229. * @see QueryInterface::withoutPairByKey()
  230. *
  231. * Returns an instance without the specified keys.
  232. *
  233. * This method MUST retain the state of the current instance, and return
  234. * an instance that contains the modified component
  235. */
  236. #[Deprecated(message:'use League\Uri\Contracts\QueryInterface::withoutPairByKey() instead', since:'league/uri-interfaces:7.3.0')]
  237. public function withoutPair(string ...$keys): self;
  238. /**
  239. * Returns an instance without the specified params.
  240. *
  241. * This method MUST retain the state of the current instance, and return
  242. * an instance that contains the modified component without PHP's value.
  243. * PHP's mangled is not taken into account.
  244. */
  245. public function withoutParameters(string ...$names): self;
  246. }