You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

  1. <?php
  2. namespace TomLingham\Searchy\Matchers;
  3. /**
  4. * Matches strings that include all the characters in the search relatively position within the string.
  5. * It also calculates the percentage of characters in the string that are matched and applies the multiplier accordingly.
  6. *
  7. * For Example, a search for 'fba' would match; 'Foo Bar' or 'Afraid of bats'
  8. *
  9. * Class ConsecutiveCharactersMatcher
  10. */
  11. class ConsecutiveCharactersUnicodeMatcher extends BaseMatcher
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $operator = 'LIKE';
  17. /**
  18. * @param $searchString
  19. *
  20. * @return string
  21. */
  22. public function formatSearchString($searchString)
  23. {
  24. $results = [];
  25. preg_match_all('/./u', $searchString, $results);
  26. return '%'.implode('%', $results[0]).'%';
  27. }
  28. /**
  29. * @param $column
  30. * @param $rawString
  31. *
  32. * @return mixed|string
  33. */
  34. public function buildQueryString($column, $rawString)
  35. {
  36. $searchString = $this->formatSearchString($rawString);
  37. return "IF( REPLACE($column, '\.', '') {$this->operator} '$searchString', ROUND({$this->multiplier} * ( CHAR_LENGTH( '$rawString' ) / CHAR_LENGTH( REPLACE($column, ' ', '') ))), 0)";
  38. }
  39. }