Option.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Idna;
  12. use ReflectionClass;
  13. use ReflectionClassConstant;
  14. /**
  15. * @see https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/uidna_8h.html
  16. */
  17. final class Option
  18. {
  19. private const DEFAULT = 0;
  20. private const ALLOW_UNASSIGNED = 1;
  21. private const USE_STD3_RULES = 2;
  22. private const CHECK_BIDI = 4;
  23. private const CHECK_CONTEXTJ = 8;
  24. private const NONTRANSITIONAL_TO_ASCII = 0x10;
  25. private const NONTRANSITIONAL_TO_UNICODE = 0x20;
  26. private const CHECK_CONTEXTO = 0x40;
  27. private function __construct(private readonly int $value)
  28. {
  29. }
  30. private static function cases(): array
  31. {
  32. static $assoc;
  33. if (null === $assoc) {
  34. $assoc = [];
  35. $fooClass = new ReflectionClass(self::class);
  36. foreach ($fooClass->getConstants(ReflectionClassConstant::IS_PRIVATE) as $name => $value) {
  37. $assoc[$name] = $value;
  38. }
  39. }
  40. return $assoc;
  41. }
  42. public static function new(int $bytes = self::DEFAULT): self
  43. {
  44. return new self(array_reduce(
  45. self::cases(),
  46. fn (int $value, int $option) => 0 !== ($option & $bytes) ? ($value | $option) : $value,
  47. self::DEFAULT
  48. ));
  49. }
  50. public static function forIDNA2008Ascii(): self
  51. {
  52. return self::new()
  53. ->nonTransitionalToAscii()
  54. ->checkBidi()
  55. ->useSTD3Rules()
  56. ->checkContextJ();
  57. }
  58. public static function forIDNA2008Unicode(): self
  59. {
  60. return self::new()
  61. ->nonTransitionalToUnicode()
  62. ->checkBidi()
  63. ->useSTD3Rules()
  64. ->checkContextJ();
  65. }
  66. public function toBytes(): int
  67. {
  68. return $this->value;
  69. }
  70. /** array<string, int> */
  71. public function list(): array
  72. {
  73. return array_keys(array_filter(
  74. self::cases(),
  75. fn (int $value) => 0 !== ($value & $this->value)
  76. ));
  77. }
  78. public function allowUnassigned(): self
  79. {
  80. return $this->add(self::ALLOW_UNASSIGNED);
  81. }
  82. public function disallowUnassigned(): self
  83. {
  84. return $this->remove(self::ALLOW_UNASSIGNED);
  85. }
  86. public function useSTD3Rules(): self
  87. {
  88. return $this->add(self::USE_STD3_RULES);
  89. }
  90. public function prohibitSTD3Rules(): self
  91. {
  92. return $this->remove(self::USE_STD3_RULES);
  93. }
  94. public function checkBidi(): self
  95. {
  96. return $this->add(self::CHECK_BIDI);
  97. }
  98. public function ignoreBidi(): self
  99. {
  100. return $this->remove(self::CHECK_BIDI);
  101. }
  102. public function checkContextJ(): self
  103. {
  104. return $this->add(self::CHECK_CONTEXTJ);
  105. }
  106. public function ignoreContextJ(): self
  107. {
  108. return $this->remove(self::CHECK_CONTEXTJ);
  109. }
  110. public function checkContextO(): self
  111. {
  112. return $this->add(self::CHECK_CONTEXTO);
  113. }
  114. public function ignoreContextO(): self
  115. {
  116. return $this->remove(self::CHECK_CONTEXTO);
  117. }
  118. public function nonTransitionalToAscii(): self
  119. {
  120. return $this->add(self::NONTRANSITIONAL_TO_ASCII);
  121. }
  122. public function transitionalToAscii(): self
  123. {
  124. return $this->remove(self::NONTRANSITIONAL_TO_ASCII);
  125. }
  126. public function nonTransitionalToUnicode(): self
  127. {
  128. return $this->add(self::NONTRANSITIONAL_TO_UNICODE);
  129. }
  130. public function transitionalToUnicode(): self
  131. {
  132. return $this->remove(self::NONTRANSITIONAL_TO_UNICODE);
  133. }
  134. public function add(Option|int|null $option = null): self
  135. {
  136. return match (true) {
  137. null === $option => $this,
  138. $option instanceof self => self::new($this->value | $option->value),
  139. default => self::new($this->value | $option),
  140. };
  141. }
  142. public function remove(Option|int|null $option = null): self
  143. {
  144. return match (true) {
  145. null === $option => $this,
  146. $option instanceof self => self::new($this->value & ~$option->value),
  147. default => self::new($this->value & ~$option),
  148. };
  149. }
  150. }