Completed
Push — master ( 0ffa20...1d790a )
by Seth
02:58
created

AbstractSearchDomain::sortByRelevance()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace smtech\StMarksSearch;
4
5
use Exception;
6
7
/**
8
 * Abstract class defining a search domain
9
 *
10
 * Includes a basic constructor and a reusable sorting method.
11
 *
12
 * @author Seth Battis <[email protected]>
13
 */
14
abstract class AbstractSearchDomain
15
{
16
    /**
17
     * Human-readable name of the search domain
18
     *
19
     * Ideally, this is loaded dynamically from the domain via API -- and any
20
     * dynamic result will override any preset configuration.
21
     * @var string
22
     */
23
    protected $name;
24
25
    /**
26
     * URL of the search domain's home page
27
     *
28
     * Ideally, this is loaded or constructed dynamically via API -- and any
29
     * dynamic result will override any preset configuration.
30
     * @var string
31
     */
32
    protected $url;
33
34
    /**
35
     * URL of the search domain's icon
36
     *
37
     * This will be rendered 16x16 pixels when displayed.
38
     * @var string
39
     */
40
    protected $icon;
41
42
    /**
43
     * Construct a SearchDomain
44
     *
45
     * Parameters are passed to this constuctor as an associative array of values, including, at a minimum:
46
     *
47
     * ```
48
     * [
49
     *   'name' => 'A human-readable name for the domain',
50
     *   'url' => 'URL of the home page of the domain'
51
     * ]
52
     * ```
53
     *
54
     * Subclasses may (and will) add additional required parameters to that
55
     * list.
56
     *
57
     * @param mixed[] $params
58
     *
59
     * @throws Exception If `$params['url']` is not a valid URL or
60
     *         `$params['name']` is empty.
61
     */
62
    public function __construct($params)
63
    {
64
        assert(isset($params['url']), new Exception('`url` parameter required'));
65
        $this->setUrl($params['url']);
66
67
        assert(isset($params['name']), new Exception('`name` parameter required'));
68
        $this->setName($params['name']);
69
70
        if (isset($params['icon'])) {
71
            $this->setIcon($params['icon']);
72
        }
73
    }
74
75
    /**
76
     * Update the URL of the search domain
77
     *
78
     * @used-by AbstractSearchDomain::__construct()
79
     * @param string $url
80
     * @throws Exception If `$url` is empty or is not a valid URL
81
     */
82
    protected function setUrl($url)
83
    {
84
        assert(
85
            !empty($url) &&
86
            filter_var($url, FILTER_VALIDATE_URL) !== false,
87
            new Exception("Valid url parameter required, received:" . PHP_EOL . print_r($url, true))
88
        );
89
        $this->url = $url;
90
    }
91
92
    /**
93
     * Set the name of the search domain
94
     *
95
     * @used-by AbstractSearchDomain::__construct()
96
     * @param string $name
97
     * @throws Exception If `$name` is empty
98
     */
99
    protected function setName($name)
100
    {
101
        assert(
102
            !empty($name),
103
            new Exception('Non-empty parameter required, received:' . PHP_EOL . print_r($name, true))
104
        );
105
        $this->name = $name;
106
    }
107
108
    /**
109
     * Set the icon URL for the search domain
110
     *
111
     *  Does nothing if `$url` is not a valid URL
112
     *
113
     * @used-by AbstractSearchDomain::__construct()
114
     * @param string $icon
115
     */
116
    protected function setIcon($icon)
117
    {
118
        if (filter_var($icon, FILTER_VALIDATE_URL)) {
119
            $this->icon = $icon;
120
        }
121
    }
122
123
    /**
124
     * Search within this domain
125
     *
126
     * @param string $query
127
     * @return SearchResult[] Ordered by descending relevance
128
     */
129
    abstract public function search($query);
130
131
    /**
132
     * Sort into order of descending relevance
133
     *
134
     * @param SearchResult[] $results
135
     * @return void
136
     */
137
    protected function sortByRelevance(&$results)
138
    {
139
        usort($results, function (SearchResult $a, SearchResult $b) {
140
            if ($a->getRelevance()->getScore() < $b->getRelevance()->getScore()) {
141
                return 1;
142
            } elseif ($a->getRelevance()->getScore() > $b->getRelevance()->getScore()) {
143
                return -1;
144
            } else {
145
                return 0;
146
            }
147
        });
148
    }
149
150
    /**
151
     * Human-readable name of the search domain
152
     *
153
     * @return string
154
     */
155
    public function getName()
156
    {
157
        return $this->name;
158
    }
159
160
    /**
161
     * URL of the home page of the search domain
162
     *
163
     * @return string
164
     */
165
    public function getUrl()
166
    {
167
        return $this->url;
168
    }
169
170
    /**
171
     * URL of the icon of this search domain
172
     *
173
     * @return string
174
     */
175
    public function getIcon()
176
    {
177
        return $this->icon;
178
    }
179
180
    /**
181
     * Force a boolean result from a particular parameter key
182
     *
183
     * @param mixed[string] $params
1 ignored issue
show
Documentation introduced by
The doc-type mixed[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

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.

Loading history...
184
     * @param string $key    [description]
185
     * @return boolean `TRUE` iff `$params[$key]` exists and has a true value
186
     *                        (`1`, `'yes'`, `'true'`, `true`, etc.), `FALSE`
187
     *                        otherwise.
188
     */
189
    protected function forceBoolean($params, $key)
190
    {
191
        return isset($params[$key]) && filter_var($params[$key], FILTER_VALIDATE_BOOLEAN);
192
    }
193
}
194