38 lines
1.1 KiB

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