1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Maslosoft\Mangan\Traits\DataProvider; |
10
|
|
|
|
11
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
12
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ConfigureTrait |
16
|
|
|
* |
17
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
18
|
|
|
*/ |
19
|
|
|
trait ConfigureTrait |
20
|
|
|
{ |
21
|
|
|
|
22
|
1 |
|
protected function configure($modelClass, $config) |
23
|
|
|
{ |
24
|
1 |
|
if (is_string($modelClass)) |
25
|
1 |
|
{ |
26
|
|
|
$this->model = new $modelClass; |
|
|
|
|
27
|
|
|
} |
28
|
1 |
|
elseif (is_object($modelClass)) |
29
|
|
|
{ |
30
|
1 |
|
$this->model = $modelClass; |
31
|
1 |
|
} |
32
|
|
|
else |
33
|
|
|
{ |
34
|
|
|
throw new ManganException('Invalid model type for ' . static::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
// Set criteria from model |
39
|
1 |
|
$criteria = $this->getCriteria(); |
40
|
1 |
|
if ($criteria instanceof MergeableInterface) |
|
|
|
|
41
|
1 |
|
{ |
42
|
|
|
// NOTE: WithCriteria and CriteriaAware have just slightly different method names |
43
|
|
|
if ($this->model instanceof WithCriteriaInterface) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$criteria->mergeWith($this->model->getDbCriteria()); |
46
|
|
|
} |
47
|
|
|
elseif ($this->model instanceof CriteriaAwareInterface) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$criteria->mergeWith($this->model->getCriteria()); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Merge criteria from configuration |
54
|
1 |
|
if (isset($config['criteria'])) |
55
|
1 |
|
{ |
56
|
|
|
$criteria->mergeWith($config['criteria']); |
57
|
|
|
unset($config['criteria']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Merge limit from configuration |
61
|
1 |
|
if (isset($config['limit']) && $config['limit'] > 0) |
62
|
1 |
|
{ |
63
|
|
|
$criteria->setLimit($config['limit']); |
64
|
|
|
unset($config['limit']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Merge sorting from configuration |
68
|
1 |
|
if (isset($config['sort'])) |
69
|
1 |
|
{ |
70
|
|
|
// Apply default sorting if criteria does not have sort configured |
71
|
|
|
if (isset($config['sort']['defaultOrder']) && empty($this->getCriteria()->getSort())) |
72
|
|
|
{ |
73
|
|
|
$criteria->setSort($config['sort']['defaultOrder']); |
74
|
|
|
} |
75
|
|
|
unset($config['sort']); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
if (!$criteria->getSelect()) |
79
|
1 |
|
{ |
80
|
1 |
|
$fields = array_keys(ManganMeta::create($this->model)->fields()); |
81
|
1 |
|
$selected = array_fill_keys($fields, true); |
82
|
1 |
|
$criteria->setSelect($selected); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
foreach ($config as $key => $value) |
86
|
|
|
{ |
87
|
|
|
$this->$key = $value; |
88
|
1 |
|
} |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return CriteriaInterface |
93
|
|
|
*/ |
94
|
|
|
abstract public function getCriteria(); |
95
|
|
|
} |
96
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: