|
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
|
28 |
|
public function __construct($models, $im = null, $manganel = null) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
28 |
|
if (is_array($models)) |
|
34
|
|
|
{ |
|
35
|
|
|
$model = current($models); |
|
36
|
|
|
} |
|
37
|
|
|
else |
|
38
|
|
|
{ |
|
39
|
|
|
// Ensure array and that model is set for further usage |
|
40
|
28 |
|
$model = $models; |
|
41
|
28 |
|
$models = [$models]; |
|
42
|
|
|
} |
|
43
|
28 |
|
if (null === $manganel) |
|
44
|
|
|
{ |
|
45
|
28 |
|
$manganel = Manganel::create($model); |
|
46
|
|
|
} |
|
47
|
28 |
|
assert($model instanceof AnnotatedInterface); |
|
48
|
|
|
|
|
49
|
28 |
|
$this->setModel($model); |
|
50
|
28 |
|
$this->setModels($models); |
|
51
|
28 |
|
$this->setScopeManager(new MultiScopeManager($model, $models)); |
|
52
|
28 |
|
$this->setAdapter(new ElasticSearchAdapter($models)); |
|
53
|
|
|
|
|
54
|
28 |
|
$this->setProfiler($manganel->getProfiler()); |
|
55
|
28 |
|
$this->setFinderEvents(new Helpers\MultiFinderEvents()); |
|
56
|
28 |
|
$this->withCursor(false); |
|
57
|
28 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create search finder instance. |
|
61
|
|
|
* |
|
62
|
|
|
* @param AnnotatedInterface $model |
|
63
|
|
|
* @param IndexManager $im |
|
64
|
|
|
* @param Manganel $manganel |
|
65
|
|
|
* @return FinderInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function create(AnnotatedInterface $model, $im = null, Manganel $manganel = null) |
|
68
|
|
|
{ |
|
69
|
|
|
return new static($model, $im, $manganel); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
19 |
|
protected function createModel($data) |
|
73
|
|
|
{ |
|
74
|
|
|
// Do not use second param for multi model |
|
75
|
|
|
// compatibility |
|
76
|
19 |
|
assert(array_key_exists('_class', $data), 'Stored document must have `_class` field'); |
|
77
|
19 |
|
return SearchArray::toModel($data); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.