Completed
Push — master ( 29954a...d9f2b5 )
by Seth
01:54
created

SearchResult::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace smtech\StMarksSearch;
4
5
use JsonSerializable;
6
7
/**
8
 * An object representing a single search result
9
 *
10
 * @author Seth Battis <[email protected]>
11
 *
12
 * @method string getUrl()
13
 * @method string getTitle()
14
 * @method string getDescription()
15
 * @method SearchSource getSource()
16
 * @method Relevance getRelevance()
17
 */
18
class SearchResult extends ParameterArrayConstructor implements JsonSerializable
19
{
20
    /**
21
     * URL of the search result
22
     * @var string
23
     */
24
    protected $url;
25
26
    /**
27
     * Description of result's relevance
28
     * @var Relevance
29
     */
30
    protected $relevance;
31
32
    /**
33
     * Human-readable title
34
     * @var string
35
     */
36
    protected $title;
37
38
    /**
39
     * Human-readable description
40
     *
41
     * Ideally 20-100 words, may be HTML-formatted (although links should be
42
     * stripped out).
43
     * @var string
44
     */
45
    protected $description;
46
47
    /**
48
     * Simplified description of search domain source of the result
49
     * @var SearchSource
50
     */
51
    protected $source;
52
53
    /**
54
     * Construct a SearchResult
55
     *
56
     * Expects an associative parameter array:
57
     *
58
     * ```
59
     * [
60
     *   'url' => URL of the search result as a string,
61
     *   'title' => Title of the search result as a string,
62
     *   'relevance' => instance of `Relevance`,
63
     *   'source' => instance of `SearchSource`,
64
     *   'description' => Optional: search result descriptin as a string
65
     * ]
66
     * ```
67
     *
68
     * @param array $params
69
     */
70
    public function __construct($params)
71
    {
72
        $this->requireParameter($params, 'url');
73
        $this->requireParameter($params, 'title');
74
        $this->requireParameter($params, 'relevance', Relevance::class);
75
        $this->requireParameter($params, 'source', SearchSource::class);
76
77
        $this->defaultParameter($params, 'description', '[no description available]');
78
79
        parent::__construct($params);
80
    }
81
82
    /**
83
     * Sort into order of descending relevance
84
     *
85
     * @param SearchResult[] $results
86
     * @return void
87
     */
88
    public static function sort(&$results)
89
    {
90
        usort($results, function (SearchResult $a, SearchResult $b) {
91
            if ($a->getRelevance()->getScore() < $b->getRelevance()->getScore()) {
92
                return 1;
93
            } elseif ($a->getRelevance()->getScore() > $b->getRelevance()->getScore()) {
94
                return -1;
95
            } else {
96
                return 0;
97
            }
98
        });
99
    }
100
101
    public function getHash()
102
    {
103
        return hash('crc32', $this->getUrl() . $this->getRelevance()->getScore());
104
    }
105
106
    public function jsonSerialize()
107
    {
108
        return [
109
            'url' => $this->getUrl(),
110
            'title' => $this->getTitle(),
111
            'description' => $this->getDescription(),
112
            'source' => $this->getSource()->jsonSerialize(),
113
            'relevance' => $this->getRelevance()->jsonSerialize(),
114
            'hash' => $this->getHash()
115
        ];
116
    }
117
}
118