1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony) |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace App\Services; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
use App\Entity\Parts\Part; |
27
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
28
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A service related for searching for tags. Mostly useful for autocomplete reasons. |
32
|
|
|
* @package App\Services |
33
|
|
|
*/ |
34
|
|
|
class TagFinder |
35
|
|
|
{ |
36
|
|
|
protected $em; |
37
|
|
|
|
38
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
39
|
|
|
{ |
40
|
|
|
$this->em = $entityManager; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function configureOptions(OptionsResolver $resolver) |
44
|
|
|
{ |
45
|
|
|
$resolver->setDefaults([ |
46
|
|
|
'query_limit' => 75, |
47
|
|
|
'return_limit' => 25, |
48
|
|
|
'min_keyword_length' => 3 |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Search tags that begins with the certain keyword. |
54
|
|
|
* @param string $keyword The keyword the tag must begin with |
55
|
|
|
* @param array $options Some options specifying the search behavior. See configureOptions for possible options. |
56
|
|
|
* @return string[] An array containing the tags that match the given keyword. |
57
|
|
|
*/ |
58
|
|
|
public function searchTags(string $keyword, array $options = []) |
59
|
|
|
{ |
60
|
|
|
$results = []; |
61
|
|
|
$keyword_regex = '/^' . preg_quote($keyword, '/') . '/'; |
62
|
|
|
|
63
|
|
|
$resolver = new OptionsResolver(); |
64
|
|
|
$this->configureOptions($resolver); |
65
|
|
|
|
66
|
|
|
$options = $resolver->resolve($options); |
67
|
|
|
|
68
|
|
|
//If the keyword is too short we will get to much results, which takes too much time... |
69
|
|
|
if (mb_strlen($keyword) < $options['min_keyword_length']) { |
70
|
|
|
return []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
//Build a query to get all |
74
|
|
|
$qb = $this->em->createQueryBuilder(); |
75
|
|
|
|
76
|
|
|
$qb->select('p.tags') |
77
|
|
|
->from(Part::class, 'p') |
78
|
|
|
->where("p.tags LIKE ?1") |
79
|
|
|
->setMaxResults($options['query_limit']) |
80
|
|
|
//->orderBy('RAND()') |
81
|
|
|
->setParameter(1, '%' . $keyword . '%'); |
82
|
|
|
|
83
|
|
|
$possible_tags = $qb->getQuery()->getArrayResult(); |
84
|
|
|
|
85
|
|
|
//Iterate over each possible tags (which are comma separated) and extract tags which match our keyword |
86
|
|
|
foreach ($possible_tags as $tags) { |
87
|
|
|
$tags = explode(',', $tags['tags']); |
88
|
|
|
$results = array_merge($results, preg_grep($keyword_regex, $tags)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$results = array_unique($results); |
92
|
|
|
//Limit the returned tag count to specified value. |
93
|
|
|
return array_slice($results, 0, $options['return_limit']); |
94
|
|
|
} |
95
|
|
|
} |