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\Traits\Finder\FinderHelpers; |
9
|
|
|
use Maslosoft\Manganel\Adapters\Finder\ElasticSearchAdapter; |
10
|
|
|
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface; |
11
|
|
|
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* SearchFinder |
15
|
|
|
* |
16
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
17
|
|
|
*/ |
18
|
|
|
class SearchFinder extends AbstractFinder implements FinderInterface, ModelsAwareInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
use FinderHelpers, |
22
|
|
|
UniqueModelsAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor |
26
|
|
|
* |
27
|
|
|
* @param object|object[] $models Model or array of model instances |
28
|
|
|
* @param IndexManager $im |
29
|
|
|
* @param Manganel $manganel |
30
|
|
|
*/ |
31
|
30 |
|
public function __construct($models, $im = null, $manganel = null) |
32
|
|
|
{ |
33
|
30 |
|
if (is_array($models)) |
34
|
|
|
{ |
35
|
11 |
|
$model = current($models); |
36
|
|
|
} |
37
|
|
|
else |
38
|
|
|
{ |
39
|
|
|
// Ensure array and that model is set for further usage |
40
|
19 |
|
$model = $models; |
41
|
19 |
|
$models = [$models]; |
42
|
|
|
} |
43
|
30 |
|
foreach ($models as $modelIndex => $modelSignature) |
44
|
|
|
{ |
45
|
30 |
|
if (is_string($modelSignature)) |
46
|
|
|
{ |
47
|
30 |
|
$models[$modelIndex] = new $modelSignature; |
48
|
|
|
} |
49
|
|
|
} |
50
|
30 |
|
if (is_string($model)) |
51
|
|
|
{ |
52
|
3 |
|
$model = new $model; |
53
|
|
|
} |
54
|
30 |
|
if (null === $manganel) |
55
|
|
|
{ |
56
|
30 |
|
$manganel = Manganel::create($model); |
57
|
|
|
} |
58
|
30 |
|
assert($model instanceof AnnotatedInterface); |
59
|
|
|
|
60
|
30 |
|
$this->setModel($model); |
61
|
30 |
|
$this->setModels($models); |
62
|
30 |
|
$this->setScopeManager(new MultiScopeManager($model, $models)); |
63
|
30 |
|
$this->setAdapter(new ElasticSearchAdapter($models)); |
64
|
|
|
|
65
|
30 |
|
$this->setProfiler($manganel->getProfiler()); |
66
|
30 |
|
$this->setFinderEvents(new Helpers\MultiFinderEvents()); |
67
|
30 |
|
$this->withCursor(false); |
68
|
30 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create search finder instance. |
72
|
|
|
* |
73
|
|
|
* @param AnnotatedInterface $model |
74
|
|
|
* @param IndexManager $im |
75
|
|
|
* @param Manganel $manganel |
76
|
|
|
* @return FinderInterface |
77
|
|
|
*/ |
78
|
|
|
public static function create(AnnotatedInterface $model, $im = null, Manganel $manganel = null) |
79
|
|
|
{ |
80
|
|
|
return new static($model, $im, $manganel); |
81
|
|
|
} |
82
|
|
|
|
83
|
21 |
|
protected function createModel($data) |
84
|
|
|
{ |
85
|
|
|
// Do not use second param for multi model |
86
|
|
|
// compatibility |
87
|
21 |
|
assert(array_key_exists('_class', $data), 'Stored document must have `_class` field'); |
88
|
21 |
|
return SearchArray::toModel($data); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|