1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Front; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Apps\Model\Front\Search\EntitySearchMain; |
7
|
|
|
use Extend\Core\Arch\FrontAppController as Controller; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
10
|
|
|
use Ffcms\Core\Helper\Type\Str; |
11
|
|
|
|
12
|
|
|
class Search extends Controller |
13
|
|
|
{ |
14
|
|
|
const EVENT_SEARCH_RUNNED = 'search.run'; |
15
|
|
|
|
16
|
|
|
const QUERY_MAX_LENGTH = 100; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Main search method |
20
|
|
|
* @return string |
21
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
22
|
|
|
* @throws NotFoundException |
23
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
24
|
|
|
*/ |
25
|
|
|
public function actionIndex() |
26
|
|
|
{ |
27
|
|
|
// get search query value from GET headers |
28
|
|
|
$query = (string)App::$Request->query->get('query', null); |
29
|
|
|
// strip html tags |
30
|
|
|
$query = App::$Security->strip_tags($query); |
31
|
|
|
// get configs |
32
|
|
|
$configs = $this->getConfigs(); |
33
|
|
|
|
34
|
|
|
// check search query length |
35
|
|
View Code Duplication |
if (Str::likeEmpty($query) || Str::length($query) < (int)$configs['minLength']) { |
|
|
|
|
36
|
|
|
throw new NotFoundException(__('Search query is too short!')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// prevent sh@t query's with big length |
40
|
|
|
if (Str::length($query) > static::QUERY_MAX_LENGTH) { |
|
|
|
|
41
|
|
|
throw new NotFoundException(__('Search query is too long!')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// initialize search controller model |
45
|
|
|
$model = new EntitySearchMain($query, $configs); |
|
|
|
|
46
|
|
|
// try to search by default apps |
47
|
|
|
$model->make(); |
48
|
|
|
|
49
|
|
|
// register search event to allow extend it model results |
50
|
|
|
App::$Event->run(static::EVENT_SEARCH_RUNNED, [ |
51
|
|
|
'model' => $model |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
// render output view with search result |
55
|
|
|
return App::$View->render('index', [ |
56
|
|
|
'model' => $model, |
57
|
|
|
'query' => $query |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.