Passed
Push — release-11.5.x ( 729324...fafc82 )
by Rafael
38:35
created

SearchRequest   F

Complexity

Total Complexity 71

Size/Duplication

Total Lines 752
Duplicated Lines 0 %

Test Coverage

Coverage 88.44%

Importance

Changes 0
Metric Value
wmc 71
eloc 187
c 0
b 0
f 0
dl 0
loc 752
ccs 199
cts 225
cp 0.8844
rs 2.7199

49 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A removeAllFacets() 0 8 2
A getRawUserQueryIsNull() 0 5 1
A getSortingPart() 0 9 2
A addFacetValue() 0 10 2
A getRawUserQueryIsEmptyString() 0 14 3
A getHasSorting() 0 4 1
A setAdditionalFilters() 0 7 1
A getActiveFacetsUrlParameterStyle() 0 3 1
A getAdditionalFilters() 0 4 1
A getActiveFacetCount() 0 3 1
A reset() 0 21 4
A getHighestGroupPage() 0 17 5
A sortActiveFacets() 0 3 1
A setActiveFacets() 0 5 1
A removeSorting() 0 6 1
A getSeperatedSortings() 0 11 2
A __construct() 0 29 4
A getContextTypoScriptConfiguration() 0 3 1
A setGroupItemPage() 0 7 1
A getRawUserQuery() 0 5 1
A setRawQueryString() 0 6 1
A removeFacetValue() 0 9 2
A setResultsPerPage() 0 7 1
A setPage() 0 10 2
A getHasFacetValue() 0 3 1
A getArgumentNamespace() 0 3 1
A getActiveFacets() 0 3 1
A getArguments() 0 3 1
A getAsArray() 0 3 1
A removeAllGroupItemPages() 0 6 1
A getContextSystemLanguageUid() 0 3 1
A prefixWithNamespace() 0 3 1
A getActiveFacetNames() 0 3 1
A getSortingDirection() 0 3 1
A getSortingName() 0 3 1
A getCopyForSubRequest() 0 25 4
A getPage() 0 4 1
A setSorting() 0 7 1
A getActiveFacetValuesByName() 0 3 1
A isActiveFacetsSorted() 0 3 1
A removeAllFacetValuesByName() 0 8 2
A getEscapedGroupItemValue() 0 3 1
A getResultsPerPage() 0 4 1
A getSorting() 0 4 1
A getContextPageUid() 0 3 1
A getStateChanged() 0 3 1
A mergeArguments() 0 10 1
A getGroupItemPage() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like SearchRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SearchRequest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\Domain\Search;
19
20
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\UrlFacetContainer;
21
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
22
use ApacheSolrForTypo3\Solr\System\Util\ArrayAccessor;
23
use TYPO3\CMS\Core\Utility\ArrayUtility;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * The searchRequest is used to act as an api to the arguments that have been passed
28
 * with GET and POST.
29
 *
30
 * @author Timo Schmidt <[email protected]>
31
 */
