|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: peter |
|
5
|
|
|
* Date: 17.12.17 |
|
6
|
|
|
* Time: 15:13 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Maslosoft\Mangan\Helpers\Index; |
|
10
|
|
|
|
|
11
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
12
|
|
|
use Maslosoft\Mangan\Command; |
|
13
|
|
|
use Maslosoft\Mangan\Criteria\ConditionDecorator; |
|
14
|
|
|
use Maslosoft\Mangan\Interfaces\InternationalInterface; |
|
15
|
|
|
use Maslosoft\Mangan\Mangan; |
|
16
|
|
|
use Maslosoft\Mangan\Meta\IndexMeta; |
|
17
|
|
|
|
|
18
|
|
|
class IndexModel |
|
19
|
|
|
{ |
|
20
|
|
|
private $model = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var IndexMeta |
|
24
|
|
|
*/ |
|
25
|
|
|
private $meta = null; |
|
26
|
|
|
|
|
27
|
|
|
private $indexes = []; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(AnnotatedInterface $model, IndexMeta $meta) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->model = $model; |
|
32
|
|
|
$this->meta = $meta; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function apply() |
|
36
|
|
|
{ |
|
37
|
|
|
$mn = Mangan::fromModel($this->model); |
|
38
|
|
|
$cd = new ConditionDecorator($this->model); |
|
39
|
|
|
|
|
40
|
|
|
$decorated = []; |
|
41
|
|
|
foreach ($this->meta->keys as $name => $sort) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($this->model instanceof InternationalInterface) |
|
44
|
|
|
{ |
|
45
|
|
|
foreach ($this->model->getLanguages() as $code) |
|
46
|
|
|
{ |
|
47
|
|
|
assert(!empty($code)); |
|
48
|
|
|
|
|
49
|
|
|
// Reset cloned model languages to get only one |
|
50
|
|
|
// language that is currently selected. So that |
|
51
|
|
|
// decorated field name will have proper code. |
|
52
|
|
|
$cd->getModel()->setLang($code, false); |
|
|
|
|
|
|
53
|
|
|
$cd->getModel()->setLanguages([$code], false); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$field = $cd->decorate($name); |
|
56
|
|
|
$key = key($field); |
|
57
|
|
|
$decorationKey = $code . '@' . $sort; |
|
58
|
|
|
if(empty($decorated[$decorationKey])) |
|
59
|
|
|
{ |
|
60
|
|
|
$decorated[$decorationKey] = []; |
|
61
|
|
|
} |
|
62
|
|
|
$decorated[$decorationKey][$key] = $sort; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
else |
|
66
|
|
|
{ |
|
67
|
|
|
$field = $cd->decorate($name); |
|
68
|
|
|
$key = key($field); |
|
69
|
|
|
$decorated[$key . '@' . $sort] = [$key => $sort]; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
$cmd = new Command($this->model, $mn); |
|
73
|
|
|
|
|
74
|
|
|
$results = []; |
|
75
|
|
|
|
|
76
|
|
|
// Remove possible duplicated entries |
|
77
|
|
|
$unique = array_unique($decorated, SORT_REGULAR); |
|
78
|
|
|
$this->indexes = $unique; |
|
79
|
|
|
foreach ($unique as $keys) |
|
80
|
|
|
{ |
|
81
|
|
|
$results[] = (int)$cmd->createIndex($keys, $this->meta->options); |
|
82
|
|
|
} |
|
83
|
|
|
return array_sum($results) === count($results); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getIndexes() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->indexes; |
|
89
|
|
|
} |
|
90
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: