1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: gordon |
6
|
|
|
* Date: 24/3/2561 |
7
|
|
|
* Time: 20:36 น. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Suilven\FreeTextSearch\Container; |
11
|
|
|
|
12
|
|
|
use SilverStripe\ORM\ArrayList; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class SearchResults |
16
|
|
|
* |
17
|
|
|
* Store the search results in a manner that is renderable in a SilverStripe template |
18
|
|
|
* |
19
|
|
|
* @package Suilven\FreeTextSearch\Container |
20
|
|
|
*/ |
21
|
|
|
class SearchResults |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** @var array<string,int|bool|float|string> */ |
25
|
|
|
private $facets; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $index; |
29
|
|
|
|
30
|
|
|
/** @var int the total number of results */ |
31
|
|
|
private $totalNumberOfResults = 0; |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
private $page = 0; |
35
|
|
|
|
36
|
|
|
/** @var int */ |
37
|
|
|
private $pageSize = 20; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
private $query = ''; |
41
|
|
|
|
42
|
|
|
/** @var \SilverStripe\ORM\ArrayList|null */ |
43
|
|
|
private $records; |
44
|
|
|
|
45
|
|
|
/** @var array<string> */ |
46
|
|
|
private $suggestions; |
47
|
|
|
|
48
|
|
|
/** @var float the time in seconds */ |
49
|
|
|
private $time; |
50
|
|
|
|
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
$this->time = 0; |
54
|
|
|
$this->suggestions =[]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** @param array<string,int|bool|float|string> $newFacets */ |
59
|
|
|
public function setFacets(array $newFacets): void |
60
|
|
|
{ |
61
|
|
|
$this->facets = $newFacets; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
public function setIndex(string $newIndex): void |
66
|
|
|
{ |
67
|
|
|
$this->index = $newIndex; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
public function getTotaNumberOfResults(): int |
72
|
|
|
{ |
73
|
|
|
return $this->totalNumberOfResults; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
public function setTotalNumberOfResults(int $newTotalNumberOfResults): void |
78
|
|
|
{ |
79
|
|
|
$this->totalNumberOfResults = $newTotalNumberOfResults; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
public function getTotalPages(): int |
84
|
|
|
{ |
85
|
|
|
$nPages = \ceil($this->totalNumberOfResults / $this->pageSize); |
86
|
|
|
|
87
|
|
|
return \intval($nPages); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
public function getPage(): int |
92
|
|
|
{ |
93
|
|
|
return $this->page; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
public function setPage(int $newPage): void |
98
|
|
|
{ |
99
|
|
|
$this->page = $newPage; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
public function getPageSize(): int |
104
|
|
|
{ |
105
|
|
|
return $this->pageSize; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
public function setPageSize(int $newPageSize): void |
110
|
|
|
{ |
111
|
|
|
$this->pageSize = $newPageSize; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
public function getQuery(): string |
116
|
|
|
{ |
117
|
|
|
return $this->query; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
public function setQuery(string $newQuery): void |
122
|
|
|
{ |
123
|
|
|
$this->query = $newQuery; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
public function setRecords(ArrayList $newRecords): void |
128
|
|
|
{ |
129
|
|
|
$this->records = $newRecords; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
public function getRecords(): \SilverStripe\ORM\ArrayList |
134
|
|
|
{ |
135
|
|
|
return \is_null($this->records) |
136
|
|
|
? new ArrayList([]) |
137
|
|
|
: $this->records; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** Accessor to the suggestions |
142
|
|
|
* |
143
|
|
|
* @return array<string> |
144
|
|
|
*/ |
145
|
|
|
public function getSuggestions(): array |
146
|
|
|
{ |
147
|
|
|
return $this->suggestions; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** @param array<string> $newSuggestions */ |
152
|
|
|
public function setSuggestions(array $newSuggestions): void |
153
|
|
|
{ |
154
|
|
|
$this->suggestions = $newSuggestions; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
public function getTime(): float |
159
|
|
|
{ |
160
|
|
|
return $this->time; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
public function setTime(float $newTime): void |
165
|
|
|
{ |
166
|
|
|
$this->time = $newTime; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|