|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Search; |
|
4
|
|
|
|
|
5
|
|
|
use \RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
use \Psr\Log\LoggerAwareInterface; |
|
8
|
|
|
use \Psr\Log\LoggerAwareTrait; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractSearch implements SearchInterface, LoggerAwareInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use LoggerAwareTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The (raw) search results. |
|
19
|
|
|
* @var array $results |
|
20
|
|
|
*/ |
|
21
|
|
|
private $results; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* [$modelFactory description] |
|
25
|
|
|
* @var [type] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $modelFactory; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $data Class options and dependencies. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(array $data) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->setLogger($data['logger']); |
|
36
|
|
|
$this->setModelFactory($data['modelFactory']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* [setModelFactory description] |
|
41
|
|
|
* @param [type] $factory [description] |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
public function setModelFactory($factory) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->modelFactory = $factory; |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* [modelFactory description] |
|
51
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
|
|
public function modelFactory() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->modelFactory; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $keyword The searched-for keyword. |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract public function search($keyword); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* A search is always callable (alias to the "search" method). |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $keyword The searched-for keyword. |
|
68
|
|
|
* @return array The results. |
|
69
|
|
|
*/ |
|
70
|
|
|
final public function __invoke($keyword) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->search($keyword); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $resultType The type of results to search. Can be only "raw" for now. |
|
77
|
|
|
* @throws RuntimeException If this method is called before a search was executed. |
|
78
|
|
|
* @return array The results from the last search operation. |
|
79
|
|
|
*/ |
|
80
|
|
|
final public function lastResults($resultType = 'raw') |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->results === null) { |
|
83
|
|
|
throw new RuntimeException( |
|
84
|
|
|
'Search was never performed' |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($resultType === 'raw') { |
|
89
|
|
|
return $this->lastResults; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $results The (raw) search results. |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
final protected function setResults(array $results) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->results = $results; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.