-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathTestAdapter.php
134 lines (109 loc) · 3.24 KB
/
TestAdapter.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
<?php
/*
* This file is part of the MassiveSearchBundle
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Massive\Bundle\SearchBundle\Search\Adapter;
use Massive\Bundle\SearchBundle\Search\AdapterInterface;
use Massive\Bundle\SearchBundle\Search\Document;
use Massive\Bundle\SearchBundle\Search\Factory;
use Massive\Bundle\SearchBundle\Search\SearchQuery;
use Massive\Bundle\SearchBundle\Search\SearchResult;
/**
* Test adapter for testing scenarios.
*/
class TestAdapter implements AdapterInterface
{
protected $documents = [];
protected $factory;
public function __construct(Factory $factory)
{
$this->factory = $factory;
}
public function index(Document $document, $indexName)
{
$this->documents[$indexName][$document->getId()] = $document;
}
public function deindex(Document $document, $indexName)
{
if (!$indexName) {
return;
}
foreach ($this->documents[$indexName] as $i => $selfDocument) {
if ($document->getId() === $selfDocument->getId()) {
unset($this->documents[$indexName][$i]);
}
}
$this->documents[$indexName] = \array_values($this->documents[$indexName]);
}
public function purge($indexName)
{
unset($this->documents[$indexName]);
}
/**
* Return all the indexed documents.
*
* NOTE: Not part of the API
*
* @return Document[]
*/
public function getDocuments()
{
$documents = [];
foreach ($this->documents as $localizedDocuments) {
foreach ($localizedDocuments as $document) {
$documents[] = $document;
}
}
return $documents;
}
public function search(SearchQuery $searchQuery)
{
$hits = [];
$indexes = $searchQuery->getIndexes();
foreach ($indexes as $index) {
if (!isset($this->documents[$index])) {
continue;
}
foreach ($this->documents[$index] as $document) {
$hit = $this->factory->createQueryHit();
$isHit = false;
foreach ($document->getFields() as $field) {
$fieldValue = $field->getValue();
if (\is_array($fieldValue)) {
$fieldValue = \implode(' ', $fieldValue);
}
if (\preg_match('{' . \trim(\preg_quote($searchQuery->getQueryString())) . '}i', $fieldValue)) {
$isHit = true;
break;
}
}
if ($isHit) {
$hit->setDocument($document);
$hit->setScore(-1);
$hits[] = $hit;
}
}
}
return new SearchResult($hits, \count($hits));
}
public function listIndexes()
{
return \array_keys($this->documents);
}
public function flush(array $indexNames)
{
}
public function getStatus()
{
return [];
}
public function initialize()
{
// nothing to do here
}
}