-
Notifications
You must be signed in to change notification settings - Fork 0
/
InMemorySearchResultStore.php
74 lines (61 loc) · 2.1 KB
/
InMemorySearchResultStore.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
<?php
namespace SelrahcD\SearchCache\Tests\Stubs\SearchResultsStores;
use SelrahcD\SearchCache\SearchResult;
use SelrahcD\SearchCache\SearchResultStores\SearchResultsStore;
use SelrahcD\SearchCache\SharedSearchResult;
final class InMemorySearchResultStore implements SearchResultsStore
{
private $searchResults = [];
private $sharedSearchResults = [];
/**
* Stores a search result using a key
* @param SearchResult $searchResult
* @return void
*/
public function store(SearchResult $searchResult)
{
$this->searchResults[$searchResult->getKey()] = $searchResult;
}
/**
* Stores a shared result using a key
* @param SharedSearchResult $searchResult
* @return void
*/
public function storeSharedResult(SharedSearchResult $searchResult)
{
$this->sharedSearchResults[$searchResult->getKey()] = $searchResult;
}
/**
* Retrieves a result using a key
* @param $key
* @return SearchResult | null
*/
public function getResult($key)
{
return isset($this->searchResults[$key])? $this->searchResults[$key] : null;
}
/**
* Retrieves a shared result using key
* @param $key
* @return SharedSearchResult | null
*/
public function getSharedResult($key)
{
return isset($this->sharedSearchResults[$key])? $this->sharedSearchResults[$key] : null;
}
public function containsResult(array $expectedResult, \DateTimeImmutable $expirationDate)
{
$filteredArray = array_filter($this->searchResults, function($storedResult) use($expectedResult, $expirationDate) {
return $storedResult->getResult() == $expectedResult
&& $storedResult->getExpirationDate() == $expirationDate;
});
return !empty($filteredArray);
}
public function containsSharedResult(array $expectedResult)
{
$filteredArray = array_filter($this->sharedSearchResults, function($storedResult) use($expectedResult) {
return $storedResult->getResult() == $expectedResult;
});
return !empty($filteredArray);
}
}