1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maslosoft\Manganel; |
4
|
|
|
|
5
|
|
|
use Maslosoft\Mangan\Abstracts\AbstractScopeManager; |
6
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface; |
7
|
|
|
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface; |
8
|
|
|
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface; |
9
|
|
|
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface; |
10
|
|
|
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Scope Manager with support for many models |
14
|
|
|
* |
15
|
|
|
* TODO, maybe it's not even necessary? Will be known when SearchFinder will be |
16
|
|
|
* implemented... |
17
|
|
|
* |
18
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
19
|
|
|
*/ |
20
|
|
|
class MultiScopeManager extends AbstractScopeManager implements ScopeManagerInterface, |
21
|
|
|
ModelsAwareInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
use UniqueModelsAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @param object $model Base model used for criteria |
29
|
|
|
* @param object[] $models Additional models used for criteria |
30
|
|
|
*/ |
31
|
4 |
|
public function __construct($model, $models = []) |
32
|
|
|
{ |
33
|
4 |
|
$this->setModel($model); |
34
|
4 |
|
$this->setModels($models); |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
public function getNewCriteria($criteria = null) |
38
|
|
|
{ |
39
|
|
|
$newCriteria = new SearchCriteria($criteria); |
40
|
|
|
$newCriteria->decorateWith($this->getModel()); |
41
|
|
|
return $newCriteria; |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
protected function getModelCriteria() |
45
|
|
|
{ |
46
|
3 |
|
$criteria = new SearchCriteria; |
47
|
|
|
|
48
|
3 |
|
foreach ($this->getModels() as $model) |
49
|
|
|
{ |
50
|
3 |
|
$criteria->mergeWith($this->getOneModelCriteria($model)); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
if (empty($criteria)) |
54
|
|
|
{ |
55
|
|
|
return $this->getNewCriteria(); |
56
|
|
|
} |
57
|
3 |
|
return $criteria; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
private function getOneModelCriteria($model) |
61
|
|
|
{ |
62
|
3 |
|
if ($model instanceof WithCriteriaInterface) |
63
|
|
|
{ |
64
|
|
|
return $model->getDbCriteria(); |
65
|
|
|
} |
66
|
|
|
elseif ($model instanceof CriteriaAwareInterface) |
67
|
|
|
{ |
68
|
|
|
return $model->getCriteria(); |
69
|
|
|
} |
70
|
|
|
else |
71
|
|
|
{ |
72
|
3 |
|
return new SearchCriteria; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|