1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace smtech\StMarksSearch; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* An object representing a single search result |
7
|
|
|
* |
8
|
|
|
* @author Seth Battis <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class SearchResult extends ParameterArrayConstructor |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* URL of the search result |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $url; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Description of result's relevance |
20
|
|
|
* @var Relevance |
21
|
|
|
*/ |
22
|
|
|
protected $relevance; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Human-readable title |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $title; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Human-readable description |
32
|
|
|
* |
33
|
|
|
* Ideally 20-100 words, may be HTML-formatted (although links should be |
34
|
|
|
* stripped out). |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $description; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Simplified description of search domain source of the result |
41
|
|
|
* @var SearchSource |
42
|
|
|
*/ |
43
|
|
|
protected $source; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Construct a SearchResult |
47
|
|
|
* |
48
|
|
|
* Expects an associative parameter array: |
49
|
|
|
* |
50
|
|
|
* ``` |
51
|
|
|
* [ |
52
|
|
|
* 'url' => URL of the search result as a string, |
53
|
|
|
* 'title' => Title of the search result as a string, |
54
|
|
|
* 'relevance' => instance of `Relevance`, |
55
|
|
|
* 'source' => instance of `SearchSource`, |
56
|
|
|
* 'description' => Optional: search result descriptin as a string |
57
|
|
|
* ] |
58
|
|
|
* ``` |
59
|
|
|
* |
60
|
|
|
* @param mixed[string] $params |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function __construct($params) |
63
|
|
|
{ |
64
|
|
|
$this->requireParameter($params, 'url'); |
65
|
|
|
$this->requireParameter($params, 'title'); |
66
|
|
|
$this->requireParameter($params, 'relevance', Relevance::class); |
67
|
|
|
$this->requireParameter($params, 'source', SearchSource::class); |
68
|
|
|
|
69
|
|
|
$this->defaultParameter($params, 'description', '[no description available]'); |
70
|
|
|
|
71
|
|
|
$this->setUrl($params['url']); |
|
|
|
|
72
|
|
|
$this->setTitle($params['title']); |
|
|
|
|
73
|
|
|
$this->setRelevance($params['relevance']); |
|
|
|
|
74
|
|
|
$this->setSource($params['source']); |
|
|
|
|
75
|
|
|
$this->setDescription($params['description']); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sort into order of descending relevance |
80
|
|
|
* |
81
|
|
|
* @param SearchResult[] $results |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public static function sort(&$results) |
85
|
|
|
{ |
86
|
|
|
usort($results, function (SearchResult $a, SearchResult $b) { |
87
|
|
|
if ($a->getRelevance()->getScore() < $b->getRelevance()->getScore()) { |
|
|
|
|
88
|
|
|
return 1; |
89
|
|
|
} elseif ($a->getRelevance()->getScore() > $b->getRelevance()->getScore()) { |
|
|
|
|
90
|
|
|
return -1; |
91
|
|
|
} else { |
92
|
|
|
return 0; |
93
|
|
|
} |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.