Regex.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * This file is part of composer/pcre.
  4. *
  5. * (c) Composer <https://github.com/composer>
  6. *
  7. * For the full copyright and license information, please view
  8. * the LICENSE file that was distributed with this source code.
  9. */
  10. namespace Composer\Pcre;
  11. class Regex
  12. {
  13. /**
  14. * @param non-empty-string $pattern
  15. */
  16. public static function isMatch(string $pattern, string $subject, int $offset = 0): bool
  17. {
  18. return (bool) Preg::match($pattern, $subject, $matches, 0, $offset);
  19. }
  20. /**
  21. * @param non-empty-string $pattern
  22. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  23. */
  24. public static function match(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchResult
  25. {
  26. self::checkOffsetCapture($flags, 'matchWithOffsets');
  27. $count = Preg::match($pattern, $subject, $matches, $flags, $offset);
  28. return new MatchResult($count, $matches);
  29. }
  30. /**
  31. * Variant of `match()` which returns non-null matches (or throws)
  32. *
  33. * @param non-empty-string $pattern
  34. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  35. * @throws UnexpectedNullMatchException
  36. */
  37. public static function matchStrictGroups(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchStrictGroupsResult
  38. {
  39. $count = Preg::matchStrictGroups($pattern, $subject, $matches, $flags, $offset);
  40. return new MatchStrictGroupsResult($count, $matches);
  41. }
  42. /**
  43. * Runs preg_match with PREG_OFFSET_CAPTURE
  44. *
  45. * @param non-empty-string $pattern
  46. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
  47. */
  48. public static function matchWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchWithOffsetsResult
  49. {
  50. $count = Preg::matchWithOffsets($pattern, $subject, $matches, $flags, $offset);
  51. return new MatchWithOffsetsResult($count, $matches);
  52. }
  53. /**
  54. * @param non-empty-string $pattern
  55. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  56. */
  57. public static function matchAll(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllResult
  58. {
  59. self::checkOffsetCapture($flags, 'matchAllWithOffsets');
  60. self::checkSetOrder($flags);
  61. $count = Preg::matchAll($pattern, $subject, $matches, $flags, $offset);
  62. return new MatchAllResult($count, $matches);
  63. }
  64. /**
  65. * Variant of `matchAll()` which returns non-null matches (or throws)
  66. *
  67. * @param non-empty-string $pattern
  68. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  69. * @throws UnexpectedNullMatchException
  70. */
  71. public static function matchAllStrictGroups(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllStrictGroupsResult
  72. {
  73. self::checkOffsetCapture($flags, 'matchAllWithOffsets');
  74. self::checkSetOrder($flags);
  75. $count = Preg::matchAllStrictGroups($pattern, $subject, $matches, $flags, $offset);
  76. return new MatchAllStrictGroupsResult($count, $matches);
  77. }
  78. /**
  79. * Runs preg_match_all with PREG_OFFSET_CAPTURE
  80. *
  81. * @param non-empty-string $pattern
  82. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
  83. */
  84. public static function matchAllWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllWithOffsetsResult
  85. {
  86. self::checkSetOrder($flags);
  87. $count = Preg::matchAllWithOffsets($pattern, $subject, $matches, $flags, $offset);
  88. return new MatchAllWithOffsetsResult($count, $matches);
  89. }
  90. /**
  91. * @param string|string[] $pattern
  92. * @param string|string[] $replacement
  93. * @param string $subject
  94. */
  95. public static function replace($pattern, $replacement, $subject, int $limit = -1): ReplaceResult
  96. {
  97. $result = Preg::replace($pattern, $replacement, $subject, $limit, $count);
  98. return new ReplaceResult($count, $result);
  99. }
  100. /**
  101. * @param string|string[] $pattern
  102. * @param ($flags is PREG_OFFSET_CAPTURE ? (callable(array<int|string, array{string|null, int<-1, max>}>): string) : callable(array<int|string, string|null>): string) $replacement
  103. * @param string $subject
  104. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  105. */
  106. public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
  107. {
  108. $result = Preg::replaceCallback($pattern, $replacement, $subject, $limit, $count, $flags);
  109. return new ReplaceResult($count, $result);
  110. }
  111. /**
  112. * Variant of `replaceCallback()` which outputs non-null matches (or throws)
  113. *
  114. * @param string $pattern
  115. * @param ($flags is PREG_OFFSET_CAPTURE ? (callable(array<int|string, array{string, int<0, max>}>): string) : callable(array<int|string, string>): string) $replacement
  116. * @param string $subject
  117. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  118. */
  119. public static function replaceCallbackStrictGroups($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
  120. {
  121. $result = Preg::replaceCallbackStrictGroups($pattern, $replacement, $subject, $limit, $count, $flags);
  122. return new ReplaceResult($count, $result);
  123. }
  124. /**
  125. * @param ($flags is PREG_OFFSET_CAPTURE ? (array<string, callable(array<int|string, array{string|null, int<-1, max>}>): string>) : array<string, callable(array<int|string, string|null>): string>) $pattern
  126. * @param string $subject
  127. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  128. */
  129. public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int $flags = 0): ReplaceResult
  130. {
  131. $result = Preg::replaceCallbackArray($pattern, $subject, $limit, $count, $flags);
  132. return new ReplaceResult($count, $result);
  133. }
  134. private static function checkOffsetCapture(int $flags, string $useFunctionName): void
  135. {
  136. if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
  137. throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the return type, use '.$useFunctionName.'() instead');
  138. }
  139. }
  140. private static function checkSetOrder(int $flags): void
  141. {
  142. if (($flags & PREG_SET_ORDER) !== 0) {
  143. throw new \InvalidArgumentException('PREG_SET_ORDER is not supported as it changes the return type');
  144. }
  145. }
  146. }