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
<?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\Decorators\QueryBuilder;
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface;
use Maslosoft\Manganel\Helpers\TypeNamer;
use Maslosoft\Manganel\Interfaces\ManganelAwareInterface;
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
use Maslosoft\Manganel\SearchCriteria;
use Maslosoft\Manganel\Traits\ManganelAwareTrait;
use Maslosoft\Manganel\Traits\ModelsAwareTrait;
/**
* MultiModelDecorator
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class MultiModelDecorator implements ManganelAwareInterface,
ModelsAwareInterface
{
use ManganelAwareTrait,
ModelsAwareTrait;
public function __construct($models)
{
$this->models = $models;
}
public function decorate(&$body, SearchCriteria $initialCriteria)
{
$queries = [];
foreach ($this->models as $model)
{
$modelCriteria = null;
if ($model instanceof CriteriaAwareInterface)
{
$modelCriteria = $model->getCriteria();
}
elseif ($model instanceof WithCriteriaInterface)
{
$modelCriteria = $model->getDbCriteria(false);
}
if (empty($modelCriteria))
{
$criteria = $initialCriteria;
}
else
{
$criteria = clone $initialCriteria;
$criteria->mergeWith($modelCriteria);
}
$criteria->setModel($model);
$partial = [];
(new SingleModelDecorator())
->setManganel($this->manganel)
->decorate($partial, $criteria);
$query = [
'bool' => [
'filter' => [
'type' => [
'value' => TypeNamer::nameType($model)
]
],
'must' => $partial['query']
]
];
$queries[] = $query;
}
$body['query']['dis_max']['queries'] = $queries;
$common = [];
(new SingleModelDecorator)->setManganel($this->getManganel())->decorate($common, $initialCriteria);
unset($common['query']);
// Use foreach here, as $body is passed by ref
foreach ($common as $key => $value)
{
$body[$key] = $value;
}
}
}
API documentation generated by ApiGen