1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\SolrFluent\States; |
4
|
|
|
|
5
|
|
|
use Firesphere\SolrSearch\Interfaces\SiteStateInterface; |
6
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
7
|
|
|
use Firesphere\SolrSearch\States\SiteState; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use TractorCow\Fluent\Extension\FluentExtension; |
10
|
|
|
use TractorCow\Fluent\Model\Locale; |
11
|
|
|
use TractorCow\Fluent\State\FluentState; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class \Firesphere\SolrFluent\States\FluentSiteState |
15
|
|
|
* |
16
|
|
|
* Manage the state of the site to apply the correct locale from Fluent to search |
17
|
|
|
* |
18
|
|
|
* @package Firesphere\SolrFluent\States |
19
|
|
|
*/ |
20
|
|
|
class FluentSiteState extends SiteState implements SiteStateInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Does the state apply to this class |
24
|
|
|
* |
25
|
|
|
* @param $class |
26
|
|
|
* @return bool |
27
|
|
|
* @throws ReflectionException |
28
|
|
|
*/ |
29
|
1 |
|
public function appliesTo($class): bool |
30
|
|
|
{ |
31
|
1 |
|
return $this->isEnabled() && |
32
|
1 |
|
SiteState::hasExtension($class, FluentExtension::class) && |
33
|
1 |
|
Locale::getCached()->count(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Is this state applicable to this extension |
38
|
|
|
* |
39
|
|
|
* @param string $state |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function stateIsApplicable($state): bool |
43
|
|
|
{ |
44
|
|
|
$locales = Locale::get()->column('Locale'); |
45
|
|
|
|
46
|
|
|
return in_array($state, $locales, true) && count($locales); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Reset the SiteState to it's default state |
51
|
|
|
* |
52
|
|
|
* @param string|null $state |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function setDefaultState($state = null) |
56
|
|
|
{ |
57
|
|
|
FluentState::singleton()->setLocale($state); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return the current state of the site |
62
|
|
|
* |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
|
|
public function currentState() |
66
|
|
|
{ |
67
|
|
|
return FluentState::singleton()->getLocale(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Activate a given state. This should only be done if the state is applicable |
72
|
|
|
* |
73
|
|
|
* @param string $state |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function activateState($state) |
77
|
|
|
{ |
78
|
|
|
FluentState::singleton()->setLocale($state); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Update the Solr query to match the current State |
83
|
|
|
* |
84
|
|
|
* @param BaseQuery $query |
85
|
|
|
*/ |
86
|
|
|
public function updateQuery(&$query) |
87
|
|
|
{ |
88
|
|
|
$locale = FluentState::singleton()->getLocale(); |
89
|
|
|
if ($locale === '') { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->updatePart($query, $locale, 'BoostedFields'); |
94
|
|
|
$this->updatePart($query, $locale, 'Filter'); |
95
|
|
|
$this->updatePart($query, $locale, 'Exclude'); |
96
|
|
|
|
97
|
|
|
$fields = []; |
98
|
|
|
foreach ($query->getFields() as $field) { |
99
|
|
|
$fields[] = $field . '_' . $locale; |
100
|
|
|
} |
101
|
|
|
$query->setFields($fields); |
102
|
|
|
|
103
|
|
|
$localisedTerms = []; |
104
|
|
|
foreach ($query->getTerms() as $term) { |
105
|
|
|
$localisedTerms = $this->updateTerms($term, $locale, $localisedTerms); |
106
|
|
|
} |
107
|
|
|
$query->setTerms($localisedTerms); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Update a part of the query for the get and set methods. |
112
|
|
|
* |
113
|
|
|
* @param $query |
114
|
|
|
* @param string $locale |
115
|
|
|
* @param string $method |
116
|
|
|
*/ |
117
|
|
|
protected function updatePart(&$query, string $locale, string $method): void |
118
|
|
|
{ |
119
|
|
|
if (!$locale) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
$new = []; |
123
|
|
|
$getMethod = 'get' . $method; |
124
|
|
|
$setMethod = 'set' . $method; |
125
|
|
|
foreach ($query->$getMethod() as $filterField => $value) { |
126
|
|
|
$fieldName = $filterField . '_' . $locale; |
127
|
|
|
$new[$fieldName] = $value; |
128
|
|
|
} |
129
|
|
|
$query->$setMethod($new); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Update the field filters to localised filters |
134
|
|
|
* |
135
|
|
|
* @param $term |
136
|
|
|
* @param string $locale |
137
|
|
|
* @param array $localisedTerms |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
private function updateTerms($term, string $locale, array $localisedTerms): array |
141
|
|
|
{ |
142
|
|
|
if (count($term['fields'])) { |
143
|
|
|
foreach ($term['fields'] as &$termField) { |
144
|
|
|
$termField .= '_' . $locale; |
145
|
|
|
} |
146
|
|
|
unset($termField); |
147
|
|
|
} |
148
|
|
|
$localisedTerms[] = $term; |
149
|
|
|
|
150
|
|
|
return $localisedTerms; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|