32
class SearchRequest
33
{
34
    /**
35
     * The default plugin namespace.
36
     *
37
     * @var string
38
     */
39
    const DEFAULT_PLUGIN_NAMESPACE = 'tx_solr';
40
41
    /**
42
     * @var string
43
     */
44
    protected string $id;
45
46
    /**
47
     * Default namespace overwritten with the configured plugin namespace.
48
     *
49
     * @var string
50
     */
51
    protected string $argumentNameSpace = self::DEFAULT_PLUGIN_NAMESPACE;
52
53
    /**
54
     * Arguments that should be kept for sub requests.
55
     *
56
     * Default values, overwritten in the constructor with the namespaced arguments
57
     *
58
     * @var array
59
     */
60
    protected array $persistentArgumentsPaths = [
61
        'tx_solr:q',
62
        'tx_solr:filter',
63
        'tx_solr:sort',
64
    ];
65
66
    /**
67
     * @var bool
68
     */
69
    protected bool $stateChanged = false;
70
71
    /**
72
     * @var ArrayAccessor|null
73
     */
74
    protected ?ArrayAccessor $argumentsAccessor = null;
75
76
    /**
77
     * The sys_language_uid that was used in the context where the request was build.
78
     * This could be different from the "L" parameter and not relevant for urls,
79
     * because typolink itself will handle it.
80
     *
81
     * @var int
82
     */
83
    protected int $contextSystemLanguageUid = 0;
84
85
    /**
86
     * The page_uid that was used in the context where the request was build.
87
     *
88
     * The pageUid is not relevant for the typolink additionalArguments and therefore
89
     * a separate property.
90
     *
91
     * @var int
92
     */
93
    protected int $contextPageUid;
94
95
    /**
96
     * @var TypoScriptConfiguration|null
97
     */
98
    protected ?TypoScriptConfiguration $contextTypoScriptConfiguration;
99
100
    /**
101
     * Container for all active facets inside the URL(TYPO3/FE)
102
     *
103
     * @var UrlFacetContainer|null
104
     */
105
    protected ?UrlFacetContainer $activeFacetContainer;
106
107
    /**
108
     * @var array
109
     */
110
    protected array $persistedArguments = [];
111
112
    /**
113
     * @param array $argumentsArray
114
     * @param int $pageUid
115
     * @param int $sysLanguageUid
116
     * @param TypoScriptConfiguration|null $typoScriptConfiguration
117
     */
118 105
    public function __construct(
119
        array $argumentsArray = [],
120
        int $pageUid = 0,
121
        int $sysLanguageUid = 0,
122
        TypoScriptConfiguration $typoScriptConfiguration = null
123
    ) {
124 105
        $this->stateChanged = true;
125 105
        $this->persistedArguments = $argumentsArray;
126 105
        $this->contextPageUid = $pageUid;
127 105
        $this->contextSystemLanguageUid = $sysLanguageUid;
128 105
        $this->contextTypoScriptConfiguration = $typoScriptConfiguration;
129 105
        $this->id = spl_object_hash($this);
130
131
        // overwrite the plugin namespace and the persistentArgumentsPaths
132 105
        if (!is_null($typoScriptConfiguration)) {
133 55
            $this->argumentNameSpace = $typoScriptConfiguration->getSearchPluginNamespace() ?? self::DEFAULT_PLUGIN_NAMESPACE;
134
        }
135
136 105
        $this->persistentArgumentsPaths = [$this->argumentNameSpace . ':q', $this->argumentNameSpace . ':filter', $this->argumentNameSpace . ':sort', $this->argumentNameSpace . ':groupPage'];
137
138 105
        if (!is_null($typoScriptConfiguration)) {
139 55
            $additionalPersistentArgumentsNames = $typoScriptConfiguration->getSearchAdditionalPersistentArgumentNames();
140 55
            foreach ($additionalPersistentArgumentsNames ?? [] as $additionalPersistentArgumentsName) {
141
                $this->persistentArgumentsPaths[] = $this->argumentNameSpace . ':' . $additionalPersistentArgumentsName;
142
            }
143 55
            $this->persistentArgumentsPaths = array_unique($this->persistentArgumentsPaths);
144
        }
145
146 105
        $this->reset();
147
    }
148
149
    /**
150
     * @return string
151
     */
152 21
    public function getId(): string
153
    {
154 21
        return $this->id;
155
    }
156
157
    /**
158
     * Can be used do merge arguments into the request arguments
159
     *
160
     * @param array $argumentsToMerge
161
     * @return SearchRequest
162
     */
163 1
    public function mergeArguments(array $argumentsToMerge): SearchRequest
164
    {
165 1
        ArrayUtility::mergeRecursiveWithOverrule(
166 1
            $this->persistedArguments,
167 1
            $argumentsToMerge
168 1
        );
169
170 1
        $this->reset();
171
172 1
        return $this;
173
    }
174
175
    /**
176
     * Helper method to prefix an accessor with the argument's namespace.
177
     *
178
     * @param string $path
179
     * @return string
180
     */
181 79
    protected function prefixWithNamespace(string $path): string
182
    {
183 79
        return $this->argumentNameSpace . ':' . $path;
184
    }
185
186
    /**
187
     * @return array
188
     */
189 2
    public function getActiveFacetNames(): array
190
    {
191 2
        return $this->activeFacetContainer->getActiveFacetNames();
0 ignored issues
show
Bug introduced by
The method getActiveFacetNames() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
        return $this->activeFacetContainer->/** @scrutinizer ignore-call */ getActiveFacetNames();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
192
    }
193
194
    /**
195
     * Returns all facet values for a certain facetName
196
     * @param string $facetName
197
     * @return array
198
     */
199 18
    public function getActiveFacetValuesByName(string $facetName): array
200
    {
201 18
        return $this->activeFacetContainer->getActiveFacetValuesByName($facetName);
202
    }
203
204
    /**
205
     * @return array
206
     */
207
    public function getActiveFacets(): array
208
    {
209
        return $this->activeFacetContainer->getActiveFacets();
210
    }
211
212
    /**
213
     * Enable sorting of URL parameters
214
     * @noinspection PhpUnused
215
     */
216
    public function sortActiveFacets(): void
217
    {
218
        $this->activeFacetContainer->enableSort();
219
    }
220
221
    /**
222
     * @return bool
223
     */
224 23
    public function isActiveFacetsSorted(): bool
225
    {
226 23
        return $this->activeFacetContainer->isSorted();
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    public function getActiveFacetsUrlParameterStyle(): string
233
    {
234
        return $this->activeFacetContainer->getParameterStyle();
235
    }
236
237
    /**
238
     * Returns the active count of facets
239
     *
240
     * @return int
241
     */
242 2
    public function getActiveFacetCount(): int
243
    {
244 2
        return $this->activeFacetContainer->count();
245
    }
246
247
    /**
248
     * @param array $activeFacets
249
     *
250
     * @return SearchRequest
251
     * @noinspection PhpUnused
252
     */
253
    protected function setActiveFacets(array $activeFacets = []): SearchRequest
254
    {
255
        $this->activeFacetContainer->setActiveFacets($activeFacets);
256
257
        return $this;
258
    }
259
260
    /**
261
     * Adds a facet value to the request.
262
     *
263
     * @param string $facetName
264
     * @param mixed $facetValue
265
     *
266
     * @return SearchRequest
267
     */
268 25
    public function addFacetValue(string $facetName, $facetValue): SearchRequest
269
    {
270 25
        $this->activeFacetContainer->addFacetValue($facetName, $facetValue);
271
272 25
        if ($this->activeFacetContainer->hasChanged()) {
273 24
            $this->stateChanged = true;
274 24
            $this->activeFacetContainer->acknowledgeChange();
275
        }
276
277 25
        return $this;
278
    }
279
280
    /**
281
     * Removes a facet value from the request.
282
     *
283
     * @param string $facetName
284
     * @param mixed $facetValue
285
     *
286
     * @return SearchRequest
287
     */
288 6
    public function removeFacetValue(string $facetName, $facetValue): SearchRequest
289
    {
290 6
        $this->activeFacetContainer->removeFacetValue($facetName, $facetValue);
291 6
        if ($this->activeFacetContainer->hasChanged()) {
292 6
            $this->stateChanged = true;
293 6
            $this->activeFacetContainer->acknowledgeChange();
294
        }
295
296 6
        return $this;
297
    }
298
299
    /**
300
     * Removes all facet values from the request by a certain facet name
301
     *
302
     * @param string $facetName
303
     *
304
     * @return SearchRequest
305
     */
306 3
    public function removeAllFacetValuesByName(string $facetName): SearchRequest
307
    {
308 3
        $this->activeFacetContainer->removeAllFacetValuesByName($facetName);
309 3
        if ($this->activeFacetContainer->hasChanged()) {
310 3
            $this->stateChanged = true;
311 3
            $this->activeFacetContainer->acknowledgeChange();
312
        }
313 3
        return $this;
314
    }
315
316
    /**
317
     * Removes all active facets from the request.
318
     *
319
     * @return SearchRequest
320
     */
321 6
    public function removeAllFacets(): SearchRequest
322
    {
323 6
        $this->activeFacetContainer->removeAllFacets();
324 6
        if ($this->activeFacetContainer->hasChanged()) {
325 6
            $this->stateChanged = true;
326 6
            $this->activeFacetContainer->acknowledgeChange();
327
        }
328 6
        return $this;
329
    }
330
331
    /**
332
     * Check if an active facet has a given value
333
     *
334
     * @param string $facetName
335
     * @param mixed $facetValue
336
     * @return bool
337
     */
338 2
    public function getHasFacetValue(string $facetName, $facetValue): bool
339
    {
340 2
        return $this->activeFacetContainer->hasFacetValue($facetName, $facetValue);
341
    }
342
343
    /**
344
     * Returns all sortings in the sorting string e.g. ['title' => 'asc', 'relevance' => 'desc']
345
     */
346 41
    public function getSeperatedSortings(): array
347
    {
348 41
        $parsedSortings = [];
349 41
        $explodedSortings = GeneralUtility::trimExplode(',', $this->getSorting(), true);
350
351 41
        foreach ($explodedSortings as $sorting) {
352
            $sortingSeperated = explode(' ', $sorting);
353
            $parsedSortings[$sortingSeperated[0]] = $sortingSeperated[1];
354
        }
355
356 41
        return $parsedSortings;
357
    }
358
359
    /**
360
     * @return bool
361
     */
362 44
    public function getHasSorting(): bool
363
    {
364 44
        $path = $this->prefixWithNamespace('sort');
365 44
        return $this->argumentsAccessor->has($path);
0 ignored issues
show
Bug introduced by
The method has() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

365
        return $this->argumentsAccessor->/** @scrutinizer ignore-call */ has($path);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
366
    }
367
368
    /**
369
     * Returns the sorting string in the url e.g. title asc.
370
     *
371
     * @return string
372
     */
373 43
    public function getSorting(): string
374
    {
375 43
        $path = $this->prefixWithNamespace('sort');
376 43
        return $this->argumentsAccessor->get($path, '');
377
    }
378
379
    /**
380
     * Helper function to get the sorting configuration name or direction.
381
     *
382
     * @param int $index
383
     * @return string
384
     */
385 2
    protected function getSortingPart(int $index): ?string
386
    {
387 2
        $sorting = $this->getSorting();
388 2
        if ($sorting === '') {
389
            return null;
390
        }
391
392 2
        $parts = explode(' ', $sorting);
393 2
        return $parts[$index] ?? null;
394
    }
395
396
    /**
397
     * Returns the sorting configuration name that is currently used.
398
     *
399
     * @return string
400
     */
401 2
    public function getSortingName(): ?string
402
    {
403 2
        return $this->getSortingPart(0);
404
    }
405
406
    /**
407
     * Returns the sorting direction that is currently used.
408
     *
409
     * @return string
410
     */
411 1
    public function getSortingDirection(): string
412
    {
413 1
        return mb_strtolower($this->getSortingPart(1) ?? '');
414
    }
415
416
    /**
417
     * @return SearchRequest
418
     */
419 1
    public function removeSorting(): SearchRequest
420
    {
421 1
        $path = $this->prefixWithNamespace('sort');
422 1
        $this->argumentsAccessor->reset($path);
423 1
        $this->stateChanged = true;
424 1
        return $this;
425
    }
426
427
    /**
428
     * @param string $sortingName
429
     * @param string $direction (asc or desc)
430
     *
431
     * @return SearchRequest
432
     */
433 2
    public function setSorting(string $sortingName, string $direction = 'asc'): SearchRequest
434
    {
435 2
        $value = $sortingName . ' ' . $direction;
436 2
        $path = $this->prefixWithNamespace('sort');
437 2
        $this->argumentsAccessor->set($path, $value);
438 2
        $this->stateChanged = true;
439 2
        return $this;
440
    }
441
442
    /**
443
     * Method to set the paginated page of the search
444
     *
445
     * @param int $page
446
     * @return SearchRequest
447
     */
448 7
    public function setPage(int $page): SearchRequest
449
    {
450 7
        $this->stateChanged = true;
451 7
        $path = $this->prefixWithNamespace('page');
452 7
        $this->argumentsAccessor->set($path, $page);
453
        // use initial url by switching back to page 0
454 7
        if ($page === 0) {
455 5
            $this->argumentsAccessor->reset($path);
456
        }
457 7
        return $this;
458
    }
459
460
    /**
461
     * Returns the passed page.
462
     *
463
     * @return int|null
464
     */
465 52
    public function getPage(): ?int
466
    {
467 52
        $path = $this->prefixWithNamespace('page');
468 52
        return $this->argumentsAccessor->get($path);
469
    }
470
471
    /**
472
     * Can be used to reset all groupPages.
473
     *
474
     * @return SearchRequest
475
     */
476 23
    public function removeAllGroupItemPages(): SearchRequest
477
    {
478 23
        $path = $this->prefixWithNamespace('groupPage');
479 23
        $this->argumentsAccessor->reset($path);
480
481 23
        return $this;
482
    }
483
484
    /**
485
     * Can be used to paginate within a groupItem.
486
     *
487
     * @param string $groupName e.g. type
488
     * @param string $groupItemValue e.g. pages
489
     * @param int $page
490
     * @return SearchRequest
491
     */
492 4
    public function setGroupItemPage(string $groupName, string $groupItemValue, int $page): SearchRequest
493
    {
494 4
        $this->stateChanged = true;
495 4
        $escapedValue = $this->getEscapedGroupItemValue($groupItemValue);
496 4
        $path = $this->prefixWithNamespace('groupPage:' . $groupName . ':' . $escapedValue);
497 4
        $this->argumentsAccessor->set($path, $page);
498 4
        return $this;
499
    }
500
501
    /**
502
     * Retrieves the current page for this group item.
503
     *
504
     * @param string $groupName
505
     * @param string $groupItemValue
506
     * @return int
507
     */
508 3
    public function getGroupItemPage(string $groupName, string $groupItemValue): int
509
    {
510 3
        $escapedValue = $this->getEscapedGroupItemValue($groupItemValue);
511 3
        $path = $this->prefixWithNamespace('groupPage:' . $groupName . ':' . $escapedValue);
512 3
        return max(1, (int)$this->argumentsAccessor->get($path));
513
    }
514
515
    /**
516
     * Removes all non-alphanumeric values from the groupItem value to have a valid array key.
517
     *
518
     * @param string $groupItemValue
519
     * @return string
520
     */
521 5
    protected function getEscapedGroupItemValue(string $groupItemValue): string
522
    {
523 5
        return preg_replace('/[^A-Za-z0-9]/', '', $groupItemValue);
524
    }
525
526
    /**
527
     * Retrieves the highest page of the groups.
528
     *
529
     * @return int
530
     */
531 1
    public function getHighestGroupPage(): int
532
    {
533 1
        $max = 1;
534 1
        $path = $this->prefixWithNamespace('groupPage');
535 1
        $groupPages = $this->argumentsAccessor->get($path, []);
536 1
        foreach ($groupPages as $groups) {
537
            if (!is_array($groups)) {
538
                continue;
539
            }
540
            foreach ($groups as $groupItemPage) {
541
                if ((int)$groupItemPage > $max) {
542
                    $max = (int)$groupItemPage;
543
                }
544
            }
545
        }
546
547 1
        return $max;
548
    }
549
550
    /**
551
     * Method to overwrite the query string.
552
     *
553
     * @param string $rawQueryString
554
     * @return SearchRequest
555
     */
556 17
    public function setRawQueryString(string $rawQueryString = ''): SearchRequest
557
    {
558 17
        $this->stateChanged = true;
559 17
        $path = $this->prefixWithNamespace('q');
560 17
        $this->argumentsAccessor->set($path, $rawQueryString);
561 17
        return $this;
562
    }
563
564
    /**
565
     * Returns the passed rawQueryString.
566
     *
567
     * @return string
568
     */
569 44
    public function getRawUserQuery(): string
570
    {
571 44
        $path = $this->prefixWithNamespace('q');
572 44
        $query = $this->argumentsAccessor->get($path);
573 44
        return (string)($query ?? '');
574
    }
575
576
    /**
577
     * Method to check if the query string is an empty string
578
     * (also empty string or whitespaces only are handled as empty).
579
     *
580
     * When no query string is set (null) the method returns false.
581
     * @return bool
582
     */
583 43
    public function getRawUserQueryIsEmptyString(): bool
584
    {
585 43
        $path = $this->prefixWithNamespace('q');
586 43
        $query = $this->argumentsAccessor->get($path);
587
588 43
        if ($query === null) {
589 3
            return false;
590
        }
591
592 40
        if (trim($query) === '') {
593 2
            return true;
594
        }
595
596 38
        return false;
597
    }
598
599
    /**
600
     * This method returns true when no querystring is present at all.
601
     * Which means no search by the user was triggered
602
     *
603
     * @return bool
604
     */
605 49
    public function getRawUserQueryIsNull(): bool
606
    {
607 49
        $path = $this->prefixWithNamespace('q');
608 49
        $query = $this->argumentsAccessor->get($path);
609 49
        return $query === null;
610
    }
611
612
    /**
613
     * Sets the results per page that are used during search.
614
     *
615
     * @param int $resultsPerPage
616
     * @return SearchRequest
617
     */
618 50
    public function setResultsPerPage(int $resultsPerPage): SearchRequest
619
    {
620 50
        $path = $this->prefixWithNamespace('resultsPerPage');
621 50
        $this->argumentsAccessor->set($path, $resultsPerPage);
622 50
        $this->stateChanged = true;
623
624 50
        return $this;
625
    }
626
627
    /**
628
     * @return bool
629
     */
630 1
    public function getStateChanged(): bool
631
    {
632 1
        return $this->stateChanged;
633
    }
634
635
    /**
636
     * Returns the passed resultsPerPage value
637
     * @return int
638
     */
639 51
    public function getResultsPerPage(): int
640
    {
641 51
        $path = $this->prefixWithNamespace('resultsPerPage');
642 51
        return (int)$this->argumentsAccessor->get($path);
643
    }
644
645
    /**
646
     * Allows setting additional filters that are used on time and not transported during the request.
647
     *
648
     * @param array $additionalFilters
649
     * @return SearchRequest
650
     */
651 3
    public function setAdditionalFilters(array $additionalFilters): SearchRequest
652
    {
653 3
        $path = $this->prefixWithNamespace('additionalFilters');
654 3
        $this->argumentsAccessor->set($path, $additionalFilters);
655 3
        $this->stateChanged = true;
656
657 3
        return $this;
658
    }
659
660
    /**
661
     * Retrieves the additional filters that have been set
662
     *
663
     * @return array
664
     */
665 41
    public function getAdditionalFilters(): array
666
    {
667 41
        $path = $this->prefixWithNamespace('additionalFilters');
668 41
        return $this->argumentsAccessor->get($path, []);
669
    }
670
671
    /**
672
     * @return int
673
     */
674 2
    public function getContextSystemLanguageUid(): int
675
    {
676 2
        return $this->contextSystemLanguageUid;
677
    }
678
679
    /**
680
     * @return int
681
     */
682 2
    public function getContextPageUid(): int
683
    {
684 2
        return $this->contextPageUid;
685
    }
686
687
    /**
688
     * Get contextTypoScriptConfiguration
689
     *
690
     * @return TypoScriptConfiguration
691
     */
692 55
    public function getContextTypoScriptConfiguration(): ?TypoScriptConfiguration
693
    {
694 55
        return $this->contextTypoScriptConfiguration;
695
    }
696
697
    /**
698
     * Assigns the last known persistedArguments and restores their state.
699
     *
700
     * @return SearchRequest
701
     */
702 105
    public function reset(): SearchRequest
703
    {
704 105
        $this->argumentsAccessor = new ArrayAccessor($this->persistedArguments);
705 105
        $this->stateChanged = false;
706 105
        $this->activeFacetContainer = new UrlFacetContainer(
707 105
            $this->argumentsAccessor,
708 105
            $this->argumentNameSpace ?? self::DEFAULT_PLUGIN_NAMESPACE,
709 105
            $this->contextTypoScriptConfiguration === null ?
710 51
                UrlFacetContainer::PARAMETER_STYLE_INDEX :
711 105
                $this->contextTypoScriptConfiguration->getSearchFacetingUrlParameterStyle()
712 105
        );
713
714
        // If the default of sorting parameter should be true, a modification of this condition is needed.
715
        // If instance of contextTypoScriptConfiguration is not TypoScriptConfiguration the sort should be enabled too
716 105
        if ($this->contextTypoScriptConfiguration instanceof TypoScriptConfiguration
717 105
            && $this->contextTypoScriptConfiguration->getSearchFacetingUrlParameterSort()
718
        ) {
719
            $this->activeFacetContainer->enableSort();
720
        }
721
722 105
        return $this;
723
    }
724
725
    /**
726
     * This can be used to start a new sub request, e.g. for a faceted search.
727
     *
728
     * @param bool $onlyPersistentArguments
729
     * @return SearchRequest
730
     */
731 40
    public function getCopyForSubRequest(bool $onlyPersistentArguments = true): SearchRequest
732
    {
733 40
        if (!$onlyPersistentArguments) {
734
            // create a new request with all data
735
            $argumentsArray = $this->argumentsAccessor->getData();
736
            return new SearchRequest(
737
                $argumentsArray,
738
                $this->contextPageUid,
739
                $this->contextSystemLanguageUid,
740
                $this->contextTypoScriptConfiguration
741
            );
742
        }
743
744 40
        $arguments = new ArrayAccessor();
745 40
        foreach ($this->persistentArgumentsPaths as $persistentArgumentPath) {
746 40
            if ($this->argumentsAccessor->has($persistentArgumentPath)) {
747 32
                $arguments->set($persistentArgumentPath, $this->argumentsAccessor->get($persistentArgumentPath));
748
            }
749
        }
750
751 40
        return new SearchRequest(
752 40
            $arguments->getData(),
753 40
            $this->contextPageUid,
754 40
            $this->contextSystemLanguageUid,
755 40
            $this->contextTypoScriptConfiguration
756 40
        );
757
    }
758
759
    /**
760
     * @return string
761
     * @noinspection PhpUnused
762
     */
763 22
    public function getArgumentNamespace(): string
764
    {
765 22
        return $this->argumentNameSpace;
766
    }
767
768
    /**
769
     * @return array
770
     */
771 44
    public function getAsArray(): array
772
    {
773 44
        return $this->argumentsAccessor->getData();
774
    }
775
776
    /**
777
     * Returns only the arguments as array.
778
     *
779
     * @return array
780
     */
781 17
    public function getArguments(): array
782
    {
783 17
        return $this->argumentsAccessor->get($this->argumentNameSpace) ?? [];
784
    }
785
}
786