Regex.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // @phpstan-ignore composerPcre.maybeUnsafeStrictGroups
  40. $count = Preg::matchStrictGroups($pattern, $subject, $matches, $flags, $offset);
  41. return new MatchStrictGroupsResult($count, $matches);
  42. }
  43. /**
  44. * Runs preg_match with PREG_OFFSET_CAPTURE
  45. *
  46. * @param non-empty-string $pattern
  47. * @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
  48. */
  49. public static function matchWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchWithOffsetsResult
  50. {
  51. $count = Preg::matchWithOffsets($pattern, $subject, $matches, $flags, $offset);
  52. return new MatchWithOffsetsResult($count, $matches);
  53. }
  54. /**
  55. * @param non-empty-string $pattern
  56. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  57. */
  58. public static function matchAll(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllResult
  59. {
  60. self::checkOffsetCapture($flags, 'matchAllWithOffsets');
  61. self::checkSetOrder($flags);
  62. $count = Preg::matchAll($pattern, $subject, $matches, $flags, $offset);
  63. return new MatchAllResult($count, $matches);
  64. }
  65. /**
  66. * Variant of `matchAll()` which returns non-null matches (or throws)
  67. *
  68. * @param non-empty-string $pattern
  69. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  70. * @throws UnexpectedNullMatchException
  71. */
  72. public static function matchAllStrictGroups(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllStrictGroupsResult
  73. {
  74. self::checkOffsetCapture($flags, 'matchAllWithOffsets');
  75. self::checkSetOrder($flags);
  76. // @phpstan-ignore composerPcre.maybeUnsafeStrictGroups
  77. $count = Preg::matchAllStrictGroups($pattern, $subject, $matches, $flags, $offset);
  78. return new MatchAllStrictGroupsResult($count, $matches);
  79. }
  80. /**
  81. * Runs preg_match_all with PREG_OFFSET_CAPTURE
  82. *
  83. * @param non-empty-string $pattern
  84. * @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
  85. */
  86. public static function matchAllWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllWithOffsetsResult
  87. {
  88. self::checkSetOrder($flags);
  89. $count = Preg::matchAllWithOffsets($pattern, $subject, $matches, $flags, $offset);
  90. return new MatchAllWithOffsetsResult($count, $matches);
  91. }
  92. /**
  93. * @param string|string[] $pattern
  94. * @param string|string[] $replacement
  95. * @param string $subject
  96. */
  97. public static function replace($pattern, $replacement, $subject, int $limit = -1): ReplaceResult
  98. {
  99. $result = Preg::replace($pattern, $replacement, $subject, $limit, $count);
  100. return new ReplaceResult($count, $result);
  101. }
  102. /**
  103. * @param string|string[] $pattern
  104. * @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
  105. * @param string $subject
  106. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  107. */
  108. public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
  109. {
  110. $result = Preg::replaceCallback($pattern, $replacement, $subject, $limit, $count, $flags);
  111. return new ReplaceResult($count, $result);
  112. }
  113. /**
  114. * Variant of `replaceCallback()` which outputs non-null matches (or throws)
  115. *
  116. * @param string $pattern
  117. * @param ($flags is PREG_OFFSET_CAPTURE ? (callable(array<int|string, array{string, int<0, max>}>): string) : callable(array<int|string, string>): string) $replacement
  118. * @param string $subject
  119. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  120. */
  121. public static function replaceCallbackStrictGroups($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
  122. {
  123. $result = Preg::replaceCallbackStrictGroups($pattern, $replacement, $subject, $limit, $count, $flags);
  124. return new ReplaceResult($count, $result);
  125. }
  126. /**
  127. * @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
  128. * @param string $subject
  129. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  130. */
  131. public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int $flags = 0): ReplaceResult
  132. {
  133. $result = Preg::replaceCallbackArray($pattern, $subject, $limit, $count, $flags);
  134. return new ReplaceResult($count, $result);
  135. }
  136. private static function checkOffsetCapture(int $flags, string $useFunctionName): void
  137. {
  138. if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
  139. throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the return type, use '.$useFunctionName.'() instead');
  140. }
  141. }
  142. private static function checkSetOrder(int $flags): void
  143. {
  144. if (($flags & PREG_SET_ORDER) !== 0) {
  145. throw new \InvalidArgumentException('PREG_SET_ORDER is not supported as it changes the return type');
  146. }
  147. }
  148. }