1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Apps\Model\Front\Search; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Apps\Model\Front\Search\Interfaces\SearchContainer; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Arch\Model; |
10
|
|
|
use Apps\ActiveRecord\Content; |
11
|
|
|
use Ffcms\Core\Helper\Text; |
12
|
|
|
use Ffcms\Core\Helper\Type\Str; |
13
|
|
|
|
14
|
|
|
class SearchContent extends Model implements SearchContainer |
15
|
|
|
{ |
16
|
|
|
private $query; |
17
|
|
|
private $limit; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* SearchContainer constructor. Pass string query inside |
21
|
|
|
* @param string $query |
22
|
|
|
* @param int $limit |
23
|
|
|
*/ |
24
|
|
|
public function __construct($query, $limit = 10) |
25
|
|
|
{ |
26
|
|
|
$this->query = $query; |
27
|
|
|
$this->limit = (int)$limit; |
28
|
|
|
if ($this->limit < 1) { |
29
|
|
|
$this->limit = 1; |
30
|
|
|
} |
31
|
|
|
parent::__construct(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Build search results |
36
|
|
|
* @return array[AbstractSearchResult] |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function getResult() |
39
|
|
|
{ |
40
|
|
|
// relevant search by string query |
41
|
|
|
$records = Content::search($this->query) |
|
|
|
|
42
|
|
|
->take($this->limit) |
43
|
|
|
->get(); |
44
|
|
|
|
45
|
|
|
// check if result is not empty |
46
|
|
|
if ($records->count() < 1) { |
47
|
|
|
return []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// build result items |
51
|
|
|
$result = []; |
52
|
|
|
foreach ($records as $item) { |
53
|
|
|
/** @var \Apps\ActiveRecord\Content $item */ |
54
|
|
|
$title = $item->getLocaled('title'); |
55
|
|
|
$text = App::$Security->strip_tags($item->getLocaled('text')); |
|
|
|
|
56
|
|
|
$snippet = Text::snippet($text); |
|
|
|
|
57
|
|
|
// prevent empty items |
58
|
|
|
if (Str::likeEmpty($title)) { |
|
|
|
|
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// initialize abstract response pattern |
63
|
|
|
$res = new AbstractSearchResult(); |
64
|
|
|
$res->setTitle($title); |
|
|
|
|
65
|
|
|
$res->setSnippet($snippet); |
66
|
|
|
$res->setDate($item->created_at); |
|
|
|
|
67
|
|
|
$res->setRelevance((int)$item->relevance); |
|
|
|
|
68
|
|
|
$res->setUri('/content/read/' . $item->getPath()); |
69
|
|
|
|
70
|
|
|
// accumulate response var |
71
|
|
|
$result[] = $res; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.