Passed
Push — master ( 3a195a...0456bd )
by Mihail
06:35
created

CollectionSearchResults   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 3 1
A getQuery() 0 3 1
A getLimit() 0 3 1
A getRelevanceBasedResult() 0 22 3
A highlightText() 0 11 2
A __construct() 0 4 1
1
<?php
2
3
namespace Apps\Model\Admin\Main;
4
5
6
use Ffcms\Core\Helper\Type\Str;
7
use Ffcms\Templex\Helper\Html\Dom;
8
9
/**
10
 * Class CollectionSearchResults. Search collection for results with some features
11
 * @package Apps\Model\Admin\Main
12
 */
13
class CollectionSearchResults
14
{
15
    /** @var AbstractSearchItem[]|null */
16
    private $results;
17
18
    private $query;
19
    private $limit;
20
21
    /**
22
     * CollectionSearchResults constructor.
23
     * @param string $query
24
     * @param int $limit
25
     */
26
    public function __construct(string $query, int $limit = 10)
27
    {
28
        $this->query = $query;
29
        $this->limit = $limit;
30
    }
31
32
    /**
33
     * Add search result item
34
     * @param AbstractSearchItem $item
35
     */
36
    public function add(AbstractSearchItem $item): void
37
    {
38
        $this->results[] = $item;
39
    }
40
41
    /**
42
     * Get search query
43
     * @return string|null
44
     */
45
    public function getQuery(): ?string
46
    {
47
        return $this->query;
48
    }
49
50
    /**
51
     * Get result limit
52
     * @return int
53
     */
54
    public function getLimit(): int
55
    {
56
        return $this->limit;
57
    }
58
59
    /**
60
     * Get sorted by relevance search response. Method return result as array: [relevance => [title, snippet, uri, date], ...]
61
     * @return AbstractSearchItem[]
62
     */
63
    public function getRelevanceBasedResult(): ?array
64
    {
65
        if (!$this->results) {
66
            return null;
67
        }
68
69
        $result = [];
70
        // each every content type
71
        foreach ($this->results as $item) {
72
            /** @var AbstractSearchItem $item */
73
            // build unique relevance. Problem: returned relevance from query is integer
74
            // and can be duplicated. So, we add random complex float value and make it string to sort in feature
75
            $uniqueRelevance = (string)($item->getRelevance() + (mt_rand(0, 999) / 10000));
76
            // build response
77
            $result[$uniqueRelevance] = $item;
78
        }
79
80
        // sort output by relevance
81
        krsort($result);
82
83
        // return result as array
0 ignored issues
show
Unused Code Comprehensibility introduced by zenn
38% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
84
        return $result;
85
    }
86
87
    /**
88
     * Highlight words in text by current query request.
89
     * @param string $text
90
     * @param string $tag
91
     * @param array $properties
92
     * @return string
93
     */
94
    public function highlightText($text, $tag, array $properties = []): ?string
95
    {
96
        $queries = explode(' ', $this->query);
97
        $dom = new Dom();
98
        foreach ($queries as $query) {
99
            $highlight = $dom->{$tag}(function () use ($query) {
100
                return $query;
101
            }, $properties);
102
            $text = Str::ireplace($query, $highlight, $text);
103
        }
104
        return $text;
105
    }
106
107
}