1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link https://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Helpers\Index; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
17
|
|
|
use Maslosoft\Mangan\Command; |
18
|
|
|
use Maslosoft\Mangan\Criteria\ConditionDecorator; |
19
|
|
|
use Maslosoft\Mangan\Interfaces\InternationalInterface; |
20
|
|
|
use Maslosoft\Mangan\Mangan; |
21
|
|
|
use Maslosoft\Mangan\Meta\IndexMeta; |
22
|
|
|
|
23
|
|
|
class IndexModel |
24
|
|
|
{ |
25
|
|
|
private $model = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var IndexMeta |
29
|
|
|
*/ |
30
|
|
|
private $meta = null; |
31
|
|
|
|
32
|
|
|
private $indexes = []; |
33
|
|
|
|
34
|
8 |
|
public function __construct(AnnotatedInterface $model, IndexMeta $meta) |
35
|
|
|
{ |
36
|
8 |
|
$this->model = $model; |
37
|
8 |
|
$this->meta = $meta; |
38
|
8 |
|
} |
39
|
|
|
|
40
|
8 |
|
public function apply() |
41
|
|
|
{ |
42
|
8 |
|
$mn = Mangan::fromModel($this->model); |
43
|
8 |
|
$cd = new ConditionDecorator($this->model); |
44
|
|
|
|
45
|
8 |
|
$decorated = []; |
46
|
8 |
|
foreach ($this->meta->keys as $name => $sort) |
47
|
|
|
{ |
48
|
8 |
|
if ($this->model instanceof InternationalInterface) |
49
|
|
|
{ |
50
|
8 |
|
foreach ($this->model->getLanguages() as $code) |
51
|
|
|
{ |
52
|
8 |
|
assert(!empty($code)); |
53
|
|
|
|
54
|
|
|
// Reset cloned model languages to get only one |
55
|
|
|
// language that is currently selected. So that |
56
|
|
|
// decorated field name will have proper code. |
57
|
8 |
|
$cd->getModel()->setLang($code, false); |
58
|
8 |
|
$cd->getModel()->setLanguages([$code], false); |
59
|
|
|
|
60
|
8 |
|
$field = $cd->decorate($name); |
61
|
8 |
|
$key = key($field); |
62
|
8 |
|
$decorationKey = $code . '@' . $sort; |
63
|
8 |
|
if(empty($decorated[$decorationKey])) |
64
|
|
|
{ |
65
|
8 |
|
$decorated[$decorationKey] = []; |
66
|
|
|
} |
67
|
8 |
|
$decorated[$decorationKey][$key] = $sort; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
else |
71
|
|
|
{ |
72
|
|
|
$field = $cd->decorate($name); |
73
|
|
|
$key = key($field); |
74
|
|
|
$decorated[$key . '@' . $sort] = [$key => $sort]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
8 |
|
$cmd = new Command($this->model, $mn); |
78
|
|
|
|
79
|
8 |
|
$results = []; |
80
|
|
|
|
81
|
|
|
// Remove possible duplicated entries |
82
|
8 |
|
$unique = array_unique($decorated, SORT_REGULAR); |
83
|
8 |
|
$this->indexes = $unique; |
84
|
8 |
|
foreach ($unique as $keys) |
85
|
|
|
{ |
86
|
8 |
|
$results[] = (int)$cmd->createIndex($keys, $this->meta->options); |
87
|
|
|
} |
88
|
8 |
|
return array_sum($results) === count($results); |
89
|
|
|
} |
90
|
|
|
|
91
|
8 |
|
public function getIndexes() |
92
|
|
|
{ |
93
|
8 |
|
return $this->indexes; |
94
|
|
|
} |
95
|
|
|
} |