-
- /**
- * Parse a search string into components.
- * @param $searchString
- * @return array
- */
- protected function parseSearchString($searchString)
- {
- $terms = [
- 'search' => [],
- 'exact' => [],
- 'tags' => [],
- 'filters' => []
- ];
-
- $patterns = [
- 'exact' => '/"(.*?)"/',
- 'tags' => '/\[(.*?)\]/',
- 'filters' => '/\{(.*?)\}/'
- ];
-
- // Parse special terms
- foreach ($patterns as $termType => $pattern) {
- $matches = [];
- preg_match_all($pattern, $searchString, $matches);
- if (count($matches) > 0) {
- $terms[$termType] = $matches[1];
- $searchString = preg_replace($pattern, '', $searchString);
- }
- }
-
- // Parse standard terms
- foreach (explode(' ', trim($searchString)) as $searchTerm) {
- if ($searchTerm !== '') {
- $terms['search'][] = $searchTerm;
- }
- }
-
- // Split filter values out
- $splitFilters = [];
- foreach ($terms['filters'] as $filter) {
- $explodedFilter = explode(':', $filter, 2);
- $splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
- }
- $terms['filters'] = $splitFilters;
-
- return $terms;
- }
-