1 | <?php |
||
2 | |||
3 | namespace Apps\Model\Front\Search; |
||
4 | |||
5 | use Ffcms\Core\Arch\Model; |
||
6 | use Ffcms\Core\Helper\Type\Any; |
||
7 | use Ffcms\Core\Helper\Type\Str; |
||
8 | use Ffcms\Templex\Helper\Html\Dom; |
||
9 | |||
10 | /** |
||
11 | * Class EntitySearchMain. Search everything main business logic model |
||
12 | * @package Apps\Model\Front\Search |
||
13 | */ |
||
14 | class EntitySearchMain extends Model |
||
15 | { |
||
16 | public $results = []; |
||
17 | public $query; |
||
18 | |||
19 | private $_configs; |
||
20 | |||
21 | /** |
||
22 | * EntitySearchMain constructor. Pass query inside |
||
23 | * @param string $query |
||
24 | * @param array|null $configs |
||
25 | */ |
||
26 | public function __construct($query, array $configs = null) |
||
27 | { |
||
28 | $this->query = $query; |
||
29 | $this->_configs = $configs; |
||
30 | parent::__construct(); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Get initialize configs |
||
35 | * @return array|null |
||
36 | */ |
||
37 | public function getConfigs(): ?array |
||
38 | { |
||
39 | return $this->_configs; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Add result item to main collection |
||
44 | * @param AbstractSearchResult $result |
||
45 | */ |
||
46 | public function add(AbstractSearchResult $result) |
||
47 | { |
||
48 | $this->results[] = $result; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Get sorted by relevance search response. Method return result as array: [relevance => [title, snippet, uri, date], ...] |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getRelevanceSortedResult() |
||
56 | { |
||
57 | $result = []; |
||
58 | // each every content type |
||
59 | foreach ($this->results as $item) { |
||
60 | /** @var AbstractSearchResult $item */ |
||
61 | // build unique relevance. Problem: returned relevance from query is integer |
||
62 | // and can be duplicated. So, we add random complex float value and make it string to sort in feature |
||
63 | $uniqueRelevance = (string)($item->getRelevance() + (mt_rand(0, 999) / 10000)); |
||
64 | // build response |
||
65 | $result[$uniqueRelevance] = $item; |
||
66 | } |
||
67 | |||
68 | // sort output by relevance |
||
69 | krsort($result); |
||
70 | |||
71 | // return result as array |
||
0 ignored issues
–
show
|
|||
72 | return $result; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Highlight words in text by current query request. |
||
77 | * @param string $text |
||
78 | * @param string $tag |
||
79 | * @param array $properties |
||
80 | * @return string |
||
81 | */ |
||
82 | public function highlightText($text, $tag, array $properties = []) |
||
83 | { |
||
84 | $queries = explode(' ', $this->query); |
||
85 | $dom = new Dom(); |
||
86 | foreach ($queries as $query) { |
||
87 | $highlight = $dom->{$tag}(function () use ($query) { |
||
88 | return $query; |
||
89 | }, $properties); |
||
90 | $text = Str::ireplace($query, $highlight, $text); |
||
91 | } |
||
92 | return $text; |
||
93 | } |
||
94 | } |
||
95 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.