-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiFacet.php
executable file
·278 lines (243 loc) · 8.84 KB
/
ApiFacet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php declare(strict_types=1);
namespace Boxalino\RealTimeUserExperience\Model\Response\Content;
use Boxalino\RealTimeUserExperience\Api\ApiFilterablePropertiesProviderInterface;
use Boxalino\RealTimeUserExperience\Service\Api\Response\Accessor\Facet;
use Boxalino\RealTimeUserExperience\Service\Api\Response\Accessor\FacetValue;
use Boxalino\RealTimeUserExperience\Service\Api\Util\RequestParametersTrait;
use Boxalino\RealTimeUserExperienceApi\Service\Api\Response\Accessor\AccessorFacetModelInterface;
use Boxalino\RealTimeUserExperienceApi\Framework\Content\Listing\ApiFacetModelAbstract;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\UrlInterface;
/**
* Class ApiFacet
*
* Item refers to any data model/logic that is desired to be rendered/displayed
* The integrator can decide to either use all data as provided by the Narrative API,
* or to design custom data layers to represent the fetched content
*
* @package Boxalino\RealTimeUserExperience\Model\Response\Content
*/
class ApiFacet extends ApiFacetModelAbstract
implements AccessorFacetModelInterface
{
use RequestParametersTrait;
/**
* @var \Magento\Eav\Model\Config
*/
protected $_config;
/**
* @var array | null
*/
protected $urlParameters = null;
/**
* @var UrlInterface
*/
protected $urlBuilder;
/**
* @var array
*/
protected $filterableProperties = [];
/**
* @var ApiFilterablePropertiesProviderInterface
*/
protected $filterablePropertiesProvider;
public function __construct(
\Magento\Eav\Model\Config $config,
UrlInterface $urlBuilder,
ApiFilterablePropertiesProviderInterface $filterablePropertiesProvider,
string $facetValuesDelimiter = self::SELECTED_FACET_VALUES_URL_DELIMITER,
string $facetPrefix = self::BOXALINO_SYSTEM_FACET_PREFIX,
bool $useFacetOptionIdFilter = false,
string $facetValueKey = "value"
){
parent::__construct();
$this->_config = $config;
$this->urlBuilder = $urlBuilder;
$this->facetPrefix = $facetPrefix;
$this->facetValuesDelimiter = $facetValuesDelimiter;
$this->useFacetOptionIdFilter = $useFacetOptionIdFilter;
$this->facetValueKey = $facetValueKey;
$this->filterablePropertiesProvider = $filterablePropertiesProvider;
}
/**
* @param array $facets
* @return $this
*/
public function setFacets(array $facets) : ApiFacetModelAbstract
{
parent::setFacets($facets);
/** @var Facet $filter */
foreach($this->facets as $filter)
{
/** @var FacetValue $facetValue */
foreach($filter->getValues() as $facetValue)
{
$facetValue->setRemoveUrl($this->getRemoveUrlByFacetValue($filter, $facetValue));
$facetValue->setSelectUrl($this->getUrlByFacetValue($filter, $facetValue));
}
}
return $this;
}
/**
* Accessing translation for the property name from DB
* If there is a matching property in M2 db - the facet is a M2 property
* If there is no matching property in M2 db - the facet is a Boxalino-provided property and prefix must be appended
*
* @param string $propertyName
* @return string
*/
public function getLabel(string $propertyName) : string
{
try{
$label = $this->_getAttributeModel($propertyName)->getStoreLabel();
if(!empty($label))
{
return $label;
}
} catch(\Throwable $exception) {}
return ucwords(str_replace("_", " ", $propertyName));
}
/**
* Accessing Magento2 attribute
*
* @param string $attributeCode
* @return AbstractAttribute
*/
public function _getAttributeModel(string $attributeCode)
{
return $this->_config->getAttribute('catalog_product', $attributeCode);
}
/**
* Create the URL for when a facet is selected
* The range fields properties are set via JS
*
* @param Facet $facet
* @param FacetValue $facetValue
*/
public function getUrlByFacetValue(Facet $facet, FacetValue $facetValue) : string
{
$value = $this->getValue($facetValue);
if($facet->isSelected() && $facet->allowMultiselect() && !$facetValue->isSelected())
{
$value = implode($this->facetValuesDelimiter,
array_merge(explode($this->facetValuesDelimiter, $this->urlParameters[$facet->getRequestField()]), [$this->getValue($facetValue)])
);
}
$query = [
$facet->getRequestField() => $value,
$this->getPageNumberParameter() => null,
];
return $this->urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, '_query' => $query]);
}
/**
* As seen in Magento2 filter item model \Magento\Catalog\Model\Layer\Filter\Item
*
* @param Facet $facet
* @param FacetValue $facetValue
* @return string
*/
public function getRemoveUrlByFacetValue(Facet $facet, FacetValue $facetValue) : string
{
$query = $this->getRemoveUrlQueryByFacetValue($facet, $facetValue);
$params['_current'] = true;
$params['_use_rewrite'] = true;
$params['_query'] = $query;
$params['_escape'] = true;
return $this->urlBuilder->getUrl('*/*/*', $params);
}
/**
* In case of the range facet - both range-from & range-to selected values are removed
* In case of multiselect facet - the value is removed from the list
* Otherwise, the value is set to clean (null)
*
* @param Facet $facet
* @param FacetValue $facetValue
* @return array
*/
public function getRemoveUrlQueryByFacetValue(Facet $facet, FacetValue $facetValue) : array
{
$parameters = $this->getUrlParams();
if($facet->isSelected() && $facet->isRange())
{
if($facetValue->getMinSelectedValue())
{
$parameters[$facet->getRangeFromField()] = $facet->getCleanValue();
}
if($facetValue->getMaxSelectedValue())
{
$parameters[$facet->getRangeToField()] = $facet->getCleanValue();
}
return $parameters;
}
if($facetValue->isSelected())
{
$values = explode($this->facetValuesDelimiter, $parameters[$facet->getRequestField()]);
$parameters[$facet->getRequestField()] = $facet->getCleanValue();
if($facet->allowMultiselect())
{
unset($values[array_search($this->getValue($facetValue), $values)]);
$parameters[$facet->getRequestField()] = implode($this->facetValuesDelimiter, $values);
}
return $parameters;
}
return $parameters;
}
/**
* List of active facets and their value
* In case of a range facet - it checks if it`s a range-from or range-to value selected
*
* @return array
*/
public function getUrlParams() : array
{
if(is_null($this->urlParameters))
{
$parameters = [];
/** @var Facet $selectedFacet */
foreach($this->getSelectedFacets() as $selectedFacet)
{
if($selectedFacet->isRange())
{
/** @var FacetValue $facetValue */
$facetValue = $selectedFacet->getSelectedValues()[0];
if($facetValue->getMinSelectedValue())
{
$parameters[$selectedFacet->getRangeFromField()] = $facetValue->getMinSelectedValue();
}
if($facetValue->getMaxSelectedValue())
{
$parameters[$selectedFacet->getRangeToField()] = $facetValue->getMaxSelectedValue();
}
continue;
}
$parameters[$selectedFacet->getRequestField()] = implode(
$this->facetValuesDelimiter,
array_map(
function(FacetValue $facetValue) {
return $this->getValue($facetValue);
},
$selectedFacet->getSelectedValues()
)
);
}
$this->urlParameters = $parameters;
}
return $this->urlParameters;
}
/**
* @param $facet
* @return bool
*/
protected function facetRequiresPrefix($facet) : bool
{
if(empty($this->filterableProperties))
{
$this->filterableProperties = $this->filterablePropertiesProvider->getFilterableAttributes();
}
if(in_array($facet->getField(), $this->filterableProperties))
{
return false;
}
return true;
}
}