1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: gordon |
6
|
|
|
* Date: 24/3/2561 |
7
|
|
|
* Time: 20:51 น. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Suilven\FreeTextSearch; |
11
|
|
|
|
12
|
|
|
use SilverStripe\Core\Config\Config; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Indexes |
16
|
|
|
* |
17
|
|
|
* @package Suilven\FreeTextSearch |
18
|
|
|
* |
19
|
|
|
* @phpcs:disable SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed |
20
|
|
|
*/ |
21
|
|
|
class Indexes |
22
|
|
|
{ |
23
|
|
|
/** @var array<string, \Suilven\FreeTextSearch\Index> */ |
24
|
|
|
private $indexesByName; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get an Index object by name from the config |
29
|
|
|
*/ |
30
|
|
|
public function getIndex(string $name): Index |
31
|
|
|
{ |
32
|
|
|
if (\is_null($this->indexesByName)) { |
|
|
|
|
33
|
|
|
$this->getIndexes(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this->indexesByName[$name]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get indexes from config |
42
|
|
|
* |
43
|
|
|
* @return array<\Suilven\FreeTextSearch\Index> ClassName -> Index |
44
|
|
|
*/ |
45
|
|
|
public function getIndexes(): array |
46
|
|
|
{ |
47
|
|
|
$indexes = []; |
48
|
|
|
|
49
|
|
|
$indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes') ; |
50
|
|
|
|
51
|
|
|
// reset |
52
|
|
|
$this->indexesByName = []; |
53
|
|
|
|
54
|
|
|
foreach ($indexesConfig as $indexConfig) { |
55
|
|
|
$index = new Index(); |
56
|
|
|
$index->setClass($indexConfig['index']['class']); |
57
|
|
|
$index->setName($indexConfig['index']['name']); |
58
|
|
|
foreach ($indexConfig['index']['fields'] as $fieldname) { |
59
|
|
|
$index->addField($fieldname); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (isset($indexConfig['index']['tokens'])) { |
63
|
|
|
foreach ($indexConfig['index']['tokens'] as $token) { |
64
|
|
|
$index->addToken($token); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// has one fields |
69
|
|
|
if (isset($indexConfig['index']['has_one'])) { |
70
|
|
|
foreach ($indexConfig['index']['has_one'] as $hasOneField) { |
71
|
|
|
$index->addHasOneField($hasOneField); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// has many fields |
76
|
|
|
// NB many many may need to be treated as bipartisan has many |
77
|
|
|
if (isset($indexConfig['index']['has_many'])) { |
78
|
|
|
foreach ($indexConfig['index']['has_many'] as $hasManyField) { |
79
|
|
|
$index->addHasManyField($hasManyField); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$indexes[] = $index; |
84
|
|
|
|
85
|
|
|
$this->indexesByName[$index->getName()] = $index; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $indexes; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** @return array<string> An array of facet fields in lower case, such as ['iso', 'aperture', 'shutterspeed'] */ |
93
|
|
|
public function getFacetFields(string $indexName): array |
94
|
|
|
{ |
95
|
|
|
$indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes'); |
96
|
|
|
$result = []; |
97
|
|
|
foreach ($indexesConfig as $indexConfig) { |
98
|
|
|
$name = ($indexConfig['index']['name']); |
99
|
|
|
|
100
|
|
|
if ($name !== $indexName) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($indexConfig['index']['tokens'])) { |
105
|
|
|
foreach ($indexConfig['index']['tokens'] as $token) { |
106
|
|
|
$result[] = \strtolower($token); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** @return array<string> */ |
116
|
|
|
public function getHasOneFields(string $indexName): array |
117
|
|
|
{ |
118
|
|
|
$indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes'); |
119
|
|
|
|
120
|
|
|
$result = []; |
121
|
|
|
foreach ($indexesConfig as $indexConfig) { |
122
|
|
|
$name = ($indexConfig['index']['name']); |
123
|
|
|
|
124
|
|
|
if ($name !== $indexName) { |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (isset($indexConfig['index']['has_one'])) { |
129
|
|
|
foreach ($indexConfig['index']['has_one'] as $hasOne) { |
130
|
|
|
$result[] = \strtolower($hasOne); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** @return array<string> */ |
140
|
|
|
public function getHasManyFields(string $indexName): array |
141
|
|
|
{ |
142
|
|
|
$indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes'); |
143
|
|
|
|
144
|
|
|
$result = []; |
145
|
|
|
foreach ($indexesConfig as $indexConfig) { |
146
|
|
|
$name = ($indexConfig['index']['name']); |
147
|
|
|
|
148
|
|
|
if ($name !== $indexName) { |
149
|
|
|
continue; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (isset($indexConfig['index']['has_many'])) { |
153
|
|
|
foreach ($indexConfig['index']['has_many'] as $hasManyField) { |
154
|
|
|
$result[] = \strtolower($hasManyField); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $result; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|