1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Search; |
4
|
|
|
|
5
|
|
|
use App\Entity\Category; |
6
|
|
|
use App\Entity\Image; |
7
|
|
|
use App\Entity\Tag; |
8
|
|
|
use App\Entity\Video; |
9
|
|
|
use App\Repository\CategoryRepository; |
10
|
|
|
use App\Repository\ImageRepository; |
11
|
|
|
use App\Repository\TagRepository; |
12
|
|
|
use App\Repository\TrickRepository; |
13
|
|
|
use App\Repository\VideoRepository; |
14
|
|
|
use function count; |
15
|
|
|
|
16
|
|
|
class TrickSearch |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var TrickRepository |
21
|
|
|
*/ |
22
|
|
|
private $trickRepository; |
23
|
|
|
/** |
24
|
|
|
* @var CategoryRepository |
25
|
|
|
*/ |
26
|
|
|
private $categoryRepository; |
27
|
|
|
/** |
28
|
|
|
* @var TagRepository |
29
|
|
|
*/ |
30
|
|
|
private $tagRepository; |
31
|
|
|
/** |
32
|
|
|
* @var ImageRepository |
33
|
|
|
*/ |
34
|
|
|
private $imageRepository; |
35
|
|
|
/** |
36
|
|
|
* @var VideoRepository |
37
|
|
|
*/ |
38
|
|
|
private $videoRepository; |
39
|
|
|
|
40
|
|
|
public function __construct( |
41
|
|
|
TrickRepository $trickRepository, |
42
|
|
|
CategoryRepository $categoryRepository, |
43
|
|
|
TagRepository $tagRepository, |
44
|
|
|
ImageRepository $imageRepository, |
45
|
|
|
VideoRepository $videoRepository |
46
|
|
|
) { |
47
|
|
|
$this->trickRepository = $trickRepository; |
48
|
|
|
$this->categoryRepository = $categoryRepository; |
49
|
|
|
$this->tagRepository = $tagRepository; |
50
|
|
|
$this->imageRepository = $imageRepository; |
51
|
|
|
$this->videoRepository = $videoRepository; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function searchTricks(string $query) |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
$query = $this->sanitizeSearchQuery($query); |
58
|
|
|
$searchTerms = $this->extractSearchTerms($query); |
59
|
|
|
|
60
|
|
|
if (count($searchTerms) === 0) { |
61
|
|
|
return $this->trickRepository->findAll(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$trickList = []; |
65
|
|
|
$trickList = array_merge($trickList, $this->trickRepository->findBySearchQuery($searchTerms)); |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
$categorySearch = $this->categoryRepository->findBySearchQuery($searchTerms); |
69
|
|
|
/** @var Category $category */ |
70
|
|
|
foreach ($categorySearch as $category) { |
71
|
|
|
$trickList = array_merge($trickList, $category->getTricks()->toArray()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$tagSearch = $this->tagRepository->findBySearchQuery($searchTerms); |
75
|
|
|
/** @var Tag $tag */ |
76
|
|
|
foreach ($tagSearch as $tag) { |
77
|
|
|
$trickList = array_merge($trickList, $tag->getTricks()->toArray()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$imageSearch = $this->imageRepository->findBySearchQuery($searchTerms); |
81
|
|
|
/** @var Image $image */ |
82
|
|
|
foreach ($imageSearch as $image) { |
83
|
|
|
if($image->getTrick() !== null){ |
84
|
|
|
$trickList[] = $image->getTrick(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$videoSearch = $this->videoRepository->findBySearchQuery($searchTerms); |
90
|
|
|
/** @var Video $video */ |
91
|
|
|
foreach ($videoSearch as $video) { |
92
|
|
|
if($video->getTrick() !== null){ |
93
|
|
|
$trickList[] = $video->getTrick(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return array_unique($trickList); |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $query |
104
|
|
|
* @return string |
105
|
|
|
* Removes all non alphanum characters except whitespace |
106
|
|
|
*/ |
107
|
|
|
private function sanitizeSearchQuery(string $query): string |
108
|
|
|
{ |
109
|
|
|
return trim(preg_replace('/[[:space:]]+/', ' ', $query)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Splits the search query into terms and removes the ones which are irrelevant. |
114
|
|
|
*/ |
115
|
|
|
private function extractSearchTerms(string $searchQuery): array |
116
|
|
|
{ |
117
|
|
|
$terms = array_unique(explode(' ', $searchQuery)); |
118
|
|
|
return array_filter($terms, function ($term) { |
119
|
|
|
return 2 <= mb_strlen($term); |
120
|
|
|
}); |
121
|
|
|
} |
122
|
|
|
} |