Maslosoft Mangan 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
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Helpers\Index;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Command;
use Maslosoft\Mangan\Criteria\ConditionDecorator;
use Maslosoft\Mangan\Interfaces\InternationalInterface;
use Maslosoft\Mangan\Mangan;
use Maslosoft\Mangan\Meta\IndexMeta;
class IndexModel
{
private $model = null;
/**
* @var IndexMeta
*/
private $meta = null;
private $indexes = [];
public function __construct(AnnotatedInterface $model, IndexMeta $meta)
{
$this->model = $model;
$this->meta = $meta;
}
public function apply()
{
$mn = Mangan::fromModel($this->model);
$cd = new ConditionDecorator($this->model);
$decorated = [];
foreach ($this->meta->keys as $name => $sort)
{
if ($this->model instanceof InternationalInterface)
{
foreach ($this->model->getLanguages() as $code)
{
assert(!empty($code));
// Reset cloned model languages to get only one
// language that is currently selected. So that
// decorated field name will have proper code.
$cd->getModel()->setLang($code, false);
$cd->getModel()->setLanguages([$code], false);
$field = $cd->decorate($name);
$key = key($field);
$decorationKey = $code . '@' . $sort;
if(empty($decorated[$decorationKey]))
{
$decorated[$decorationKey] = [];
}
$decorated[$decorationKey][$key] = $sort;
}
}
else
{
$field = $cd->decorate($name);
$key = key($field);
$decorated[$key . '@' . $sort] = [$key => $sort];
}
}
$cmd = new Command($this->model, $mn);
$results = [];
// Remove possible duplicated entries
$unique = array_unique($decorated, SORT_REGULAR);
$this->indexes = $unique;
foreach ($unique as $keys)
{
$results[] = (int)$cmd->createIndex($keys, $this->meta->options);
}
return array_sum($results) === count($results);
}
public function getIndexes()
{
return $this->indexes;
}
}
API documentation generated by ApiGen