Passed
Push — sheepy/introspection ( 000d40...b6b869 )
by Marco
02:43
created

FulltextSearchExtension::initToYml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 27
rs 9.7333
cc 2
nc 2
nop 0
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Compat;
5
6
use Firesphere\SolrSearch\Indexes\BaseIndex;
7
use Firesphere\SolrSearch\Queries\BaseQuery;
8
use Firesphere\SolrSearch\Results\SearchResult;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Core\Extension;
11
use SilverStripe\View\ArrayData;
12
13
/**
14
 * Class FulltextSearchExtension
15
 * Backward compatibility stubs for the Full text search module
16
 *
17
 * @package Firesphere\SolrSearch\Extensions
18
 * @property FulltextSearchExtension $owner
19
 */
20
class FulltextSearchExtension extends Extension
21
{
22
23
    /**
24
     * Convert the SearchResult class to a Full text search compatible ArrayData
25
     * @param SearchResult|ArrayData $results
26
     */
27
    public function updateSearchResults(&$results): void
28
    {
29
        $request = Controller::curr()->getRequest();
30
        $data = [
31
            'Matches'               => $results->getPaginatedMatches($request),
32
            'Facets'                => $results->getFacets(),
33
            'Highlights'            => $results->getHighlight(),
34
            'Spellcheck'            => $results->getSpellcheck(),
35
            'Suggestion'            => $results->getCollatedSpellcheck(),
36
            'SuggestionNice'        => $this->getCollatedNice($results->getCollatedSpellcheck()),
37
            'SuggestionQueryString' => $results->getCollatedSpellcheck()
38
        ];
39
        // Override the results with an FTS compatible feature list
40
        $results = ArrayData::create($data);
41
    }
42
43
    /**
44
     * Create a spellcheck string that's not the literal collation with Solr query parts
45
     *
46
     * @param string $spellcheck
47
     * @return string mixed
48
     */
49
    protected function getCollatedNice($spellcheck): string
50
    {
51
        return str_replace(' +', ' ', $spellcheck);
52
    }
53
54
    /**
55
     * @param BaseQuery $query
56
     * @param int $start deprecated in favour of $query, exists for backward compatibility with FTS
57
     * @param int $limit deprecated in favour of $query, exists for backward compatibility with FTS
58
     * @param array $params deprecated in favour of $query, exists for backward compatibility with FTS
59
     * @param bool $spellcheck deprecated in favour of #query, exists for backward compatibility with FTS
60
     * @return SearchResult|ArrayData|mixed
61
     * @deprecated This is used as an Fulltext Search compatibility method. Call doSearch instead with the correct Query
62
     */
63
    public function search($query, $start = 0, $limit = 10, $params = [], $spellcheck = null)
64
    {
65
        $query->getStart() === $start ?: $query->setStart($start);
66
        $query->getRows() === $limit ?: $query->setRows($limit);
67
        $query->hasSpellcheck() !== $spellcheck ?: $query->setSpellcheck($spellcheck);
68
        if (isset($params['fq']) && !count($query->getFields())) {
69
            $query->setFields($params['fq']);
70
        }
71
72
        /** @var BaseIndex $owner */
73
        $owner = $this->owner;
74
75
        return $owner->doSearch($query);
76
    }
77
}
78