1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maslosoft\Manganel; |
4
|
|
|
|
5
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
6
|
|
|
use Maslosoft\Mangan\Abstracts\AbstractFinder; |
7
|
|
|
use Maslosoft\Mangan\Interfaces\FinderInterface; |
8
|
|
|
use Maslosoft\Mangan\Profillers\NullProfiler; |
9
|
|
|
use Maslosoft\Mangan\Traits\Finder\FinderHelpers; |
10
|
|
|
use Maslosoft\Manganel\Adapters\Finder\ElasticSearchAdapter; |
11
|
|
|
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface; |
12
|
|
|
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* SearchFinder |
16
|
|
|
* |
17
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
18
|
|
|
*/ |
19
|
|
|
class SearchFinder extends AbstractFinder implements FinderInterface, ModelsAwareInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use FinderHelpers, |
23
|
|
|
UniqueModelsAwareTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param object|object[] $models Model or array of model instances |
29
|
|
|
* @param IndexManager $im |
30
|
|
|
* @param Manganel $manganel |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct($models, $im = null, $manganel = null) |
|
|
|
|
33
|
|
|
{ |
34
|
4 |
|
if (is_array($models)) |
35
|
|
|
{ |
36
|
|
|
$model = current($model); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
else |
39
|
|
|
{ |
40
|
|
|
// Ensure array and that model is set for further usage |
41
|
4 |
|
$model = $models; |
42
|
4 |
|
$models = [$models]; |
43
|
|
|
} |
44
|
4 |
|
if (null === $manganel) |
45
|
|
|
{ |
46
|
4 |
|
$manganel = Manganel::create($model); |
|
|
|
|
47
|
|
|
} |
48
|
4 |
|
assert($model instanceof AnnotatedInterface); |
49
|
|
|
|
50
|
4 |
|
$this->setModel($model); |
51
|
4 |
|
$this->setModels($models); |
52
|
4 |
|
$this->setScopeManager(new MultiScopeManager($model, $models)); |
53
|
4 |
|
$this->setAdapter(new ElasticSearchAdapter($models)); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* TODO Use profiler here if any available |
57
|
|
|
*/ |
58
|
4 |
|
$this->setProfiler(new NullProfiler); |
59
|
4 |
|
$this->setFinderEvents(new Helpers\MultiFinderEvents()); |
60
|
4 |
|
$this->withCursor(false); |
61
|
4 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create search finder instance. |
65
|
|
|
* |
66
|
|
|
* @param AnnotatedInterface $model |
67
|
|
|
* @param IndexManager $im |
68
|
|
|
* @param Manganel $manganel |
69
|
|
|
* @return FinderInterface |
70
|
|
|
*/ |
71
|
|
|
public static function create(AnnotatedInterface $model, $im = null, Manganel $manganel = null) |
72
|
|
|
{ |
73
|
|
|
return new static($model, $im, $manganel); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function createModel($data) |
77
|
|
|
{ |
78
|
|
|
// Do not use second param for multi model |
79
|
|
|
// compatibility |
80
|
|
|
assert(array_key_exists('_class', $data), 'Stored document must have `_class` field'); |
81
|
|
|
return SearchArray::toModel($data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.