Completed
Push — master ( 071f6e...2ed909 )
by Seth
25:30
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 extends ParameterArrayConstructor
15
{
16
    const NAME = 'name';
17
    const URL = 'url';
18
    const ICON = 'icon';
19
20
    /**
21
     * Human-readable name of the search domain
22
     *
23
     * Ideally, this is loaded dynamically from the domain via API -- and any
24
     * dynamic result will override any preset configuration.
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * URL of the search domain's home page
31
     *
32
     * Ideally, this is loaded or constructed dynamically via API -- and any
33
     * dynamic result will override any preset configuration.
34
     * @var string
35
     */
36
    protected $url;
37
38
    /**
39
     * URL of the search domain's icon
40
     *
41
     * This will be rendered 16x16 pixels when displayed.
42
     * @var string
43
     */
44
    protected $icon;
45
46
    /**
47
     * Construct a SearchDomain
48
     *
49
     * Parameters are passed to this constuctor as an associative array of values, including, at a minimum:
50
     *
51
     * ```
52
     * [
53
     *   'name' => 'A human-readable name for the domain',
54
     *   'url' => 'URL of the home page of the domain'
55
     * ]
56
     * ```
57
     *
58
     * Subclasses may (and will) add additional required parameters to that
59
     * list.
60
     *
61
     * @param mixed[] $params
62
     *
63
     * @throws Exception If `$params['url']` is not a valid URL or
64
     *         `$params['name']` is empty.
65
     */
66
    public function __construct($params)
67
    {
68
        $this->requireParameter($params, self::URL);
69
        $this->requireParameter($params, self::NAME);
70
71
        parent::__construct($params);
72
    }
73
74
    /**
75
     * Update the URL of the search domain
76
     *
77
     * @used-by AbstractSearchDomain::__construct()
78
     * @param string $url
79
     * @throws Exception If `$url` is empty or is not a valid URL
80
     */
81
    protected function setUrl($url)
82
    {
83
        if (empty($url) ||
84
            filter_var($url, FILTER_VALIDATE_URL) === false
85
        ) {
86
            throw new Exception("Valid url parameter required, received:" . PHP_EOL . print_r($url, true));
87
        }
88
        $this->url = $url;
89
    }
90
91
    /**
92
     * Set the name of the search domain
93
     *
94
     * @used-by AbstractSearchDomain::__construct()
95
     * @param string $name
96
     * @throws Exception If `$name` is empty
97
     */
98
    protected function setName($name)
99
    {
100
        if (empty($name)) {
101
            throw new Exception('Non-empty parameter required, received:' . PHP_EOL . print_r($name, true));
102
        }
103
        $this->name = $name;
104
    }
105
106
    /**
107
     * Set the icon URL for the search domain
108
     *
109
     *  Does nothing if `$url` is not a valid URL
110
     *
111
     * @used-by AbstractSearchDomain::__construct()
112
     * @param string $icon
113
     */
114
    protected function setIcon($icon)
115
    {
116
        if (filter_var($icon, FILTER_VALIDATE_URL)) {
117
            $this->icon = $icon;
118
        }
119
    }
120
121
    /**
122
     * Search within this domain
123
     *
124
     * @param string $query
125
     * @return SearchResult[] Ordered by descending relevance
126
     */
127
    abstract public function search($query);
128
}
129