CwpSearchResult::getOriginalLink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CWP\Search;
4
5
use SilverStripe\ORM\PaginatedList;
6
use SilverStripe\View\ArrayData;
7
use SilverStripe\View\ViewableData;
8
9
/**
10
 * Container for a set of search results
11
 */
12
class CwpSearchResult extends ViewableData
13
{
14
    private static $casting = [
15
        'Original' => 'Text',
16
        'OriginalLink' => 'Text',
17
        'Suggestion' => 'Text',
18
        'SuggestionLink' => 'Text',
19
        'Query' => 'Text',
20
        'SearchLink' => 'Text',
21
        'RSSLink' => 'Text',
22
        'AtomLink' => 'Text',
23
    ];
24
25
    /**
26
     * List of results
27
     *
28
     * @var PaginatedList
29
     */
30
    protected $matches;
31
32
    /**
33
     * Search terms for these results
34
     *
35
     * @var string
36
     */
37
    protected $query;
38
39
    /**
40
     * Suggested search keywords
41
     * Used when this search has suggested terms, but following suggestions isn't enabled
42
     *
43
     * @var string
44
     */
45
    protected $suggestion;
46
47
    /**
48
     * Original terms superceded by these result.
49
     * Used when a prior search had suggested terms, and follow suggestions is enabled.
50
     *
51
     * @var PaginatedList
52
     */
53
    protected $original;
54
55
    /**
56
     * Create a new CwpSearchResult
57
     *
58
     * @param string $terms
59
     * @param ArrayData $results Result from SolrIndex
60
     */
61
    public function __construct($terms = '', ArrayData $results = null)
62
    {
63
        $this->query = $terms;
64
        if ($results) {
65
            // Clean up the results.
66
            $matches = $results->Matches;
67
            foreach ($matches as $result) {
68
                if (!$result->canView()) {
69
                    $matches->remove($result);
70
                }
71
            }
72
73
            $this->matches = $matches;
74
            $this->suggestion = $results->SuggestionNice;
75
        }
76
    }
77
78
    /**
79
     * Get search results
80
     *
81
     * @return PaginatedList
82
     */
83
    public function getResults()
84
    {
85
        return $this->matches;
86
    }
87
88
    /**
89
     * Check if there are found results
90
     *
91
     * @return bool
92
     */
93
    public function hasResults()
94
    {
95
        return $this->matches && $this->matches->exists();
96
    }
97
98
    /**
99
     * Get search keywords matching these results
100
     *
101
     * @return string
102
     */
103
    public function getQuery()
104
    {
105
        return $this->query;
106
    }
107
108
    /**
109
     * Get suggested search keywords
110
     *
111
     * @return string
112
     */
113
    public function getSuggestion()
114
    {
115
        return $this->suggestion;
116
    }
117
118
    /**
119
     * Get original search keywords superceded by these results
120
     *
121
     * @return string
122
     */
123
    public function getOriginal()
124
    {
125
        return $this->original;
126
    }
127
128
    /**
129
     * Set original keywords
130
     *
131
     * @param string $original
132
     */
133
    public function setOriginal($original)
134
    {
135
        $this->original = $original;
0 ignored issues
show
Documentation Bug introduced by
It seems like $original of type string is incompatible with the declared type SilverStripe\ORM\PaginatedList of property $original.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
136
    }
137
138
    /**
139
     * Get the link to the suggested search
140
     *
141
     * @param string $format Optional output format
142
     * @return string
143
     */
144
    public function getSuggestionLink($format = null)
145
    {
146
        return $this->getLink($this->suggestion, $format);
147
    }
148
149
    /**
150
     * Gets the link to the given search
151
     *
152
     * @param string $format Optional output format
153
     * @return string
154
     */
155
    public function getSearchLink($format = null)
156
    {
157
        return $this->getLink($this->query, $format);
158
    }
159
160
    /**
161
     * Gets the link to the original search, with suggestions disabled
162
     *
163
     * @param string $format Optional output format
164
     * @return string
165
     */
166
    public function getOriginalLink($format = null)
167
    {
168
        return $this->getLink($this->original, $format) . "&suggestions=0";
169
    }
170
171
    /**
172
     * Get link to these results in RSS format
173
     *
174
     * @return string
175
     */
176
    public function getRSSLink()
177
    {
178
        return $this->getLink($this->query, 'rss');
179
    }
180
181
    /**
182
     * Get link to these results in atom format
183
     *
184
     * @return string
185
     */
186
    public function getAtomLink()
187
    {
188
        return $this->getLink($this->query, 'atom');
189
    }
190
191
    /**
192
     * Get a search link for given terms
193
     *
194
     * @param string $terms
195
     * @return string|null
196
     */
197
    protected function getLink($terms, $format = null)
198
    {
199
        if (!$terms) {
200
            return null;
201
        }
202
        $link = 'search/SearchForm?Search='.rawurlencode($terms);
203
        if ($format) {
204
            $link .= '&format='.rawurlencode($format);
205
        }
206
        return $link;
207
    }
208
209
    public function hasField($field)
210
    {
211
        // Fix customise not detecting custom field getters
212
        return array_key_exists($field, $this->config()->casting);
213
    }
214
}
215