QueryInterface.php 8.7 KB

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