Completed
Push — master ( 1d790a...bb9b7d )
by Seth
02:11
created

AbstractSearchDomain::forceBoolean()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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