ThreadMatcher.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Gt\CssXPath;
  3. class ThreadMatcher {
  4. private CssSelectorLexer $cssSelectorLexer;
  5. public function __construct(
  6. ?CssSelectorLexer $cssSelectorLexer = null
  7. ) {
  8. $this->cssSelectorLexer = $cssSelectorLexer
  9. ?? new CssSelectorLexer();
  10. }
  11. /** @return array<int, array<string, mixed>> */
  12. public function collate(
  13. string $regex,
  14. string $string,
  15. ?callable $transform = null
  16. ):array {
  17. if($regex === Translator::CSS_REGEX) {
  18. return $this->collateCssSelector($string, $transform);
  19. }
  20. preg_match_all(
  21. $regex,
  22. $string,
  23. $matches,
  24. PREG_PATTERN_ORDER
  25. );
  26. $set = $this->initialiseSet($matches[0]);
  27. foreach($matches as $key => $matchedGroup) {
  28. if(is_numeric($key)) {
  29. continue;
  30. }
  31. $this->collateGroup($set, $key, $matchedGroup, $transform);
  32. }
  33. return $set;
  34. }
  35. /** @return array<int, array<string, mixed>> */
  36. private function collateCssSelector(
  37. string $selector,
  38. ?callable $transform
  39. ):array {
  40. return $this->cssSelectorLexer->lex($selector, $transform);
  41. }
  42. /**
  43. * @param array<int, string> $matches
  44. * @return array<int, array<string, mixed>|null>
  45. */
  46. private function initialiseSet(array $matches):array {
  47. $set = [];
  48. foreach($matches as $index => $value) {
  49. if($value !== "") {
  50. $set[$index] = null;
  51. }
  52. }
  53. return $set;
  54. }
  55. /**
  56. * @param array<int, array<string, mixed>|null> $set
  57. * @param array<int, string> $matchedGroup
  58. */
  59. private function collateGroup(
  60. array &$set,
  61. string $groupKey,
  62. array $matchedGroup,
  63. ?callable $transform
  64. ):void {
  65. foreach($matchedGroup as $index => $match) {
  66. if($match === "") {
  67. continue;
  68. }
  69. $toSet = $this->buildMatchPayload($groupKey, $match, $transform);
  70. $this->appendMatch($set, $index, $toSet);
  71. }
  72. }
  73. /** @return array<string, string> */
  74. private function buildMatchPayload(
  75. string $groupKey,
  76. string $match,
  77. ?callable $transform
  78. ):array {
  79. if($transform) {
  80. return $transform($groupKey, $match);
  81. }
  82. return ["type" => $groupKey, "content" => $match];
  83. }
  84. /**
  85. * @param array<int, array<string, mixed>|null> $set
  86. * @param array<string, string> $toSet
  87. */
  88. private function appendMatch(array &$set, int $index, array $toSet):void {
  89. if(!isset($set[$index])) {
  90. $set[$index] = $toSet;
  91. return;
  92. }
  93. if(!isset($set[$index]["detail"])) {
  94. $set[$index]["detail"] = [];
  95. }
  96. $set[$index]["detail"][] = $toSet;
  97. }
  98. }