|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Stinger Enity Search package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
|
7
|
|
|
* (c) Florian Meyer <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Services; |
|
13
|
|
|
|
|
14
|
|
|
use StingerSoft\EntitySearchBundle\Model\Document; |
|
15
|
|
|
use StingerSoft\EntitySearchBundle\Model\Query; |
|
16
|
|
|
use StingerSoft\EntitySearchBundle\Model\ResultSetAdapter; |
|
17
|
|
|
use StingerSoft\EntitySearchBundle\Model\Result\FacetSetAdapter; |
|
18
|
|
|
use StingerSoft\EntitySearchBundle\Model\Result\FacetSet; |
|
19
|
|
|
use StingerSoft\EntitySearchBundle\Model\Result\FacetAdapter; |
|
20
|
|
|
|
|
21
|
|
|
class DummySearchService extends AbstractSearchService { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* @var Document[] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $index = array(); |
|
28
|
|
|
|
|
29
|
|
|
protected function createDocumentId(Document $document) { |
|
30
|
|
|
$id = $document->getEntityClass(); |
|
31
|
|
|
$id .= '#'; |
|
32
|
|
|
$entityId = $document->getEntityId(); |
|
33
|
|
|
if(is_scalar($entityId)) { |
|
34
|
|
|
$id .= $entityId; |
|
35
|
|
|
} else { |
|
36
|
|
|
$id .= md5(serialize($entityId)); |
|
37
|
|
|
} |
|
38
|
|
|
return $id; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
* |
|
45
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::clearIndex() |
|
46
|
|
|
*/ |
|
47
|
|
|
public function clearIndex() { |
|
48
|
|
|
$this->index = array(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* |
|
53
|
|
|
* {@inheritDoc} |
|
54
|
|
|
* |
|
55
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::saveDocument() |
|
56
|
|
|
*/ |
|
57
|
|
|
public function saveDocument(Document $document) { |
|
58
|
|
|
$this->index[$this->createDocumentId($document)] = $document; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* {@inheritDoc} |
|
64
|
|
|
* |
|
65
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::removeDocument() |
|
66
|
|
|
*/ |
|
67
|
|
|
public function removeDocument(Document $document) { |
|
68
|
|
|
unset($this->index[$this->createDocumentId($document)]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* |
|
73
|
|
|
* {@inheritDoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::autocomplete() |
|
76
|
|
|
*/ |
|
77
|
|
|
public function autocomplete($search, $maxResults = 10) { |
|
78
|
|
|
$words = array(); |
|
79
|
|
|
foreach($this->index as $doc) { |
|
80
|
|
|
foreach($doc->getFields() as $content) { |
|
81
|
|
|
if(is_string($content)) { |
|
82
|
|
|
$words = array_merge($words, explode(' ', $content)); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return array_filter($words, function ($word) use ($search) { |
|
88
|
|
|
return stripos($word, $search) === 0; |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* |
|
94
|
|
|
* {@inheritDoc} |
|
95
|
|
|
* |
|
96
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::search() |
|
97
|
|
|
*/ |
|
98
|
|
|
public function search(Query $query) { |
|
99
|
|
|
$term = $query->getSearchTerm(); |
|
100
|
|
|
|
|
101
|
|
|
$result = new ResultSetAdapter(); |
|
102
|
|
|
$facets = new FacetSetAdapter(); |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$hits = array(); |
|
106
|
|
|
foreach($this->index as $key => $doc) { |
|
107
|
|
|
foreach($doc->getFields() as $content) { |
|
108
|
|
|
if(is_string($content) && stripos($content, $term) !== false) { |
|
109
|
|
|
if(!isset($hits[$key])) { |
|
110
|
|
|
$hits[$key] = 0; |
|
111
|
|
|
} |
|
112
|
|
|
$hits[$key] = $hits[$key] + 1; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
arsort($hits); |
|
117
|
|
|
$results = array(); |
|
118
|
|
|
foreach(array_keys($hits) as $docId){ |
|
119
|
|
|
$doc = $this->index[$docId]; |
|
120
|
|
|
$facets->addFacetValue(FacetSet::FACET_ENTITY_TYPE, $doc->getEntityClass()); |
|
121
|
|
|
$facets->addFacetValue(FacetSet::FACET_AUTHOR, $doc->getFieldValue(Document::FIELD_AUTHOR)); |
|
122
|
|
|
$results[] = $doc; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$result->setResults($results); |
|
126
|
|
|
$result->setFacets($facets); |
|
127
|
|
|
|
|
128
|
|
|
return $result; |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.