Maslosoft Manganel API
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/manganel
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/manganel/
*/
namespace Maslosoft\Manganel;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Abstracts\AbstractFinder;
use Maslosoft\Mangan\Interfaces\FinderInterface;
use Maslosoft\Mangan\Traits\Finder\FinderHelpers;
use Maslosoft\Manganel\Adapters\Finder\ElasticSearchAdapter;
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait;
/**
* SearchFinder
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class SearchFinder extends AbstractFinder implements FinderInterface, ModelsAwareInterface
{
use FinderHelpers,
UniqueModelsAwareTrait;
/**
* Constructor
*
* @param object|object[] $models Model or array of model instances
* @param IndexManager $im
* @param Manganel $manganel
*/
public function __construct($models, $im = null, $manganel = null)
{
if (is_array($models))
{
$model = current($models);
}
else
{
// Ensure array and that model is set for further usage
$model = $models;
$models = [$models];
}
foreach ($models as $modelIndex => $modelSignature)
{
if (is_string($modelSignature))
{
$models[$modelIndex] = new $modelSignature;
}
}
if (is_string($model))
{
$model = new $model;
}
if (null === $manganel)
{
$manganel = Manganel::create($model);
}
assert($model instanceof AnnotatedInterface);
$this->setModel($model);
$this->setModels($models);
$this->setScopeManager(new MultiScopeManager($model, $models));
$this->setAdapter(new ElasticSearchAdapter($models));
$this->setProfiler($manganel->getProfiler());
$this->setFinderEvents(new Helpers\MultiFinderEvents());
$this->withCursor(false);
}
/**
* Create search finder instance.
*
* @param AnnotatedInterface $model
* @param IndexManager $im
* @param Manganel $manganel
* @return FinderInterface
*/
public static function create(AnnotatedInterface $model, $im = null, Manganel $manganel = null)
{
return new static($model, $im, $manganel);
}
protected function createModel($data)
{
// Do not use second param for multi model
// compatibility
assert(array_key_exists('_class', $data), 'Stored document must have `_class` field');
return SearchArray::toModel($data);
}
}
API documentation generated by ApiGen