QueryComposeMode.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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;
  12. enum QueryComposeMode
  13. {
  14. /**
  15. * Pre-PHP 8.4 Mode.
  16. *
  17. * Strictly uses get_object_vars on objects (Enum included)
  18. * If the value can not be serialized the entry is skipped.
  19. *
  20. * ie http_build_query behavior before PHP8.4
  21. */
  22. case Compatible;
  23. /**
  24. * PHP 8.4+ enum-compatible lenient mode.
  25. *
  26. * Provides stable support for BackedEnum values.
  27. * UnitEnum values are skipped.
  28. * Uses get_object_vars() for non-enum objects.
  29. * Unserializable values are skipped.
  30. *
  31. * Behaves like {@see QueryComposeMode::EnumCompatible}
  32. * but does not throw for UnitEnum values.
  33. *
  34. * Mirrors http_build_query behavior in PHP 8.4+,
  35. * except that error cases are silently ignored
  36. * instead of throwing.
  37. *
  38. * This mode is tolerant by design and skips entries that would otherwise
  39. * result in an exception in {@see QueryComposeMode::EnumCompatible}.
  40. */
  41. case EnumLenient;
  42. /**
  43. * PHP 8.4+ mode.
  44. *
  45. * Provides stable support for BackedEnum values.
  46. * Throws for UnitEnum.
  47. * Uses get_object_vars() for non-enum objects.
  48. * Unserializable values are skipped.
  49. *
  50. * http_build_query behavior in PHP 8.4+.
  51. */
  52. case EnumCompatible;
  53. /**
  54. * Use PHP version http_build_query algorithm.
  55. *
  56. * In pre-PHP8.4 you get the same results as `Compatible`
  57. * In PHP PHP8.4+ you get the same results as `EnumCompatible`
  58. */
  59. case Native;
  60. /**
  61. * Validation-first mode.
  62. *
  63. * Guarantees that only scalar values, BackedEnum, and null are accepted.
  64. * Any object, UnitEnum, resource, or recursive structure
  65. * results in an exception.
  66. *
  67. * - null: the key name is used but the separator and its content are omitted
  68. * - string: used as-is
  69. * - bool: converted to string “0” (false) or “1” (true)
  70. * - int: converted to numeric string (123 -> “123”)
  71. * - float: converted to decimal string (3.14 -> “3.14”)
  72. * - Backed Enum: converted to their backing value and then stringify see int and string
  73. * - array: empty array: An empty array has zero items, therefore empty arrays are omitted from the query parameter list.
  74. * - lists: Becomes a repeated name suffixed with empty brackets (ie "a" with ["foo", false, 1.23] will result in a[]=foo&a[]=0&a[]=1.23)
  75. * - maps: Becomes a repeated name suffixed with brackets containing the key (ie "a" with ["b" => "foo", "c" => false, "d" => 1.23] will result in a[b]=foo&a[c]=0&a[d]=1.23)
  76. *
  77. * This contract is stable and independent of PHP's http_build_query implementation.
  78. */
  79. case Safe;
  80. }