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
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
16
|
|
|
*/ |
17
|
|
|
class MultiScopeManager extends AbstractScopeManager implements ScopeManagerInterface, |
18
|
|
|
ModelsAwareInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
use UniqueModelsAwareTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* @param object $model Base model used for criteria |
26
|
|
|
* @param object[] $models Additional models used for criteria |
27
|
|
|
*/ |
28
|
30 |
|
public function __construct($model, $models = []) |
29
|
|
|
{ |
30
|
30 |
|
$this->setModel($model); |
31
|
30 |
|
$this->setModels($models); |
32
|
30 |
|
} |
33
|
|
|
|
34
|
9 |
|
public function getNewCriteria($criteria = null) |
35
|
|
|
{ |
36
|
9 |
|
$newCriteria = new SearchCriteria($criteria); |
37
|
9 |
|
$newCriteria->decorateWith($this->getModel()); |
38
|
9 |
|
$newCriteria->setModels($this->getModels()); |
39
|
9 |
|
return $newCriteria; |
40
|
|
|
} |
41
|
|
|
|
42
|
30 |
|
protected function getModelCriteria() |
43
|
|
|
{ |
44
|
30 |
|
$criteria = new SearchCriteria; |
45
|
|
|
|
46
|
30 |
|
foreach ($this->getModels() as $model) |
47
|
|
|
{ |
48
|
30 |
|
$criteria->mergeWith($this->getOneModelCriteria($model)); |
49
|
|
|
} |
50
|
30 |
|
$criteria->setModels($this->getModels()); |
51
|
|
|
|
52
|
30 |
|
if (empty($criteria)) |
53
|
|
|
{ |
54
|
|
|
return $this->getNewCriteria(); |
55
|
|
|
} |
56
|
30 |
|
return $criteria; |
57
|
|
|
} |
58
|
|
|
|
59
|
30 |
|
private function getOneModelCriteria($model) |
60
|
|
|
{ |
61
|
30 |
|
if ($model instanceof WithCriteriaInterface) |
62
|
|
|
{ |
63
|
|
|
return $model->getDbCriteria(); |
64
|
|
|
} |
65
|
|
|
elseif ($model instanceof CriteriaAwareInterface) |
66
|
|
|
{ |
67
|
|
|
return $model->getCriteria(); |
68
|
|
|
} |
69
|
|
|
else |
70
|
|
|
{ |
71
|
30 |
|
return new SearchCriteria; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|