1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel; |
14
|
|
|
|
15
|
|
|
use Maslosoft\Mangan\Abstracts\AbstractScopeManager; |
16
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface; |
18
|
|
|
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface; |
19
|
|
|
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface; |
20
|
|
|
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Scope Manager with support for many models |
24
|
|
|
* |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class MultiScopeManager extends AbstractScopeManager implements ScopeManagerInterface, |
28
|
|
|
ModelsAwareInterface |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
use UniqueModelsAwareTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @param object $model Base model used for criteria |
36
|
|
|
* @param object[] $models Additional models used for criteria |
37
|
|
|
*/ |
38
|
55 |
|
public function __construct($model, $models = []) |
39
|
|
|
{ |
40
|
55 |
|
$this->setModel($model); |
41
|
55 |
|
$this->setModels($models); |
42
|
55 |
|
} |
43
|
|
|
|
44
|
9 |
|
public function getNewCriteria($criteria = null) |
45
|
|
|
{ |
46
|
9 |
|
$newCriteria = new SearchCriteria($criteria); |
47
|
9 |
|
$newCriteria->decorateWith($this->getModel()); |
48
|
9 |
|
$newCriteria->setModels($this->getModels()); |
49
|
9 |
|
return $newCriteria; |
50
|
|
|
} |
51
|
|
|
|
52
|
55 |
|
protected function getModelCriteria() |
53
|
|
|
{ |
54
|
55 |
|
$criteria = new SearchCriteria; |
55
|
|
|
|
56
|
55 |
|
foreach ($this->getModels() as $model) |
57
|
|
|
{ |
58
|
55 |
|
$criteria->mergeWith($this->getOneModelCriteria($model)); |
59
|
|
|
} |
60
|
55 |
|
$criteria->setModels($this->getModels()); |
61
|
|
|
|
62
|
55 |
|
if (empty($criteria)) |
63
|
|
|
{ |
64
|
|
|
return $this->getNewCriteria(); |
65
|
|
|
} |
66
|
55 |
|
return $criteria; |
67
|
|
|
} |
68
|
|
|
|
69
|
55 |
|
private function getOneModelCriteria($model) |
70
|
|
|
{ |
71
|
55 |
|
if ($model instanceof WithCriteriaInterface) |
72
|
|
|
{ |
73
|
1 |
|
return $model->getDbCriteria(); |
74
|
|
|
} |
75
|
54 |
|
elseif ($model instanceof CriteriaAwareInterface) |
76
|
|
|
{ |
77
|
|
|
return $model->getCriteria(); |
78
|
|
|
} |
79
|
|
|
else |
80
|
|
|
{ |
81
|
54 |
|
return new SearchCriteria; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|